2010-10-27 46 views
4

我是Android的新手。任何人都可以爲我的問題提供一些想法。如何從Android的佈局中刪除子佈局?

/* Parent Linear Layout */ 

    final LinearLayout par_layout=new LinearLayout(this); 
    par_layout.setOrientation(LinearLayout.VERTICAL); 

    /* Child Linear Layout */ 
    final LinearLayout chl_layout=new LinearLayout(this); 
    chl_layout.setOrientation(LinearLayout.VERTICAL); 

    TextView tv_name=new TextView(this); 
    tv_name.setText("Name "); 

    TextView tv_item=new TextView(this); 
    tv_item.setText("Items "); 

    Button btn_submit=new Button(this); 
    btn_submit.setText("Submit"); 
    btn_submit.setOnClickListener(new OnClickListener() {   
     @Override 
     public void onClick(View v) { 
      // TODO Auto-generated method stub 
      par_layout.removeAllViewsInLayout();     
     } 
    }); 

    chl_layout.addView(tv_name); 
    chl_layout.addView(tv_item); 
    chl_layout.addView(btn_submit);   
    par_layout.addView(chl_layout); 
    setContentView(par_layout); 

在按鈕點擊時上面的代碼中,我希望清除par_layout中的chl_layout。但是我不能。任何人都可以提出一些想法

注: 下面的代碼也不能正常工作下面的代碼

par_layout.removeView(chl_layout); 

回答

2

用於刪除從父視圖子視圖。

par_layout.removeView(chl_layout); 
+0

我已經使用這個,但它不會刪除chl_layout – Alex 2010-10-27 06:21:00

+0

@Alex - 那麼爲什麼你接受這個? – katzenhut 2014-04-09 08:34:42

-1

我無法使用removeView(View view)從其父級(TableLayout)中刪除子視圖(TableRow)。 但addView正在工作。 奇怪...

+0

這不是問題的答案 – 2013-01-03 01:42:41

0

嘗試使用這樣的:

fatherLayout.removeViewInLayout(childLayout); 
2

,使整個佈局空 有一個void函數...

的LinearLayout李=新的LinearLayout(本);

li.removeAllViews();