Ver dentro de ScrollView no ocupa todo el lugar


144

Tengo un RelativeLayout dentro de un ScrollView. Mi RelativeLayout tiene android:layout_height="match_parent"pero la vista no toma el tamaño completo, es como un wrap_content.

¿Hay alguna manera de tener el verdadero comportamiento fill_parent / match_parent?

Mi diseño:

<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>

Respuestas:


461

Intente agregar android:fillViewport="true"a su ScrollView

recuerde que android:layout_height=”fill_parent”significa "establecer la altura a la altura del padre". Obviamente, esto no es lo que quieres cuando usas a ScrollView. Después de todo, el ScrollViewsería inútil si su contenido fuera siempre tan alto como él mismo. Para evitar esto, debe usar el atributo ScrollView llamado android:fillViewport. Cuando se establece en verdadero, este atributo hace que el elemento secundario de la vista de desplazamiento se expanda a la altura de ScrollViewsi es necesario. Cuando el niño es más alto que el ScrollView, el atributo no tiene efecto.

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


3
Lo mismo es para HorizontalScrollView.
— CoolMind

Me salvó el día, la vista de desplazamiento apesta sin esto: p
— Qasim

¡Trabajado como un encanto! Gracias. Salvaste mi día.
— Anas Azeem

Si no funciona para usted, asegúrese de establecer el atributo en ScrollView, ¡no en el hijo!
— Enselic

Gracias por la gran respuesta. Salva mi día también
— dudi

8

Simplemente agregue android: fillViewport = "true" en su diseño xml en Scrollview

<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>
Al usar nuestro sitio, usted reconoce que ha leído y comprende nuestra Política de Cookies y Política de Privacidad.
Licensed under cc by-sa 3.0 with attribution required.