如果單純要切換layout,利用setContentView就可以做到啦
import android.app.Activity; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; public class test extends Activity { Button connect = null; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); //設定layout setLayout(1); } public void setLayout(int layout_num){ //layout1 if(layout_num == 1) { setContentView(R.layout.layout1); connect = (Button) findViewById( R.id.connect); //點擊按鈕後更換layout connect.setOnClickListener(new OnClickListener(){ public void onClick(View v){ setLayout(2); } }); } //layout2 else{ setContentView(R.layout.layout2); } } }