You can see the result when you run it ~ I wrote you a note when I opened the key ~
Layout file layout content:
-
& lt? Xml version =" 1.0 "encoding ="utf-8"? & gt
& ltlinear layout xmlns:Android = "/apk/RES/Android "
Android:orientation = " vertical " Android:layout _ width = " fill _ parent "
Android:layout _ height = " fill _ parent " Android:id = " @+id/linear layout " & gt;
& ltlinear layout Android:id = " @+id/linear layout 0 1 "
Android:layout _ width = " wrap _ content " Android:layout _ height = " wrap _ content " & gt;
& lt button Android: layout _ height = "wrap _ content" Android: id = "@+id/add"
Android:text = " Add " Android:layout _ width = " 100 px " & gt; & lt/Button & gt;
& lt button Android: layout _ height = "wrap _ content"
Android:layout _ width = " 100 px " Android:text = " Remove " Android:id = " @+id/Remove " & gt; & lt/Button & gt;
& lt/linear layout & gt;
& lt TextView Android: id = "@+id/textview01"Android: text = "This is textview."
Android:layout _ width = " fill _ parent " Android:gravity = " center "
Android:layout _ height = " 50px " & gt; & lt/TextView & gt;
& lt/linear layout & gt;
-
Content corresponding to the activity:
-
Package com. foxconn.dialog;
Import android.app.activity;
Import android.os.bundle;
Import android.view.view;
Import Android.view.view.onclicklistener;
Import android.view.viewgroup.layoutparams;
Import android.widget.button;
Import android.widget.linearlayout;
The public class DialogTest extends the Activity to realize OnClickListener {
Private buttons add_btn, remove _ btn
private linear layout linear layout;
private int index = 0;
/* * Called when an activity is first created. */
@ Overlay
public void on create(Bundle saved instancestate){
super . oncreate(savedInstanceState);
setContentView(r . layout . main);
findViews();
Register ();
}
Private void register () {
add _ BTN . setonclicklistener(this);
remove _ BTN . setonclicklistener(this);
}
Private void findViews() {
add _ BTN =(Button)findViewById(r . id . add);
remove _ BTN =(Button)findViewById(r . id . remove);
linear layout =(linear layout)findViewById(r . id . linear layout);
}
Protected view createView() {
Button btn = new button (this);
BTN . setid(index++);
Btn.setLayoutParams (new LayoutParams(LayoutParams). WRAP_CONTENT,LayoutParams。 WRAP _ CONTENT));
BTN . settext(" aaaaaa "+index);
Return btn
}
Private void removeView() {
//get the number of linearlayout subviews.
int count = linear layout . get child count();
//Studying the whole layout, the layout with the Add and Delete buttons is at the 0 th position.
//count- 1 is a textview centered on the text.
//Therefore, when removing, only 0
//Every removal starts from the position of count-2, that is, the control above textview ~
if(count-2 & gt; 0) {
//count-2 & gt; 0 is used to judge that there are more than two subviews in the current linearlayout, that is, there are buttons added by clicking add.
linear layout . remove viewat(count-2);
}
}
Public void onClick (view v)
switch (v.getId()) {
Add case number:
linear layout . add view(create view(), 1);
Break;
Delete case number:
remove view();
Break;
Default value:
Break;
}
}
}
-