4

I'd like a view to match the height of its parent in the following:

<RelativeLayout
    layout_height="wrap_content">

    <LinearLayout
        id="@+id/A"
        layout_height="wrap_content"
        layout_alignParentTop="true">
        ...
    </LinearLayout>

    <LinearLayout
        id="@+id/B"
        layout_height="match_parent"   <-- possible?
        layout_alignParentTop="true">
        ...
    </LinearLayout>

</RelativeLayout>

So linear layout A gets inflated to lets say 100dp. Linear layout B gets inflated to about 50 dp tall. But I want it to match the height of the parent, which in this case should have been 100dp to accomodate A. Is there a way to do this? I've got my layout as above, but B does not extend to the full height that A is setting for the parent container.

Thanks

user291701
  • 38,411
  • 72
  • 187
  • 285

2 Answers2

6

So, match_parent replaced fill_parent. and I found this question which was answered by Romain Guy.

combining wrap_content on parent and fill_parent on child

He states that this shouldn't work, but that they made it work for LinearLayout and that he made it recently work for FrameLayout for Honeycomb.

Based on this, I'll speculate that this might not work for RelativeLayout?

Try using LinearLayout or FrameLayout instead of RelativeLayout maybe and see if that works?

EDIT Here is what I did:

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

    <FrameLayout
        android:layout_height="wrap_content"
        android:layout_width="match_parent">

        <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            >
            <TextView  
                android:layout_width="match_parent" 
                android:layout_height="wrap_content" 
                android:text="@string/hello"
                />
        </LinearLayout>

        <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            >
            <Button 
                android:layout_height="match_parent"
                android:layout_width="match_parent"
                android:text="TEST"/>
        </LinearLayout>

    </FrameLayout>



</LinearLayout>

The value of the string:

<string name="hello">Hello World, Game! Hello World, Game! Hello World, Game! Hello World, Game! Hello World, Game! Hello World, Game! Hello World, Game! Hello World, Game! Hello World, Game! Hello World, Game! Hello World, Game! Hello World, Game! Hello World, Game! Hello World, Game! Hello World, Game! Hello World, Game! Hello World, Game! Hello World, Game! Hello World, Game! Hello World, Game! Hello World, Game! Hello World, Game! Hello World, Game! Hello World, Game! Hello World, Game! Hello World, Game! Hello World, Game! Hello World, Game! Hello World, Game! Hello World, Game!</string>
Community
  • 1
  • 1
Stericson
  • 415
  • 2
  • 5
  • Oh but I need either FrameLayout or RelativeLayout specifically so B can sit on top of A in z order. I tried replacing RelativeLayout with FrameLayout on the outermost parent (if that's what you meant), no effect though. – user291701 Sep 20 '12 at 18:48
  • Are you developing for Honeycomb or higher? (API level 11+)? I did a quick test using a FrameLayout targeting API level 11+ and it worked as you described. I can confirm it does not work for RelativeLayout. – Stericson Sep 20 '12 at 18:59
  • Ah ok gotcha - this is quite a nice trick, it works, even with my complex layout. Thanks! – user291701 Sep 20 '12 at 21:50
-1

Why don't you simply set alignParentTop & alignParentBottom to be true? It should work for you.

Derekyy
  • 1,018
  • 4
  • 18
  • 31