如何在Android中以编程方式包含布局?
我正在寻找一种以编程方式包含布局的方法,而不是像我的示例中那样使用XML标记如何在Android中以编程方式包含布局?,android,android-layout,parameters,include,xamarin.android,Android,Android Layout,Parameters,Include,Xamarin.android,我正在寻找一种以编程方式包含布局的方法,而不是像我的示例中那样使用XML标记include: <include layout="@layout/message" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_weight="0.75"/> 需要以编程方式更改此参数“layo
include
:
<include layout="@layout/message"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="0.75"/>
需要以编程方式更改此参数“layout=“@layout/message”
你知道怎么做吗?使用
viewsub
而不是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" />
在Mono.Droid/Xamarin中,这对我很有用:
ViewStub stub = FindViewById<ViewStub>(Resource.Id.layout_stub);
stub.LayoutResource = Resource.Layout.whatever_layout_you_want;
stub.Inflate();
ViewStub stub=findviewbyd(Resource.Id.layout\u stub);
stub.LayoutResource=Resource.Layout.which_Layout_所需;
充气();
据我从您的问题中了解,您正在寻找一种以编程方式更改布局参数的方法,因此下面是链接:而且。您不能这样做。您可以在代码中膨胀布局并将膨胀视图添加到父布局。或者您可以使用片段。好的。如果您想更改“layout“paramater,KCopock的答案是合适的。@KCopock谢谢,没关系,但我如何在我的布局中调用TextView,因为使用此代码TextView close1=(TextView)findViewById(R.id.close1);我有error@kcoppock以及如何在单击按钮后更改视图(我有4个视图)无需将膨胀视图保存在单独的变量中,布局将添加到原始视图中,以便可以像往常一样使用findViewById取消检索组件,因此只需调用stub.inflate();除非您立即要对膨胀的结果执行某些操作。是否还有其他方法可以通过编程方式使ViewStub不可见/可见..?您将如何使用profile_header.xml中的视图?
ViewStub stub = (ViewStub) findViewById(R.id.text_post);
stub.setLayoutResource(R.layout.profile_header);
View inflated = stub.inflate();
ViewStub stub = FindViewById<ViewStub>(Resource.Id.layout_stub);
stub.LayoutResource = Resource.Layout.whatever_layout_you_want;
stub.Inflate();