0

Я хочу динамически добавлять relativelayout ниже предыдущего каждый раз, когда нажимается кнопка. Я смог добиться этого за один клик. Пожалуйста, поделитесь исходным кодом для этого.android -add relativelayout под другим нажатием кнопки

hotel_search.xml:

<RelativeLayout 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:layout_centerHorizontal="true" 
    android:layout_centerVertical="true" 
    android:layout_marginBottom="60dp" 
    android:layout_marginLeft="30dp" 
    android:layout_marginRight="30dp" 
    android:background="#80212121"> 

    <TextView 
     android:id="@+id/locationtext" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_marginLeft="10dp" 
     android:layout_marginTop="30dp" 
     android:text="Location" 
     android:textColor="#BDBDBD" 
     android:textSize="15sp" /> 

    <AutoCompleteTextView 
     android:id="@+id/autohotellocation" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_below="@+id/locationtext" 
     android:layout_marginLeft="30dp" 
     android:layout_marginTop="20dp" 
     android:background="@android:color/transparent" 
     android:text="Hotel Name/City/Code" 
     android:textColor="#ffffff" /> 

    <View 
     android:id="@+id/view1" 
     android:layout_width="100dp" 
     android:layout_height="0.5dp" 
     android:layout_below="@+id/autohotellocation" 
     android:layout_centerHorizontal="true" 
     android:layout_marginTop="22dp" 
     android:background="#00838F" 
     android:foregroundGravity="center"></View> 

    <TextView 
     android:id="@+id/checkin" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentLeft="true" 
     android:layout_below="@+id/view1" 
     android:layout_marginLeft="30dp" 
     android:layout_marginTop="10dp" 
     android:text="CheckIn" 
     android:textColor="#BDBDBD" /> 

    <RelativeLayout 
     android:id="@+id/relativeLayout" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 

     android:layout_alignLeft="@+id/locationtext" 
     android:layout_alignStart="@+id/locationtext" 
     android:layout_below="@+id/checkin" 
     android:layout_marginLeft="12dp" 
     android:layout_marginStart="12dp"> 

     <TextView 
      android:id="@+id/checkindate" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_alignParentTop="true" 
      android:layout_centerHorizontal="true" 
      android:text="18" 
      android:textColor="#fff" 
      android:textSize="23sp" /> 

     <TextView 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_below="@+id/checkindate" 
      android:text="Jun 2015" 
      android:textColor="#fff" 
      android:textSize="15sp" /> 
    </RelativeLayout> 

    <View 
     android:id="@+id/view2" 
     android:layout_width="100dp" 
     android:layout_height="0.5dp" 
     android:layout_below="@+id/relativeLayout" 
     android:layout_centerHorizontal="true" 
     android:layout_marginTop="22dp" 
     android:background="#00838F" 
     android:foregroundGravity="center"></View> 

    <ScrollView 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_below="@+id/view2" 
     android:id="@+id/scrollView" 
     android:scrollbars="vertical"> 

     <RelativeLayout 
      android:id="@+id/rootlayout" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_below="@+id/view2" 
      android:layout_marginTop="30dp" 
      android:scrollbars="vertical" 
      > 

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


       <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> 
     </RelativeLayout> 
    </ScrollView> 

    <TextView 
     android:id="@+id/checkout" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentEnd="true" 
     android:layout_alignParentRight="true" 
     android:layout_alignTop="@+id/checkin" 
     android:layout_marginEnd="21dp" 
     android:layout_marginRight="21dp" 
     android:text="CheckOut" 
     android:textColor="#BDBDBD" /> 

    <RelativeLayout 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignLeft="@+id/checkout" 
     android:layout_alignStart="@+id/checkout" 
     android:layout_alignTop="@+id/relativeLayout" 
     android:id="@+id/relativeLayout2"> 

     <TextView 
      android:id="@+id/checkoutdate" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_alignParentTop="true" 
      android:layout_centerHorizontal="true" 
      android:text="18" 

      android:textColor="#fff" 
      android:textSize="23sp" /> 

     <TextView 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_below="@+id/checkoutdate" 
      android:text="Jun 2015" 
      android:textColor="#fff" 
      android:textSize="15sp" /> 
    </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> 

HotelSearch.java:

public class HotelSearch extends Fragment { 
    Button button;RelativeLayout relativeLayout,relativeLayout1;int counter=0; 
@Override 
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable final Bundle savedInstanceState) { 

     View v=inflater.inflate(R.layout.hotel_search,container,false); 

     relativeLayout=((RelativeLayout) v.findViewById(R.id.rootlayout)); 
     button=(Button) v.findViewById(R.id.addroom); 
     button.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 

       relativeLayout1=new RelativeLayout(getContext()); 
       RelativeLayout.LayoutParams layoutParams=new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT,20); 
       layoutParams.addRule(RelativeLayout.BELOW,R.id.roomlayout); 
       relativeLayout1.setBackgroundColor(Color.parseColor("#000000")); 
       relativeLayout.addView(relativeLayout1, layoutParams); 
       Log.e("Counter",String.valueOf(counter+1)); 


        } 


     }); 
     return v; 
    } 
} 
+0

Ваш вопрос не понятен , –

+0

использовать линейную компоновку было бы легче сделать с этим –

+0

Я не понимаю ваш макет ... – russellhoff

ответ

0

На вашем методе OnClick, где вы создаете новый RelativeLayout() вызов SETID() с некоторым случайным междунаром (вы не можете используйте hardcoded), затем сохраните его в поле класса, например private int lastId;

Как только это будет сделано, в вызове LayoutParams.addRule() замените ваше поле R.id.roomlayout своим последним полем.

Упование вы получили.

0

Простое решение будет сделать свой родительский макет в Linear Layout с вертикальной ориентации Теперь остальные вещи в вашем коде будет то же самое с одним дополнением в вашем коде после этого relativeLayout.addView(relativeLayout1, layoutParams); добавить эту строку 'linerLayout.addView(relativeLayout)';

Смежные вопросы