1 回答

TA貢獻1840條經驗 獲得超5個贊
可以的。有2中方式:
1、動態添加的時候為組件設置id,刪除的時候根據id查找到對應組件,然后刪除
2、根據父節點,獲取所有父組件下的子組件,然后依次刪除。
示例:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | 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() {//動態刪除組件(按鈕) //獲取linearlayout子view的個數 int count = linearLayout.getChildCount(); //研究整個LAYOUT布局,第0位的是含add和remove兩個button的layout //第count-1個是那個文字被置中的textview //因此,在remove的時候,只能操作的是0<location<count-1這個范圍的 //在執行每次remove時,我們從count-2的位置即textview上面的那個控件開始刪除~ if (count - 2 > 0) { //count-2>0用來判斷當前linearlayout子view數多于2個,即還有我們點add增加的button linearLayout.removeViewAt(count - 2); } } |
- 1 回答
- 0 關注
- 563 瀏覽
添加回答
舉報