0

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

social_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>

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

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

我不知道我是否能让你明白。任何人都可以帮助我吗?谢谢

4

1 回答 1

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

如果您多次使用 social_form_organization.xml,请尝试:

LinearLayout child = (LinearLayout) view.getParent().getParent(); 
LinearLayout parent = (LinearLayout) child.getParent(); 
parent.removeView(child);
于 2012-07-17T16:41:31.900 回答