一个Activity中使用两个layout实例
package com.sbs.aas2l; import android.app.Activity; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.widget.AdapterView; import android.widget.ArrayAdapter; import android.widget.Button; import android.widget.ListView; import android.widget.TextView; import android.widget.AdapterView.OnItemClickListener; public class aas2l extends Activity implements OnClickListener { /** Called when the activity is first created. */ private String[] data= {"Item-1", "Item-2", "Item-3"}; private Button btn, btn2; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); //setContentView(R.layout.main); set_aas2l_layout(); } public void set_aas2l_layout() { setContentView(R.layout.aas2l); btn = (Button)findViewById(R.id.pu_btn); btn.setOnClickListener(this); btn2 = (Button)findViewById(R.id.exit_btn); btn2.setOnClickListener(this); } public void onClick(View v){ if (v == btn) this.set_pu_layout(); if(v == btn2) this.finish(); } public void set_pu_layout(){ setContentView(R.layout.pickup); ListView lv = (ListView)findViewById(R.id.list); ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, data); lv.setAdapter(arrayAdapter); lv.setOnItemClickListener(listener); } OnItemClickListener listener= new OnItemClickListener(){ public void onItemClick(AdapterView arg0, View arg1, int arg2, long arg3){ set_aas2l_layout(); TextView tv = (TextView)findViewById(R.id.tv); tv.setText("select: " + data[arg2]); } }; }
step3。编写ass2l.xml (当然也可以在main.xml中写,这次我新建一个。) <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="wrap_content"> <TextView android:id= "@+id/tv" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginRight="3dip" android:text="@string/dialog" /> <Button android:id= "@+id/pu_btn" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginRight="3dip" android:text="@string/pickup" /> <Button android:id= "@+id/exit_btn" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginRight="3dip" android:text="@string/exit" /> </LinearLayout> step4。编写pickup.xml(点击select按钮后显示的布局,是一个listview。) <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 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="Hello World, aas2l" /> <ListView android:id="@+id/list" android:layout_width="wrap_content" android:layout_height="wrap_content" /> </LinearLayout> step5。修改strings.xml <?xml version="1.0" encoding="utf-8"?> <resources> <string name="hello">Hello World, aas2l!</string> <string name="app_name">adroid activity surport 2 layouts</string> <string name="pickup">select please</string> <string name="exit">Exit</string> <string name="dialog">dialog</string> </resources> step6。运行。 如图: 初始界面 点击select后 选择后 end 。。。。
step3。编写ass2l.xml (当然也可以在main.xml中写,这次我新建一个。) <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="wrap_content"> <TextView android:id= "@+id/tv" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginRight="3dip" android:text="@string/dialog" /> <Button android:id= "@+id/pu_btn" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginRight="3dip" android:text="@string/pickup" /> <Button android:id= "@+id/exit_btn" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginRight="3dip" android:text="@string/exit" /> </LinearLayout> step4。编写pickup.xml(点击select按钮后显示的布局,是一个listview。) <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 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="Hello World, aas2l" /> <ListView android:id="@+id/list" android:layout_width="wrap_content" android:layout_height="wrap_content" /> </LinearLayout> step5。修改strings.xml <?xml version="1.0" encoding="utf-8"?> <resources> <string name="hello">Hello World, aas2l!</string> <string name="app_name">adroid activity surport 2 layouts</string> <string name="pickup">select please</string> <string name="exit">Exit</string> <string name="dialog">dialog</string> </resources> step6。运行。 如图: 初始界面 点击select后 选择后 end 。。。。