簡體   English   中英

刪除線性布局內的所有項目

[英]Remove all items inside linearlayout

我創建了一個引用 xml 項目的線性布局。 在這個線性布局中,我動態地放置了一些文本視圖,因此無需從 xml 中取出它們。 現在我需要從線性布局中刪除這些文本視圖。 我試過這個:

if(((LinearLayout) linearLayout.getParent()).getChildCount() > 0)
    ((LinearLayout) linearLayout.getParent()).removeAllViews();

但它不起作用。 我能怎么做? 謝謝,馬蒂亞

你為什么寫linearLayout.getParent()

您應該直接在LinearLayout上調用它:

linearLayout.removeAllViews();

嗨,請試試這個代碼對我有用

public class ShowText extends Activity {
    /** Called when the activity is first created. */
    LinearLayout linearLayout;
    TextView textView,textView1;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        textView=new TextView(this);
        textView1=new TextView(this);
        textView.setText("First TextView");
        textView1.setText("First TextView");

        linearLayout=(LinearLayout) findViewById(R.id.mn);
        linearLayout.addView(textView);
        linearLayout.addView(textView1);
        linearLayout.removeAllViews();

    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM