Derek 朕會功夫
Derek 朕會功夫

Reputation: 94319

Setting minimum height of a view to be the height of its parent

Inside a ScrollView I have a LinearLayout. The LinearLayout can be as tall as it wants, but I want its minimum height to be at least as tall as its parent (the ScrollView).

<ScrollView
    ...>
    <LinearLayout
        ...
        android:layout_marginTop="@dimen/some_margin"
        android:minHeight="match_parent">

This does not work. It won't accept "match_parent" for minHeight. So how do I approach this?

Upvotes: 3

Views: 2077

Answers (2)

Mike
Mike

Reputation: 4570

You could use the ScrollView's android:fillViewport="true" property in the xml file like this:

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fillViewport="true">

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

        <!--Rest of the children-->
    </LinearLayout>

</ScrollView>

Upvotes: 8

Riaz
Riaz

Reputation: 874

So you just want to avoid having wasted real estate when the stuff in the LinearLayout doesn't fill up the space allowed by the ScrollView? How about just setting LinearLayout height to:

android:layout_height="match_parent"

You could set the same on the ScrollView so that it fits its own parent as well, but not strictly necessary.

Upvotes: 0

Related Questions