Die Ansicht in ScrollView findet nicht alle statt


144

Ich habe ein RelativeLayout in einer ScrollView. Mein RelativeLayout hat, android:layout_height="match_parent"aber die Ansicht nimmt nicht die gesamte Größe an, es ist wie ein wrap_content.

Gibt es eine Möglichkeit, das echte Verhalten von fill_parent / match_parent zu erreichen?

Mein Layout:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">

    <ImageView
    android:id="@+id/top"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:scaleType="fitXY"
    android:src="@drawable/top" />

    <ImageView
    android:id="@+id/header"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_below="@+id/top"
    android:scaleType="fitXY"
    android:src="@drawable/headerbig" />

    <ImageView
    android:id="@+id/logo"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignBottom="@+id/header"
    android:layout_centerHorizontal="true"
    android:layout_marginBottom="3dp"
    android:src="@drawable/logo" />

    <ScrollView
    android:layout_width="fill_parent"
        android:layout_height="match_parent"
        android:layout_above="@+id/bottom"
    android:layout_below="@+id/logo"
    android:background="@drawable/background" >

        <RelativeLayout
        android:layout_width="fill_parent"
        android:layout_height="fill_parent">

             <ImageView
            android:id="@+id/dashboard01"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_above="@+id/dashboard02"
            android:layout_centerHorizontal="true"
            android:layout_marginBottom="30dp"
            android:src="@drawable/dashboard01"
            android:visibility="visible" />

            <ImageView
            android:id="@+id/dashboard02"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerInParent="true"
            android:layout_alignParentRight="true"
            android:layout_marginRight="10dp"
            android:src="@drawable/dashboard02" />

             <ImageView
            android:id="@+id/dashboard03"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@+id/dashboard02"
            android:layout_centerHorizontal="true"
            android:layout_marginTop="40dp"
            android:src="@drawable/dashboard03"
            android:visibility="visible" />
         </RelativeLayout>
    </ScrollView>

    <ImageView
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_below="@+id/logo"
    android:layout_centerHorizontal="true"
    android:scaleType="fitXY"
    android:src="@drawable/bar" />

    <ImageView
    android:id="@+id/bottom"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:scaleType="fitXY"
    android:src="@drawable/bottom" />

</RelativeLayout>

Antworten:


461

Versuchen Sie, android:fillViewport="true"zu Ihrer ScrollView hinzuzufügen

Denken Sie daran, dass dies android:layout_height=”fill_parent”bedeutet, dass Sie die Höhe auf die Höhe des Elternteils einstellen. Dies ist offensichtlich nicht das, was Sie wollen, wenn Sie a verwenden ScrollView. Immerhin ScrollViewwürde das nutzlos werden, wenn sein Inhalt immer so groß wäre wie er selbst. Um dies zu umgehen, müssen Sie das aufgerufene ScrollView-Attribut verwenden android:fillViewport. Wenn dieses Attribut auf true gesetzt ist, wird das untergeordnete Element der Bildlaufansicht ScrollViewbei Bedarf auf die Höhe der erweitert . Wenn das Kind größer als das ist ScrollView, hat das Attribut keine Auswirkung.

http://www.curious-creature.org/2010/08/15/scrollviews-handy-trick/


3
Gleiches gilt für HorizontalScrollView.
— CoolMind

Rettete meinen Tag, Scrollview saugt ohne dies: p
— Qasim

Lief wie am Schnürchen! Vielen Dank. Du hast meinen Tag gerettet.
— Anas Azeem

Wenn es bei Ihnen nicht funktioniert, stellen Sie sicher, dass Sie das Attribut in der ScrollView festlegen, nicht das untergeordnete Element!
— Enselic

Danke für die tolle Antwort. Rette auch meinen Tag
— dudi

8

Fügen Sie einfach android: fillViewport = "true" in Ihrem XML-Layout in Scrollview hinzu

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

<!--define views here-->


</ScrollView>
Durch die Nutzung unserer Website bestätigen Sie, dass Sie unsere Cookie-Richtlinie und Datenschutzrichtlinie gelesen und verstanden haben.
Licensed under cc by-sa 3.0 with attribution required.