2016-04-06 36 views
1

Ich habe ein Fragment, das eine Schaltfläche hat, die beim Klicken ein relatives Layout unter anderen hinzufügen müssen. So oft die Schaltfläche angeklickt wird, muss das gleiche Layout unterhalb der vorherigen hinzugefügt werden.Android-fügen Sie das gleiche relative Layout unter dem anderen programmgesteuert auf Knopfdruck

In hotel_search.xml, wenn die Schaltfläche mit id(@+id/addroom) wird das Layout im hotel_room_fragment geklickt muss unterhalb der RelativeLayout mit id @+id/roomlayout und wieder aufgenommen werden, wenn sie das gleiche Layout in der hotel_room_fragment geklickt wird, muss aufgenommen werden unter dem zuvor zugegeben.

Bitte zeigen Sie mir auch, wie Sie das Layout entfernen, wenn Sie auf eine Schließen-Schaltfläche klicken.

hotel_search.xml:

<RelativeLayout 
        android:id="@+id/roomlayout" 
        android:layout_width="match_parent" 
        android:layout_height="wrap_content" 
        android:background="@android:color/transparent"> 


        <TextView 
         android:id="@+id/rooms" 
         android:layout_width="wrap_content" 
         android:layout_height="wrap_content" /> 

        <TextView 
         android:id="@+id/adults" 
         android:layout_width="wrap_content" 
         android:layout_height="wrap_content" 
         android:layout_below="@+id/rooms" 

         android:layout_toEndOf="@+id/button3" 
         android:layout_toRightOf="@+id/button3" 
         android:text="Adults" 
         android:textColor="#BDBDBD" /> 

        <Button 
         android:id="@+id/button3" 
         android:layout_width="30dp" 
         android:layout_height="30dp" 
         android:layout_below="@+id/adults" 
         android:layout_marginLeft="18dp" 
         android:layout_marginStart="18dp" 
         android:layout_marginTop="10dp" 
         android:layout_toEndOf="@+id/rooms" 
         android:layout_toRightOf="@+id/rooms" 
         android:background="#90006064" 
         android:text="-" 
         android:textColor="#fff" 
         android:textSize="22sp" /> 

        <Button 
         android:id="@+id/button4" 
         android:layout_width="30dp" 
         android:layout_height="30dp" 
         android:layout_alignLeft="@+id/adults" 
         android:layout_alignStart="@+id/adults" 
         android:layout_alignTop="@+id/button3" 
         android:layout_marginLeft="37dp" 

         android:layout_marginStart="37dp" 
         android:background="#90006064" 
         android:text="+" 
         android:textColor="#fff" 
         android:textSize="18sp" /> 

        <TextView 
         android:id="@+id/children" 
         android:layout_width="wrap_content" 
         android:layout_height="wrap_content" 
         android:layout_above="@+id/button3" 
         android:layout_alignParentEnd="true" 
         android:layout_alignParentRight="true" 
         android:layout_marginEnd="38dp" 
         android:layout_marginRight="38dp" 
         android:text="Children" 
         android:textColor="#BDBDBD" /> 

        <Button 
         android:id="@+id/button6" 
         android:layout_width="30dp" 
         android:layout_height="30dp" 
         android:layout_marginEnd="13dp" 

         android:layout_marginRight="13dp" 
         android:background="#90006064" 
         android:text="+" 
         android:textColor="#fff" 
         android:textSize="18sp" 
         android:layout_alignParentBottom="true" 
         android:layout_alignParentRight="true" 
         android:layout_alignParentEnd="true" /> 

        <Button 
         android:id="@+id/button5" 
         android:layout_width="30dp" 
         android:layout_height="30dp" 
         android:background="#90006064" 
         android:text="-" 
         android:textColor="#fff" 
         android:textSize="22sp" 
         android:layout_alignParentBottom="true" 
         android:layout_toLeftOf="@+id/button6" 
         android:layout_toStartOf="@+id/button6" 
         android:layout_marginRight="41dp" 
         android:layout_marginEnd="41dp" /> 
       </RelativeLayout> 
<View 
      android:id="@+id/view3" 
      android:layout_width="100dp" 
      android:layout_height="0.5dp" 
      android:layout_marginTop="20dp" 
      android:background="#00838F" 
      android:foregroundGravity="center" 
      android:layout_below="@+id/scrollView" 
      android:layout_toRightOf="@+id/checkin" 
      android:layout_toEndOf="@+id/checkin"></View> 
<Button 
    android:layout_width="wrap_content" 
    android:layout_height="30dp" 
    android:text="Add Room" 
    android:id="@+id/addroom" 
    android:textColor="#fff" 
    android:layout_marginLeft="20dp" 
    android:background="#50000000" 
    android:layout_marginTop="20dp" 
    android:layout_below="@+id/view3"/> 

hotel_room_fragment.xml:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/roomlayout" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:background="@android:color/transparent"> 


    <TextView 
     android:id="@+id/rooms" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" /> 

    <TextView 
     android:id="@+id/adults" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_below="@+id/rooms" 

     android:layout_toEndOf="@+id/button3" 
     android:layout_toRightOf="@+id/button3" 
     android:text="Adults" 
     android:textColor="#BDBDBD" /> 

    <Button 
     android:id="@+id/button3" 
     android:layout_width="30dp" 
     android:layout_height="30dp" 
     android:layout_below="@+id/adults" 
     android:layout_marginLeft="18dp" 
     android:layout_marginStart="18dp" 
     android:layout_marginTop="10dp" 
     android:layout_toEndOf="@+id/rooms" 
     android:layout_toRightOf="@+id/rooms" 
     android:background="#90006064" 
     android:text="-" 
     android:textColor="#fff" 
     android:textSize="22sp" /> 

    <Button 
     android:id="@+id/button4" 
     android:layout_width="30dp" 
     android:layout_height="30dp" 
     android:layout_alignLeft="@+id/adults" 
     android:layout_alignStart="@+id/adults" 
     android:layout_alignTop="@+id/button3" 
     android:layout_marginLeft="37dp" 

     android:layout_marginStart="37dp" 
     android:background="#90006064" 
     android:text="+" 
     android:textColor="#fff" 
     android:textSize="18sp" /> 

    <TextView 
     android:id="@+id/children" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_above="@+id/button3" 
     android:layout_alignParentEnd="true" 
     android:layout_alignParentRight="true" 
     android:layout_marginEnd="38dp" 
     android:layout_marginRight="38dp" 
     android:text="Children" 
     android:textColor="#BDBDBD" /> 

    <Button 
     android:id="@+id/button6" 
     android:layout_width="30dp" 
     android:layout_height="30dp" 

     android:background="#90006064" 
     android:text="+" 
     android:textColor="#fff" 
     android:textSize="18sp" 

     android:layout_marginRight="13dp" 
     android:layout_marginEnd="13dp" 
     android:layout_alignTop="@+id/button5" 
     android:layout_alignParentRight="true" 
     android:layout_alignParentEnd="true" /> 

    <Button 
     android:id="@+id/button5" 
     android:layout_width="30dp" 
     android:layout_height="30dp" 
     android:background="#90006064" 
     android:text="-" 
     android:textColor="#fff" 
     android:textSize="22sp" 
     android:layout_alignTop="@+id/button4" 
     android:layout_toLeftOf="@+id/children" 
     android:layout_toStartOf="@+id/children" /> 
</RelativeLayout> 
+0

Was haben Sie mit Java-Code versucht? –

+0

Ich würde Ihnen empfehlen, einen ListView dafür zu verwenden. –

Antwort

0

ich mit Bona Fide einverstanden, dass Sie einen Listview/RecyclerView dafür verwenden sollten, nur darauf hinweisen.

Wie auch immer, auf Ihrer onClick-Methode, wo Sie einen neuen RelativeLayout() Aufruf setID() mit einigen zufälligen int erstellen (Sie können eine hardcoded verwenden), dann speichern Sie es in einem Klassenfeld wie private int lastId;

Sobald dies erledigt ist, ersetzen Sie in der LayoutParams.addRule() - Aufruf Ihre R.id.roomlayout mit Ihrem lastId Feld.

Hoffe, du hast es.