1

を膨らませ たいのです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>
4

2 に答える 2

2

のみでLayoutInflater動作しR.layout.*ます。だけを膨張させたい場合はTextView、それを独自のレイアウト XML ファイルに入れる必要があります。コンテナーが必要な場合があり、他では必要ない場合はTextView、コンテナー レイアウトに含めることができます。

catText.xml で:

<?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>

その後、必要な方を膨らませることができます。

于 2011-08-03T00:03:22.327 に答える
0

XML レイアウトから View オブジェクトを作成する場合、実際には inflate 呼び出しは必要ありません。ビュー、つまりこの場合は TextView を初期化するには、findViewByID を使用する必要があります。

TextView の膨張呼び出しを使用しようとしていることを正確に説明できますか?

于 2011-08-02T19:07:49.847 に答える