删除后无法对布局进行充气

时间:2015-12-19 10:48:02

标签: java android layout android-inflate

我想多次展开布局,每次点击删除布局时都应删除它。现在我可以添加和删除布局,但在删除布局后无法再次添加。如果我不删除并添加其他布局,则会在删除布局后添加但不添加。 并且无法删除单个布局。如果我给两个布局充气,那么它们都会被移除。怎么做单身?

destinationParent.removeView(目的地);

我试过这个但是不行。所以我用过这个:

parent =(ViewGroup)newDestination.getParent();                        parent.removeView(newDestination);

但是,所有已添加的布局都被删除而不是单个布局。

为什么会这样?

这是我的代码:

  nextDestination.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
           // removeDestination.setVisibility(View.VISIBLE);

            newDestination = getLayoutInflater().inflate(R.layout.next_destination_layout, addDestination, true);

            LinearLayout remove = (LinearLayout) newDestination.findViewById(R.id.remove);
            remove.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {

                   // addDestination.removeView(newDestination);

                    parent = (ViewGroup)newDestination.getParent();
                   parent.removeView(newDestination);
                }
            });
        }
    });

    removeDestination.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

           // destinationParent.removeView(destination);

          //  parent = (ViewGroup)destination.getParent();
           // parent.removeView(destination);

            }
    });

}

布局文件

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical"
            android:background="@android:color/white"
            android:layout_marginLeft="10dp"
            android:layout_marginTop="05dp"
            android:layout_marginRight="10dp"
            android:id="@+id/parent">

            <TextView
                android:layout_width="wrap_content"

                android:layout_height="wrap_content"
                android:textAppearance="?android:attr/textAppearanceMedium"
                android:text="@string/DriversNear"
                android:id="@+id/textView10"
                android:layout_marginStart="20dp"
                android:layout_marginTop="10dp" />


            <fragment android:layout_width="match_parent"
                android:layout_height="200dp"
                android:id="@+id/map"
                tools:context=".GoBoxPickupActivity"
                android:name="com.google.android.gms.maps.MapFragment"
                android:layout_marginTop="10dp" />
        </LinearLayout>
    <LinearLayout
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="05dp"
        android:layout_weight="1.00"
        android:background="@android:color/white"
        android:layout_marginLeft="10dp"
        android:layout_marginRight="10dp">

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textAppearance="?android:attr/textAppearanceMedium"
                android:text="ORIGIN"
                android:id="@+id/textView14"
                android:layout_marginStart="20dp"
                android:layout_marginTop="20dp" />

        <ImageView
            android:layout_width="match_parent"
            android:layout_height="08dp"
            android:id="@+id/imageView14"
            android:background="@drawable/line2"/>
        <LinearLayout
            android:orientation="horizontal"
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

            <ImageView
                android:layout_width="25dp"
                android:layout_height="25dp"
                android:id="@+id/imageView10"
                android:layout_marginStart="20dp"
                android:background="@drawable/ic_place_black_48dp"
                android:layout_marginTop="05dp"
                android:layout_gravity="center" />

            <EditText
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:id="@+id/Text_to"
                android:drawableEnd="@drawable/ic_chevron_right_black_24dp"
                android:layout_marginEnd="10dp"
                android:text="@string/Loc"
                android:layout_gravity="center"
                android:layout_marginTop="05dp"
                android:textAppearance="@android:style/TextAppearance.Medium"
                android:cursorVisible="false"/>

        </LinearLayout>
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:hint="Street/Building/Place"
            android:layout_marginStart="48dp"/>
        <LinearLayout
            android:orientation="horizontal"
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

            <ImageView
                android:layout_width="25dp"
                android:layout_height="25dp"
                android:id="@+id/imageView17"
                android:layout_marginStart="20dp"
                android:background="@drawable/ic_description_black_48dp"
                android:layout_marginTop="10dp" />

            <EditText
                android:layout_width="match_parent"
                android:layout_height="45dp"
                android:id="@+id/editText_to_details"
                android:layout_marginEnd="10dp"
                android:hint="@string/LocationDetails"/>

        </LinearLayout>

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textAppearance="?android:attr/textAppearanceSmall"
            android:id="@+id/textViewHouseDetailsTo"
            android:layout_marginStart="48dp"
            android:hint="@string/HouseDetails" />

        <LinearLayout
            android:orientation="horizontal"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="center_horizontal"
            android:id="@+id/LinearLayoutAdditionalContactFrom">

            <ImageView
                android:layout_width="25dp"
                android:layout_height="25dp"
                android:id="@+id/imageView18"
                android:layout_marginStart="20dp"
                android:background="@drawable/ic_person_black_48dp"
                android:layout_marginTop="05dp" />

            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:id="@+id/contactDetailsFrom"
                android:layout_marginEnd="10dp"
                android:text="@string/additionalContact"
                android:textAppearance="?android:attr/textAppearanceMedium"
                android:drawableEnd="@drawable/ic_expand_more_black_24dp"
                android:layout_marginTop="08dp"
                android:layout_marginStart="05dp" />
        </LinearLayout>
        <LinearLayout
            android:orientation="vertical"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginStart="60dp"
            android:layout_marginEnd="50dp"
            android:layout_gravity="center"
            android:visibility="gone"
            android:id="@+id/LinearLayoutFrom"
            android:layout_marginTop="20dp">

            <EditText
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:id="@+id/editTex"
                android:layout_weight="1"
                android:hint="Name"
                android:layout_gravity="center"/>

            <EditText
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:id="@+id/editTe"
                android:layout_weight="1"
                android:hint="Phone"
                android:layout_gravity="center"
                />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textAppearance="?android:attr/textAppearanceSmall"
                android:text="@string/or"
                android:id="@+id/textViewInputIfSender"
                android:layout_gravity="center"
                android:layout_marginTop="20dp" />

            <LinearLayout
                android:layout_width="220dp"
                android:layout_height="40dp"
                android:background="@drawable/shape"
                android:layout_gravity="center_horizontal">


                <ImageButton
                    android:layout_width="20dp"
                    android:layout_height="20dp"
                    android:id="@+id/imageButton1"
                    android:background="@drawable/phone2"
                    android:layout_gravity="center"
                    android:layout_marginStart="30dp" />

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:textAppearance="?android:attr/textAppearanceSmall"
                    android:text="@string/addFromContacts"
                    android:id="@+id/textView35"
                    android:layout_gravity="center"
                    android:layout_marginStart="20dp" />
            </LinearLayout>

        </LinearLayout>
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textAppearance="?android:attr/textAppearanceSmall"
            android:id="@+id/textViewInputReceiver"
            android:layout_marginLeft="48dp"
            android:hint="@string/contact1"
            android:layout_marginTop="10dp" />
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal"
            android:focusable="true"
            android:focusableInTouchMode="true">

            <ImageView
                android:layout_width="25dp"
                android:layout_height="25dp"
                android:id="@+id/imageView12"
                android:layout_marginStart="20dp"
                android:background="@drawable/ic_description_black_48dp"
                android:layout_marginTop="10dp" />

            <EditText
                android:layout_width="match_parent"
                android:layout_height="45dp"
                android:layout_marginEnd="10dp"
                android:hint="Instruction"
                android:layout_gravity="center"
                android:layout_marginLeft="10dp" />

        </LinearLayout>

        <LinearLayout
            android:orientation="vertical"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="10dp"
            android:layout_weight="1.00"
            android:background="@android:color/white"
            android:layout_marginLeft="10dp"
            android:layout_marginRight="10dp"
            android:id="@+id/destination">
            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="horizontal">
                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:textAppearance="?android:attr/textAppearanceMedium"
                    android:text="DESTINATION"
                    android:layout_marginStart="20dp"
                    android:layout_marginTop="20dp" />
                <LinearLayout
                    android:layout_width="wrap_content"
                    android:layout_height="40dp"
                    android:orientation="horizontal"
                    android:background="@drawable/shape"
                    android:gravity="right"
                    android:layout_gravity="center|right"
                    android:layout_marginTop="10dp"
                    android:layout_marginBottom="10dp"
                    android:layout_marginLeft="100dp"
                    android:id="@+id/LinearRemove"
                    android:visibility="gone">
                    <ImageView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:background="@drawable/ic_clear_black_18dp"
                        android:layout_marginLeft="05dp"
                        android:layout_marginTop="05dp" />

                    <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:textAppearance="?android:attr/textAppearanceSmall"
                        android:text="REMOVE"
                        android:id="@+id/textView47"
                        android:layout_gravity="center"
                        android:layout_marginLeft="05dp"
                        android:textSize="14sp"
                        android:layout_marginRight="15dp" />
                </LinearLayout>
            </LinearLayout>


            <ImageView
                android:layout_width="match_parent"
                android:layout_height="08dp"
                android:background="@drawable/line2"/>
            <LinearLayout
                android:orientation="horizontal"
                android:layout_width="match_parent"
                android:layout_height="wrap_content">

                <ImageView
                    android:layout_width="25dp"
                    android:layout_height="25dp"
                    android:layout_marginStart="20dp"
                    android:background="@drawable/ic_place_black_48dp"
                    android:layout_marginTop="05dp"
                    android:layout_gravity="center" />

                <EditText
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:drawableEnd="@drawable/ic_chevron_right_black_24dp"
                    android:layout_marginEnd="10dp"
                    android:text="@string/Loc"
                    android:layout_gravity="center"
                    android:layout_marginTop="05dp"
                    android:textAppearance="@android:style/TextAppearance.Medium"
                    android:cursorVisible="false"/>

            </LinearLayout>
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:hint="Street/Building/Place"
                android:layout_marginStart="48dp"/>
            <LinearLayout
                android:orientation="horizontal"
                android:layout_width="match_parent"
                android:layout_height="wrap_content">

                <ImageView
                    android:layout_width="25dp"
                    android:layout_height="25dp"
                    android:layout_marginStart="20dp"
                    android:background="@drawable/ic_description_black_48dp"
                    android:layout_marginTop="10dp" />

                <EditText
                    android:layout_width="match_parent"
                    android:layout_height="45dp"
                    android:layout_marginEnd="10dp"
                    android:hint="@string/LocationDetails"/>

            </LinearLayout>

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textAppearance="?android:attr/textAppearanceSmall"
                android:layout_marginStart="48dp"
                android:hint="@string/HouseDetails" />

            <LinearLayout
                android:orientation="horizontal"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_gravity="center_horizontal"
                android:id="@+id/LinearLayoutAdditionalContactTo">

                <ImageView
                    android:layout_width="25dp"
                    android:layout_height="25dp"
                    android:layout_marginStart="20dp"
                    android:background="@drawable/ic_person_black_48dp"
                    android:layout_marginTop="05dp" />

                <TextView
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:id="@+id/contactDetailsTo"
                    android:layout_marginEnd="10dp"
                    android:text="@string/additionalContact"
                    android:textAppearance="?android:attr/textAppearanceMedium"
                    android:drawableEnd="@drawable/ic_expand_more_black_24dp"
                    android:layout_marginTop="08dp"
                    android:layout_marginStart="05dp" />
            </LinearLayout>
            <LinearLayout
                android:orientation="vertical"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginStart="60dp"
                android:layout_marginEnd="50dp"
                android:layout_gravity="center"
                android:visibility="gone"
                android:id="@+id/LinearLayoutTo"
                android:layout_marginTop="20dp">

                <EditText
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"
                    android:hint="Name"
                    android:layout_gravity="center"/>

                <EditText
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"
                    android:hint="Phone"
                    android:layout_gravity="center"
                    />

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:textAppearance="?android:attr/textAppearanceSmall"
                    android:text="@string/or"
                    android:layout_gravity="center"
                    android:layout_marginTop="20dp" />

                <LinearLayout
                    android:layout_width="220dp"
                    android:layout_height="40dp"
                    android:background="@drawable/shape"
                    android:layout_gravity="center_horizontal">


                    <ImageButton
                        android:layout_width="20dp"
                        android:layout_height="20dp"
                        android:background="@drawable/phone2"
                        android:layout_gravity="center"
                        android:layout_marginStart="30dp" />

                    <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:textAppearance="?android:attr/textAppearanceSmall"
                        android:text="@string/addFromContacts"
                        android:layout_gravity="center"
                        android:layout_marginStart="20dp" />
                </LinearLayout>

            </LinearLayout>
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textAppearance="?android:attr/textAppearanceSmall"
                android:layout_marginLeft="48dp"
                android:hint="@string/contact1"
                android:layout_marginTop="10dp" />
            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="horizontal"
                android:focusable="true"
                android:focusableInTouchMode="true">

                <ImageView
                    android:layout_width="25dp"
                    android:layout_height="25dp"
                    android:layout_marginStart="20dp"
                    android:background="@drawable/ic_description_black_48dp"
                    android:layout_marginTop="10dp" />

                <EditText
                    android:layout_width="match_parent"
                    android:layout_height="45dp"
                    android:id="@+id/editText_from_details"
                    android:layout_marginEnd="10dp"
                    android:hint="Instruction"
                    android:layout_gravity="center"
                    android:layout_marginLeft="10dp" />

            </LinearLayout>

    <LinearLayout
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1.00"
        android:id="@+id/addeddestination"
        android:layout_marginStart="10dp"
        android:layout_marginEnd="10dp">

        </LinearLayout>
        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@android:color/white"
            android:layout_marginStart="10dp"
            android:layout_marginEnd="10dp">
            <LinearLayout
                android:layout_width="wrap_content"
                android:layout_height="40dp"
                android:orientation="horizontal"
                android:background="@drawable/shape"
                android:id="@+id/LinearNextDestination"
                android:layout_gravity="center_vertical"
                android:layout_alignParentTop="true"
                android:layout_alignParentEnd="true"
                android:layout_marginEnd="20dp"
                android:layout_marginTop="10dp"
                android:layout_marginBottom="10dp">
                <ImageView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:background="@drawable/ic_add_black_18dp"
                    android:layout_marginLeft="05dp"
                    android:layout_marginTop="05dp" />

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:textAppearance="?android:attr/textAppearanceSmall"
                    android:text="NEXT DESTINATION"
                    android:layout_gravity="center"
                    android:layout_marginLeft="05dp"
                    android:textSize="14sp"
                    android:layout_marginRight="15dp" />

            </LinearLayout>

        </RelativeLayout>

添加内部添加目的地布局。

0 个答案:

没有答案