2

ループを使用してレイアウトを複数回膨らませて、別のレイアウトにしたい。TextViewまたはその他の単一のビューを拡張する場合、Javaは問題ありません。しかし、私はこのレイアウト全体を膨らませたいです。次のコードは機能しないため、これにはどの方法を使用しますか。(私が言ったように、ループとすべては問題ありません、それはレイアウト全体を膨らませることができません。私は単純なビューを膨らませる方法しか知りません)。

View childInflatedView = (View) theInflater.inflate(R.id.restaurant_displayed_on_list, null);
linLayout.addView(childInflatedView);
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="30dp"
android:orientation="horizontal"
android:background="@drawable/gradient_bg_hover"
android:id="@+id/restaurant_displayed_on_list" >

<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="0dp"
    android:layout_weight="6"
    android:layout_height="fill_parent"
    android:text="this is the inflated text"
    android:textColor="#990099"
    android:textSize="20dp"
    android:gravity="center_horizontal"

    />

<Button    
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="fill_parent"
android:background= "@drawable/delete"
/>
</LinearLayout>

編集1:

Matthewが述べたように、解決策はレイアウトリソースIDによってレイアウトを膨らませることだと思います。レイアウトをTextViewにキャストできないため、上記に示されていないこのコード行に問題があると思います。

View categoryInflatedView1 = (View) theInflater.inflate(R.layout.categories_available, null);

((TextView) categoryInflatedView1).setText(category1);          

linLayout.addView(categoryInflatedView1);

レイアウト内にあるTextViewにアクセスするにはどうすればよいですか。膨らませる前にテキストビューを編集する必要があります。

4

3 に答える 3

2

代わりに、レイアウトリソースIDを渡す必要があります。に置き換えればR.id.restaurant_displayed_on_listR.layout.<name of your layout file here>うまくいくはずです。

于 2012-10-30T07:06:15.640 に答える
1

したがって、これが機能しなかった理由は2つあります。

マシューが述べたように、レイアウトリソースIDを介してレイアウトを膨らませるだけです

View childInflatedView = (View) theInflater.inflate(R.layout.restaurant_available, null);

レイアウトをTextViewにキャストしたために、ログで追加のエラーが発生していました。代わりに、このコードを使用して機能させる必要があります(レイアウト変数を使用してから、IDでビューを検索します)。

((TextView) childInflatedView.findViewById(R.id.restaurant_displayed_on_list)).setText(currentlyDelRest1);

この答えは正しいですが、質問で入手可能な情報を考慮して、彼の答えが正しかったので、受け入れられた答えをマシューに割り当てます。助けてくれてありがとう

于 2012-10-30T15:12:09.270 に答える
0

<include>タグを使用してください。これには、既存のXMLレイアウト内に別のXMLレイアウトが含まれます。たとえば、以下の2行を追加すると、親レイアウトに他の2つのレイアウトが含まれます。

 <include android:id="@+id/cell1" layout="@layout/workspace_screen" />
<include android:id="@+id/cell2" layout="@layout/workspace_screen" />

唯一の場合、layout属性が必要です。この属性は、android名前空間プレフィックスなしで、拡張するレイアウトファイルへの参照です。android:id含まれるレイアウトのルートビューのIDを指定するために使用することもできます。また、定義されている場合は、含まれているレイアウトのIDをオーバーライドします。同様に、すべてのレイアウトパラメータを上書きできます。

于 2012-10-30T07:29:19.057 に答える