我如何以编程方式在Android中包含布局?

我正在寻找一种方法来包含一个布局编程,而不是像我的例子中使用的XML标签:

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

请以编程方式更改此参数“ layout =”@ layout / message “。

任何想法如何做到这一点?

使用ViewStub而不是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(); 
 ViewStub stub = (ViewStub) findViewById(R.id.text_post); stub.setLayoutResource(R.layout.profile_header); View inflated = stub.inflate(); 

在Mono.Droid / Xamarin这工作对我来说:

 ViewStub stub = FindViewById<ViewStub>(Resource.Id.layout_stub); stub.LayoutResource = Resource.Layout.whatever_layout_you_want; stub.Inflate();