0

Ich versuche, ein relativelayout in einem Fragment auf die Schaltfläche programmatisch hinzufügen click.But es einfach nicht die layout.below Anzeige der Code:android können nicht hinzufügen relativelayout in Fragment dynamisch

ich bearbeitet haben und veröffentlicht die XML-Layout

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.roomlayout)); 
     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")); 
       relativeLayout1.setLayoutParams(layoutParams); 
       Log.e("Counter",String.valueOf(counter+1)); 


        } 


     }); 
     return v; 
    } 
} 

hotel_search.xml: (Edited Xml Layout)

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:background="@android:color/transparent" 
    android:orientation="vertical"> 

    <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: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> 
<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"/> 
    </RelativeLayout> 
</RelativeLayout> 
+2

Sie verpassen einen Aufruf von addView(). Wahrscheinlich relativeLayout.addView (relativLayout1, layoutParams) ;? – Nanoc

+0

wie die obige Methode zu verwenden.Bitte sagen Sie mir – jobin

+0

Das war die vollständige Zeile, die Sie brauchen ... ersetzen Sie einfach Ihren Aufruf von setLayoutParams mit dieser Zeile. – Nanoc

Antwort

1

verwenden xml

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

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

Ich habe nur eine ID auf das Layout, die Raumaufteilung enthält.

Dann fügen Sie einfach die Ansicht, dass das Layout stattdessen

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

Hoffnung, das hilft.

+0

Danke Nanoc.It hat funktioniert. – jobin

+0

Ich möchte das relative Layout1 unter dem vorherigen hinzufügen, so oft die Schaltfläche geklickt wird. Bitte teilen Sie den Code dafür mit. – jobin

+0

Posten Sie eine andere Frage, die eine Menge Code hinzufügen wird (im Grunde genommen und speichern Sie die ID des zuletzt erstellten Layouts, wenn Sie es selbst tun können). – Nanoc

0

Try this:

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.roomlayout)); 
    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")); 
      relativeLayout1.setLayoutParams(layoutParams); 
      Log.e("Counter",String.valueOf(counter+1)); 

       //this is added: 
      relativeLayout.addView(relativeLayout1); 


       } 


    }); 
    return v; 
} 

}

+0

Versucht es, aber zeigt immer noch den gleichen Weg – jobin