问题描述
xml文件RelativeLayout_A.xml代码RelativeLayout_A.xml<RelativeLayout id="@+id/A">........<RelativeLayout id="@+id/B">........<RelativeLayout id="@+id/C"></RelativeLayout>........</RelativeLayout>........</RelativeLayout>RelativeLayout_A1.xml 去除C的布局RelativeLayout_C1.xml C1的布局RelativeLayout_C2.xml C2的布局其中RelativeLayout C的布局会变化,但是其它布局不变我想在Java中动态的添加会变的C,比如添加C1,删除C1,添加C2等等怎么实现啊,以前都是xml布局,不会java布局详细一点,先谢谢大家了
解决方案
可以使用ViewStub大约参考代码如下:main.xml<?xml version="1.0" encoding="utf-8"?><merge xmlns:android="http://schemas.android.com/apk/res/android"><LinearLayout android:orientation="vertical"android:layout_width="fill_parent" android:layout_height="fill_parent"><TextView android:layout_width="fill_parent"android:layout_height="wrap_content" android:text="Showing ViewStub" /><Button android:id="@+id/openstub" android:layout_width="fill_parent"android:layout_height="wrap_content" android:text="Open ViewStub" /><ViewStub android:id="@+id/stub1" android:inflatedId="@+id/showlayout"android:layout="@layout/layout1" android:layout_width="fill_parent"android:layout_height="wrap_content" android:layout_gravity="bottom"/></LinearLayout></merge>layout1.xml<?xml version="1.0" encoding="UTF-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="fill_parent" android:layout_height="wrap_content"><TextView android:id="@+id/label_import" android:layout_width="wrap_content"android:layout_height="wrap_content" android:text="Textview from Viewstub" /><Button android:id="@+id/button_cancel" android:layout_width="wrap_content"android:layout_height="wrap_content" android:minWidth="100dip"android:text="Next" /></LinearLayout>SampleViewStub.javapublic class SampleViewStub extends Activity {ViewStub stub;boolean click = true;/** Called when the activity is first created. */@Overridepublic void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.main);findViewById(R.id.openstub).setOnClickListener(new OnClickListener() {public void onClick(View v) {if (click) {stub = (ViewStub) findViewById(R.id.stub1);stub.inflate();click = false;}}});}}