2012-03-20 65 views
44

我创建了一个引用xml项目的线性布局。在这个linearlayout里面,我动态地放置了一些textview,所以不需要从xml中取出它们。现在我需要从linearlayout中移除这些textviews。我试过这个:删除linearlayout中的所有项目

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

但它不起作用。 我该怎么办? 谢谢,马蒂亚

回答

115

为什么你写linearLayout.getParent()你应该做的这一切直接的LinearLayout

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

你好,请试试这个代码的工作对我来说

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(); 

    } 
} 
相关问题