public class TestLayout extends Activity {
/** Called when the activity is first created. */
//Element el = new Element(this);
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.testlayout);
Button btn2 = (Button)findViewById(R.id.btnChange);
btn2.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
changeToLayout2();
}
});
}
public void changeToLayout2(){
setContentView(R.layout.testlayout2);
Button btn3 = (Button)findViewById(R.id.btnChange2);
btn3.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// 点击的时候重新设置layout
changeToLaytout1();
}
});
}
public void changeToLaytout1(){
setContentView(R.layout.testlayout2);
Button btn3 = (Button)findViewById(R.id.btnChange2);
btn3.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
setContentView(R.layout.testlayout);
}
});
}
}我就想点击一个按钮切换layout,结果点击按钮,到了setContentView的时候就报错了. 可是我看书上就这么写的啊, testlayout.xml<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView
android:id="@+id/layoutTextView"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
<ImageView
android:id="@+id/imgTest"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/a"
android:layout_alignParentRight="true"
/>
<Button android:id="@+id/btnImg"
android:text="keshi"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<Button android:id="@+id/btnChange"
android:text="change"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
/>
</RelativeLayout>testlayout2.xml
[code=XML]
<?xml version="1.0" encoding="utf-8"?>
<AbsoluteLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<Button android:id="@+id/btnChange2"
android:text="changeToLayout1"
android:layout_x="5px"
android:layout_y="5px"/>
</AbsoluteLayout>
[/code]
这种方法对LinearLayout也适用吗?