xml文件
RelativeLayout_A.xml
代码
RelativeLayout_A.xml
........
........
........
........
RelativeLayout_A1.xml 去除C的布局
RelativeLayout_C1.xml C1的布局
RelativeLayout_C2.xml C2的布局
其中RelativeLayout C的布局会变化,但是其它布局不变
我想在Java中动态的添加会变的C,比如添加C1,删除C1,添加C2等等
怎么实现啊,以前都是xml布局,不会java布局
详细一点,先谢谢大家了
可以使用ViewStub
大约参考代码如下:
main.xml
[code="xml"]<?xml version="1.0" encoding="utf-8"?>
android:layout_width="fill_parent" android:layout_height="fill_parent">
android:layout_height="wrap_content" android:text="Showing ViewStub" />
android:layout_height="wrap_content" android:text="Open ViewStub" />
android:layout="@layout/layout1" android:layout_width="fill_parent"
android:layout_height="wrap_content" android:layout_gravity="bottom"/>
[/code]
layout1.xml
[code="xml"]<?xml version="1.0" encoding="UTF-8"?>
android:layout_width="fill_parent" android:layout_height="wrap_content">
android:layout_height="wrap_content" android:text="Textview from Viewstub" />
android:layout_height="wrap_content" android:minWidth="100dip"
android:text="Next" />
[/code]
SampleViewStub.java
[code="java"]public class SampleViewStub extends Activity {
ViewStub stub;
boolean click = true;
/** Called when the activity is first created. */
@Override
public 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;
}
}
});
}
}[/code]