android 无法在 fragment 中动态添加 relativelayout

android cannot add relativelayout in fragment dynamically

我正在尝试在按钮 click.But 上以编程方式在片段内添加相对布局,它根本不显示 layout.below 是代码:

我已经编辑并发布了 xml 布局。

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:(已编辑 Xml 布局)

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

试试这个:

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

}

使用这个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>

我只是在包含 roomlayout 的布局上放置了一个 id。

然后只需将视图添加到该布局即可

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

希望这对您有所帮助。