简体   繁体   English

删除线性布局内的所有项目

[英]Remove all items inside linearlayout

I create a linearlayout that refes to an xml item.我创建了一个引用 xml 项目的线性布局。 Inside this linearlayout i put some textview dynamically, so without taking them from the xml.在这个线性布局中,我动态地放置了一些文本视图,因此无需从 xml 中取出它们。 Now i need to remove these textviews from the linearlayout.现在我需要从线性布局中删除这些文本视图。 I tried this:我试过这个:

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

but it doesn't work.但它不起作用。 How can i do?我能怎么做? Thanks, Mattia谢谢,马蒂亚

Why you wrote linearLayout.getParent() ?你为什么写linearLayout.getParent()

You should call this directly on LinearLayout :您应该直接在LinearLayout上调用它:

linearLayout.removeAllViews();

Hi Please try this code its working for me嗨,请试试这个代码对我有用

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