可使用LayoutInflator来在运行时动态地将XML布局文件加载为View对象,然后就可以使用这个View对象了。
http://greenboy1.iteye.com/blog/1068482
亮点 :
public class Main extends Activity {
/** Called when the activity is first created. */
TextView tv1, tv2;
Button b1, b2,b3;
View layout1, layout2;
boolean view2Load=false;//main2是否载入过的flag
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//LayoutInflater inflater = getLayoutInflater();
LayoutInflater inflater = LayoutInflater.from(this);
//以上两行功能一样
layout1 = inflater.inflate(R.layout.main, null);
layout2 = inflater.inflate(R.layout.main2, null);
setView1();
tv1 = (TextView) findViewById(R.id.tv1);
b1 = (Button) findViewById(R.id.b1);
b3 = (Button) findViewById(R.id.b3);
b3.setOnClickListener(l3);
b1.setOnClickListener(l1);
//控件及监听器只需一次查找绑定,切换view不影响
}
private void setView1() {
setContentView(layout1);
//切换到main
}
帐篷征婚
HxLauncher: Launch Android applications by voice commands