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