我想膨胀 同样,我可以将 R.id.catText
,但它从来没有显示如果我膨胀它自己。如果我充气 R.id.assets
(容器),那么两个元素都显示得很好。我只是不想要那个容器。如何使充气 R.id.catText
而不使充气 R.id.assets
?R.id.catText
膨胀为一个对象吗?如。
我代码:TextView catText = inflater.inflate(R.id.catText);
someView.addView(new catText());
LinearLayout myRoot = (LinearLayout) findViewById(R.id.assets);
LayoutInflater inflater = (LayoutInflater)this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view = inflater.inflate(R.layout.assets, myRoot, false);
我的风格:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:weightSum="1"
android:padding="50px" android:id="@+id/assets">
<TextView android:id="@+id/catText" android:text="TextView" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textColor="#ff0000"></TextView>
</LinearLayout>
LayoutInflater
只适用于R.layout.*
。如果您只想填充TextView
,则应该将其放入自己的布局XML文件中。如果您有时需要容器,但其他时候不需要,您可以在容器布局中包含TextView
。
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/catText"
android:text="TextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#ff0000"/>
在容器xml文件中:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:weightSum="1"
android:padding="50px"
android:id="@+id/assets">
<include layout="@layout/catText"/>
</LinearLayout>
然后你可以把你需要的充气。
从XML布局创建视图对象实际上并不需要一个膨胀调用。你应该使用findViewByID来初始化视图,也就是TextView。
你能解释一下你到底想用一个膨胀调用TextView吗?