在inflater布局中删除两个LinearLayout

时间:2012-07-17 16:31:11

标签: android parent android-linearlayout removechild layout-inflater

我有一个名为socio_form_organization.xml的xml文件,它有两个布局。这个xml文件在名为content的布局中膨胀,我们可以通过下面的代码看到它

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

但只有在我有一个布局时才有效。在这种情况下,我有两个布局,不起作用。

我不知道我是否能让你理解。有人可以帮帮我吗? 感谢

1 个答案:

答案 0 :(得分: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);