74

include私の例のようにXML タグを使用する代わりに、プログラムでレイアウトを含める方法を探しています 。

  <include layout="@layout/message"  
           android:layout_width="match_parent" 
           android:layout_height="match_parent" 
           android:layout_weight="0.75"/>

このパラメータ " layout="@layout/message " をプログラムで変更する必要があります。

これを行う方法はありますか?

4

3 に答える 3

168

ViewStubの代わりにa を使用しincludeます。

<ViewStub
    android:id="@+id/layout_stub"
    android:inflatedId="@+id/message_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_weight="0.75" />

次にコードで、スタブへの参照を取得し、そのレイアウト リソースを設定して、インフレートします。

ViewStub stub = (ViewStub) findViewById(R.id.layout_stub);
stub.setLayoutResource(R.layout.whatever_layout_you_want);
View inflated = stub.inflate();
于 2013-09-25T08:18:39.500 に答える
3

Mono.Droid / Xamarin では、これは私にとってはうまくいきました:

ViewStub stub = FindViewById<ViewStub>(Resource.Id.layout_stub);
stub.LayoutResource = Resource.Layout.whatever_layout_you_want;
stub.Inflate();
于 2016-05-09T12:54:09.283 に答える