如何在父布局中多次添加布局单击其他布局



我有一个父布局,我想在单击另一个布局时在其中添加一个子布局。我想添加的布局的可见性现在已经消失了。单击"下一个目的地"布局后,会显示另一个布局,即LinearAddedDestination。我想在用户点击NextDestination布局14次后添加此布局,直到14次后NextDestiination布局不可见。

我怎样才能做到这一点??

GoBox活动布局

<?xml version="1.0" encoding="utf-8"?>

        <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="15dp"
            android:layout_marginRight="15dp">
            <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:id="@+id/textView14"
                    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: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/LinearLayoutAdditionalContactTo">
                <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/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: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: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_marginTop="10dp"
                android:layout_weight="1.00"
                android:background="@android:color/white"
                android:layout_marginLeft="15dp"
                android:layout_marginRight="15dp"
                android:id="@+id/LinearAddedDestination"
                android:visibility="gone">
                <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:id="@+id/textView14"
                        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="80dp"
                        android:id="@+id/LinearRemove">
                        <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: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/LinearLayoutAdditionalContactTo">
                    <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/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: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:id="@+id/editText_from_details"
                        android:layout_marginEnd="10dp"
                        android:hint="Instruction"
                        android:layout_gravity="center"
                        android:layout_marginLeft="10dp" />
                </LinearLayout>
            </LinearLayout>
            <LinearLayout
                android:layout_width="wrap_content"
                android:layout_height="40dp"
                android:orientation="horizontal"
                android:background="@drawable/shape"
                android:layout_gravity="right"
                android:layout_marginEnd="15dp"
                android:layout_marginTop="15dp"
                android:layout_marginBottom="15dp"
                android:id="@+id/LinearNextDestination">
                <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:id="@+id/textView47"
                    android:layout_gravity="center"
                    android:layout_marginLeft="05dp"
                    android:textSize="14sp"
                    android:layout_marginRight="15dp" />
            </LinearLayout>

        </LinearLayout>

您有许多布局作为主布局的子布局。但实现目标的更好方法是添加碎片。

使用片段,您可以轻松地更改布局视图。看看这些链接。添加碎片将解决您的问题。

http://developer.android.com/training/basics/fragments/creating.html

http://examples.javacodegeeks.com/android/core/app/fragment/android-fragments-example/

http://www.vogella.com/tutorials/AndroidFragments/article.html

http://www.learn2crack.com/2014/05/android-working-with-fragments.html

使用片段,您可以划分布局中的空间,并在其中添加多个片段。

最新更新