Android 移除线性布局内的所有项目

Android 移除线性布局内的所有项目,android,android-linearlayout,Android,Android Linearlayout,我创建了一个引用xml项的linearlayout。在这个线性布局中,我动态地放置了一些textview,因此不需要从xml中获取它们。现在我需要从linearlayout中删除这些文本视图。我试过这个: if(((LinearLayout) linearLayout.getParent()).getChildCount() > 0) ((LinearLayout) linearLayout.getParent()).removeAllViews(); 但它不起作用。 我该怎么办

我创建了一个引用xml项的linearlayout。在这个线性布局中,我动态地放置了一些textview,因此不需要从xml中获取它们。现在我需要从linearlayout中删除这些文本视图。我试过这个:

if(((LinearLayout) linearLayout.getParent()).getChildCount() > 0)
    ((LinearLayout) linearLayout.getParent()).removeAllViews();
但它不起作用。 我该怎么办?
谢谢,Mattia

为什么要编写
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();

    }
}

无需添加计数检查,因为
removeAllViews()
已在内部执行此操作。从
removeallview()
源代码
if(计数