简体   繁体   中英

Remove two LinearLayout's inside a inflater layout

I have a xml file named socio_form_organization.xml that have two layout's. This xml file is inflated in a layout named content how we can see by the code below

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>

Layout where is inflated the 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" />

In my class i do the inflation

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

But now á need to remove the two layout inside the content when i click in ImageButton but i can't.

I try with

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

But only works when I have one layout. In this case I have two layout and don't work.

I do not know if I could make you understand. Anyone can help me? Thanks

You are referencing two ids that do not exist in the layouts you provided:

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

Where is se_contentOrganization ? Did you mean se_contentEmail ?

And when you try to remove the views:

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

Where is se_content ? Or did you mean se_contentEmail ? Or sfo_layout ?

Addition

Try this:

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

If you are using socio_form_organization.xml multiple times then try:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM