import android.app.Activity; import android.os.Bundle; import android.os.Handler; import android.os.Message; import android.util.Log; import android.view.LayoutInflater; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.LinearLayout; public class PadTestActivity extends Activity { View chacheView; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); // setContentView(R.layout.main); LayoutInflater inflater = LayoutInflater.from(this); final View mainView = inflater.inflate(R.layout.main, null); // Add first page LinearLayout myFirst = (LinearLayout) inflater.inflate(R.layout.first, null).findViewById(R.id.myFirst); LinearLayout layoutFirst = (LinearLayout) mainView.findViewById(R.id.LayoutFirst); layoutFirst.removeAllViews(); layoutFirst.addView(myFirst); // Show the page first // Add second page LinearLayout mySecond = (LinearLayout) inflater.inflate(R.layout.second, null).findViewById(R.id.mySecond); LinearLayout layoutSecond = (LinearLayout) mainView.findViewById(R.id.LayoutSecond); layoutSecond.removeAllViews(); layoutSecond.addView(mySecond); // Add third page LinearLayout myThird = (LinearLayout) inflater.inflate(R.layout.third, null).findViewById(R.id.myThird); LinearLayout layoutThird = (LinearLayout) mainView.findViewById(R.id.LayoutThird); layoutThird.removeAllViews(); layoutThird.addView(myThird); setContentView(mainView); chacheView = myFirst; Button btnDelete = (Button) mainView.findViewById(R.id.btnDelete); btnDelete.setOnClickListener(new OnClickListener() { public void onClick(View v) { LinearLayout layoutFirst = (LinearLayout) mainView.findViewById(R.id.LayoutFirst); layoutFirst.removeAllViews(); } }); Button btnAdd = (Button) mainView.findViewById(R.id.btnAdd); btnAdd.setOnClickListener(new OnClickListener() { public void onClick(View v) { LinearLayout layoutFirst = (LinearLayout) mainView.findViewById(R.id.LayoutFirst); if (chacheView != null) { layoutFirst.addView(chacheView); } } }); } }
欢迎关注公众号"Devin说",会不定期更新技术知识。