3 回答

TA貢獻1784條經驗 獲得超2個贊
您removeViewAt與索引一起使用
final LinearLayout mLayout=findViewById(R.id.linearLayout)
mLayout.removeViewAt(mLayout.getChildCount()-1); // get the last view
//or using index counter variable
//mLayout.removeViewAt(ArraySize-1); // adjust the value accordingly
或者您可以使用removeView獲取視圖并刪除它
final LinearLayout mLayout=findViewById(R.id.linearLayout)
View v = mLayout.getChildAt(mLayout.getChildCount()-1);
mLayout.removeView(v);

TA貢獻1856條經驗 獲得超17個贊
您可以在此路徑 res/values/ids.xml 中創建一個 XML 文件;
<?xml version="1.0" encoding="utf-8"?>
<resources>
<item name="view_1" type="id"/>
</resources>
然后在 Java 中添加 id,就像這樣
textView.setId(R.id.view_1);
當你想刪除它時
LinearLayout mLayout=findViewById(R.id.linearLayout);//to find in this specific view
TextView textView = (TextView)mLayout.findViewById(R.id.view_1);
mLayout.removeView(textView);
添加回答
舉報