fffred
fffred

Reputation: 697

ScrollView hides some sub-layouts

I am trying to have several layouts inside a ScrollView. Of course a ScrollView can contain only one child layout, so this is what I did:

<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:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <LinearLayout
            android:orientation="vertical"
            android:layout_width="match_parent"
            android:layout_height="match_parent">

            <TextView
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:gravity="center_horizontal"
                android:minHeight="50dp"
                android:text="Top layout" />
        </LinearLayout>

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

            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="bottom layout"
                android:gravity="center_horizontal" />
        </LinearLayout>

    </LinearLayout>
</ScrollView>

Basically I have one LinearLayout that contains two other LinearLayouts ("top" and "bottom").

I want the top layout to take as much space as possible, leaving the bottom one at the very bottom.

Problem: by setting android:layout_height="match_parent" everywhere, the top layout takes all the space, and the bottom layout does not appear.

How can I fix that?

EDIT: Keeping the layouts heights fixed is not what I'm looking for. The size must be able to extend vertically.

Upvotes: 0

Views: 988

Answers (3)

SorryForMyEnglish
SorryForMyEnglish

Reputation: 1181

here's what you need

<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:orientation="vertical"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            >

            <LinearLayout
                android:orientation="vertical"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_weight="1">

                <TextView
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:gravity="center_horizontal"
                    android:minHeight="50dp"
                    android:text="Top layout" />
            </LinearLayout>

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

                <TextView
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:text="bottom layout"
                    android:gravity="center_horizontal" />
            </LinearLayout>

        </LinearLayout>
    </ScrollView>

in first chld layout need add android:layout_weight="1"

Upvotes: 0

Alexander Zhak
Alexander Zhak

Reputation: 9272

You set height of your Top layout to match_parent. That's why it pushes Bottom layout off screen. Use layout_height="0dp" and layout_weight="1" instead

Upvotes: 2

Kostas Drak
Kostas Drak

Reputation: 3260

try this:

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

    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/scrollView" >

        <LinearLayout
            android:orientation="vertical"
            android:layout_width="match_parent"
            android:layout_height="match_parent">

            <LinearLayout
                android:orientation="vertical"
                android:layout_width="match_parent"
                android:layout_gravity="top|center"
                android:layout_height="472dp" >

                <TextView
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:textAppearance="?android:attr/textAppearanceMedium"
                    android:text="Medium Text"
                    android:id="@+id/textView" />
            </LinearLayout>

            <LinearLayout
                android:orientation="vertical"
                android:layout_width="match_parent"
                android:layout_gravity="bottom|center"
                android:layout_height="100dp" >

                <TextView
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:textAppearance="?android:attr/textAppearanceMedium"
                    android:text="Medium Text"
                    android:id="@+id/textView4" />
            </LinearLayout>
        </LinearLayout>
    </ScrollView>
</RelativeLayout>

Upvotes: 0

Related Questions