Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I make a `View` fill a `RelativeLayout`?

I have a RelativeLayout with some stuff in it, and I want a background view to fill the whole thing. This is what I would expect to work:

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="#000" >

            <View
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_alignParentLeft="true"
                android:layout_alignParentRight="true"
                android:layout_alignParentTop="true"
                android:layout_alignParentBottom="true"
                android:layout_margin="1px"
                android:background="#fafafa" />

            <ImageView
                android:id="@+id/im"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentLeft="true"
                android:layout_centerVertical="true"
                android:src="@drawable/ic_blank_profile" />

        .... and so on.

That doesn't work. It fills the width but the height gets set to zero. However, if I change layout_alignParentBottom to layout_alignBottom="@+id/im" then it does work (sort of; that's not a satisfactory solution but at least it isn't 0 height any more).

What's going on? Is this a bug?

like image 294
Timmmm Avatar asked Oct 17 '12 17:10

Timmmm


1 Answers

You need to change the relative layout from

android:layout_height="wrap_content"

to

android:layout_height="match_parent"

Because you are wrapping the height of the parent to the child, and the child to match the parent, it will come up 0.

if you are wanting to have other things above/below the relative layout, then use android weights.

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="0px"  <!-- or 0dip -->
        android:layout_weight="1"
        android:background="#000" >
like image 94
IAmGroot Avatar answered Oct 16 '22 22:10

IAmGroot