可以使用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]