0

我有一個名爲socio_form_organization.xml的xml文件,它有兩個佈局。此XML文件在佈局名爲內容誇大,我們如何通過代碼見下文在inflater佈局中刪除兩個LinearLayout

socio_form_organization.xml

<LinearLayout  
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/sfo_layout" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:background="@drawable/background_tile" 
    android:orientation="vertical" > 

    <LinearLayout 
     xmlns:android="http://schemas.android.com/apk/res/android" 
     android:id="@+id/sfo_layout2" 
     android:layout_width="wrap_content" 
     android:layout_height="51dp" 
     android:background="@drawable/background_tile" 
     android:baselineAligned="true" 
     android:orientation="horizontal"> 

     <Button 
      android:id="@+id/sfo_btOrganization" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_marginBottom="2dp" 
      android:layout_marginLeft="5dp" 
      android:layout_marginRight="5dp" 
      android:layout_marginTop="2dp" 
      android:hint="@string/sfo_btOrganization" /> 

     <EditText 
      android:id="@+id/sfo_etEmpresa" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:layout_marginBottom="2dp" 
      android:layout_marginTop="2dp" 
      android:layout_weight="0.14" 
      android:hint="@string/sfo_etEmpresa" 
      android:inputType="textMultiLine" 
      android:scrollHorizontally="false" /> 


     <ImageButton 
      android:id="@+id/sfo_ivRemove" 
      style="?android:attr/buttonStyleSmall" 
      android:layout_height="60dp" 
      android:layout_width="50dp" 
      android:layout_marginTop="2dp" 
      android:layout_marginBottom="2dp" 
      android:layout_marginRight="5dp" 
      android:onClick="onClick" 
      android:background="@android:color/transparent" 
      android:src="@drawable/tb_no_delete" /> 

    </LinearLayout> 

    <EditText 
     android:id="@+id/sfo_etTitulo" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:layout_marginBottom="2dp" 
     android:layout_marginLeft="65dp" 
     android:layout_marginRight="55dp" 
     android:layout_marginTop="2dp" 
     android:layout_weight="0.14" 
     android:hint="@string/sfo_etTitulo" 
     android:inputType="textMultiLine" 
     android:scrollHorizontally="false" /> 

</LinearLayout> 

佈局,其中充氣時socio_form_organization.xml

<LinearLayout 
     xmlns:android="http://schemas.android.com/apk/res/android" 
     android:id="@+id/se_contentOrganization" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_marginBottom="2dp" 
     android:background="@drawable/background_tile" 
     android:orientation="vertical" > 

     <include 
      xmlns:android="http://schemas.android.com/apk/res/android" 
      android:layout_width="match_parent" 
      android:layout_height="70dp" 
      layout="@layout/socio_form_organization" /> 

在我的班級我做通脹

LayoutInflater inflaterOrganization= 
    (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
    LinearLayout organization =  
    (LinearLayout)inflaterOrganization.inflate(R.layout.socio_form_organization, null); 
    LinearLayout lLayoutOrganization; 
    lLayoutOrganization = (LinearLayout)findViewById(R.id.se_contentOrganization); 
    lLayoutOrganization.addView(organization); 

但現在需要刪除內容中的兩個佈局,當我點擊ImageButton,但我不能。

我嘗試用

View toRemove = (View) view.getParent(); 
    ViewGroup vg = (ViewGroup)findViewById(R.id.se_content); 
    vg.removeView(toRemove); 

但只有當我有一個佈局工作。在這種情況下,我有兩個佈局,不工作。

我不知道我是否能讓你明白。任何人都可以幫助我? 感謝

+0

您是否收到任何錯誤?如果是這樣,請發佈logcats。 – Sam 2012-07-17 16:36:58

+0

Hy Sam.I沒有錯誤。只要當我點擊圖像按鈕刪除內容膨脹沒有任何反應,內容不會被刪除。 – 2012-07-17 16:43:01

回答

0

你引用的是不要在佈局存在兩個ID您提供:

lLayoutOrganization = (LinearLayout)findViewById(R.id.se_contentOrganization); 

哪裏se_contentOrganization?你的意思是se_contentEmail

當你嘗試刪除的看法:

ViewGroup vg = (ViewGroup)findViewById(R.id.se_content); 

哪裏se_content?還是你的意思是se_contentEmail?或者sfo_layout

加成

試試這個:

LinearLayout parent = (LinearLayout) findViewById(R.id.se_contentOrganization); 
LinearLayout child = (LinearLayout) findViewById(R.id.sfo_layout); 
parent.removeView(child); 

如果您正在使用socio_form_organization.xml多次再嘗試:

LinearLayout child = (LinearLayout) view.getParent().getParent(); 
LinearLayout parent = (LinearLayout) child.getParent(); 
parent.removeView(child); 
+0

對不起山姆的錯誤。已升級。不是se_contentEmail,而是se_contentOrganization。 – 2012-07-17 16:46:33

+0

好的,你想要移除哪個LinearLayout? 'se_contentOranization'? – Sam 2012-07-17 16:47:41

+0

我需要刪除thw兩個linearLayouts。 android:id =「@ + id/sfo_layout2」和android:id =「@ + id/sfo_layout2」。當我嘗試點擊圖像按鈕時View toRemove =(View)view.getParent(); ViewGroup vg =(ViewGroup)findViewById(R.id.se_contentOrganization); vg.removeView(toRemove);不工作 – 2012-07-17 16:49:07