简体   繁体   中英

ScrollView doesn't work on a different device

I created a simple ScrollView usin RelativeLayout and LinearLayout, the ScrollView works well on newer devices, i test it on a Nexus 5 and everything is ok, but when i test it in older devices(not so old) the scroll doesn't work, the layout shows just the top part of the view and it doesn't let me do the scrolling.

So my xml is like this:

<?xml version="1.0" encoding="utf-8"?>

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


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

<android.support.percent.PercentRelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">


        <android.support.design.widget.FloatingActionButton
            android:id="@+id/fab"
            app:srcCompat="@android:drawable/ic_menu_camera"
            android:elevation="2dp"
            app:layout_widthPercent="90%"
            app:layout_heightPercent="10%"
            app:layout_marginTopPercent="90%"
            app:layout_marginLeftPercent="80%"
            app:backgroundTint="#f1c40f"
            android:onClick="capture"/>

        <include
            layout="@layout/custom_toolbar"
            android:id="@+id/my_toolbar"
            app:layout_widthPercent="100%"
            app:layout_heightPercent="8%"
            app:layout_marginTopPercent="0%"
            app:layout_marginLeftPercent="0%" />



        <com.example.afcosta.inesctec.pt.android.Helpers.OpenSansMedium
            android:id="@+id/SpecieName"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textColor="@color/nephritis"
            android:textSize="14dp"
            app:layout_widthPercent="60%"
            app:layout_heightPercent="5%"
            app:layout_marginTopPercent="85%"
            app:layout_marginLeftPercent="30%"/>

        <com.example.afcosta.inesctec.pt.android.Helpers.OpenSansMedium
            android:id="@+id/GenreName"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:onClick="genrePressed"
            android:textColor="@color/nephritis"
            android:textSize="14dp"
            app:layout_widthPercent="60%"
            app:layout_heightPercent="5%"
            app:layout_marginTopPercent="95%"
            app:layout_marginLeftPercent="30%"/>

        <com.example.afcosta.inesctec.pt.android.Helpers.OpenSansMedium
            android:id="@+id/FamilyName"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:onClick="familyPressed"
            android:textColor="@color/nephritis"
            android:textSize="14dp"
            app:layout_widthPercent="60%"
            app:layout_heightPercent="5%"
            app:layout_marginTopPercent="105%"
            app:layout_marginLeftPercent="30%"/>

        <com.example.afcosta.inesctec.pt.android.Helpers.OpenSansMedium
            android:id="@+id/textView8"
            android:text="Espécie"
            android:textSize="14dp"
            app:layout_widthPercent="25%"
            app:layout_heightPercent="5%"
            app:layout_marginTopPercent="85%"
            app:layout_marginLeftPercent="5%"
 />

        <com.example.afcosta.inesctec.pt.android.Helpers.OpenSansMedium
            android:id="@+id/textView7"
            app:layout_widthPercent="25%"
            app:layout_heightPercent="5%"
            app:layout_marginTopPercent="95%"
            app:layout_marginLeftPercent="5%"
            android:text="Género"
            android:textSize="14dp" />

        <com.example.afcosta.inesctec.pt.android.Helpers.OpenSansMedium
            android:id="@+id/Family"
            app:layout_widthPercent="25%"
            app:layout_heightPercent="5%"
            app:layout_marginTopPercent="105%"
            app:layout_marginLeftPercent="5%"
            android:text="Familia"
            android:textSize="14dp" />

        <com.example.afcosta.inesctec.pt.android.Helpers.OpenSansMedium
            android:id="@+id/specieDescription"
            android:textSize="14dp"
            app:layout_widthPercent="90%"
            android:layout_height="wrap_content"
            app:layout_marginTopPercent="120%"
            app:layout_marginLeftPercent="5%"/>

        <com.example.afcosta.inesctec.pt.android.Helpers.OpenSansMedium
            android:id="@+id/descricaoTit"
            android:text="Descrição"
            android:textSize="14dp"
            app:layout_widthPercent="30%"
            app:layout_heightPercent="5%"
            app:layout_marginTopPercent="115%"
            app:layout_marginLeftPercent="5%"/>

        <ImageView
            android:id="@+id/plantImage"
            android:adjustViewBounds="true"
            android:scaleType="fitXY"
            app:layout_widthPercent="80%"
            app:layout_heightPercent="50%"
            app:layout_marginTopPercent="10%"
            app:layout_marginLeftPercent="10%"
            app:srcCompat="@drawable/color_cursor_white" />

        <android.support.v7.widget.RecyclerView
            android:id="@+id/gallery"
            app:layout_widthPercent="100%"
            app:layout_heightPercent="10%"
            app:layout_marginTopPercent="62%"
            app:layout_marginLeftPercent="0%" />

        <RelativeLayout
            app:layout_widthPercent="40%"
            app:layout_heightPercent="5%"
            app:layout_marginTopPercent="75%"
            app:layout_marginLeftPercent="5%"
            android:onClick="showMap">

            <TextView
                android:id="@+id/googleMapsText"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_toRightOf="@+id/imageView6"
                android:layout_alignBottom="@+id/imageView6"
                android:text="Ver no mapa" />

            <ImageView
                android:id="@+id/imageView6"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:src="@drawable/ic_facebook_placeholder_for_locate_places_on_maps"
                android:tint="#F90101" />
        </RelativeLayout>

    </android.support.percent.PercentRelativeLayout>
        </LinearLayout>
</ScrollView>

I tried to surround my layout with a linear layout, trying to follow the sugested approaches from the stackoverflow users, but doesn't work.

Any idea?

Just give it a try. I am not sure it ll work or not because i don't know about your percent layout.

<android.support.v4.widget.NestedScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fillViewport="true">


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

<android.support.percent.PercentRelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">


        <android.support.design.widget.FloatingActionButton
            android:id="@+id/fab"
            app:srcCompat="@android:drawable/ic_menu_camera"
            android:elevation="2dp"
            app:layout_widthPercent="90%"
            app:layout_heightPercent="10%"
            app:layout_marginTopPercent="90%"
            app:layout_marginLeftPercent="80%"
            app:backgroundTint="#f1c40f"
            android:onClick="capture"/>

        <include
            layout="@layout/custom_toolbar"
            android:id="@+id/my_toolbar"
            app:layout_widthPercent="100%"
            app:layout_heightPercent="8%"
            app:layout_marginTopPercent="0%"
            app:layout_marginLeftPercent="0%" />



        <com.example.afcosta.inesctec.pt.android.Helpers.OpenSansMedium
            android:id="@+id/SpecieName"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textColor="@color/nephritis"
            android:textSize="14dp"
            app:layout_widthPercent="60%"
            app:layout_heightPercent="5%"
            app:layout_marginTopPercent="85%"
            app:layout_marginLeftPercent="30%"/>

        <com.example.afcosta.inesctec.pt.android.Helpers.OpenSansMedium
            android:id="@+id/GenreName"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:onClick="genrePressed"
            android:textColor="@color/nephritis"
            android:textSize="14dp"
            app:layout_widthPercent="60%"
            app:layout_heightPercent="5%"
            app:layout_marginTopPercent="95%"
            app:layout_marginLeftPercent="30%"/>

        <com.example.afcosta.inesctec.pt.android.Helpers.OpenSansMedium
            android:id="@+id/FamilyName"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:onClick="familyPressed"
            android:textColor="@color/nephritis"
            android:textSize="14dp"
            app:layout_widthPercent="60%"
            app:layout_heightPercent="5%"
            app:layout_marginTopPercent="105%"
            app:layout_marginLeftPercent="30%"/>

        <com.example.afcosta.inesctec.pt.android.Helpers.OpenSansMedium
            android:id="@+id/textView8"
            android:text="Espécie"
            android:textSize="14dp"
            app:layout_widthPercent="25%"
            app:layout_heightPercent="5%"
            app:layout_marginTopPercent="85%"
            app:layout_marginLeftPercent="5%"
 />

        <com.example.afcosta.inesctec.pt.android.Helpers.OpenSansMedium
            android:id="@+id/textView7"
            app:layout_widthPercent="25%"
            app:layout_heightPercent="5%"
            app:layout_marginTopPercent="95%"
            app:layout_marginLeftPercent="5%"
            android:text="Género"
            android:textSize="14dp" />

        <com.example.afcosta.inesctec.pt.android.Helpers.OpenSansMedium
            android:id="@+id/Family"
            app:layout_widthPercent="25%"
            app:layout_heightPercent="5%"
            app:layout_marginTopPercent="105%"
            app:layout_marginLeftPercent="5%"
            android:text="Familia"
            android:textSize="14dp" />

        <com.example.afcosta.inesctec.pt.android.Helpers.OpenSansMedium
            android:id="@+id/specieDescription"
            android:textSize="14dp"
            app:layout_widthPercent="90%"
            android:layout_height="wrap_content"
            app:layout_marginTopPercent="120%"
            app:layout_marginLeftPercent="5%"/>

        <com.example.afcosta.inesctec.pt.android.Helpers.OpenSansMedium
            android:id="@+id/descricaoTit"
            android:text="Descrição"
            android:textSize="14dp"
            app:layout_widthPercent="30%"
            app:layout_heightPercent="5%"
            app:layout_marginTopPercent="115%"
            app:layout_marginLeftPercent="5%"/>

        <ImageView
            android:id="@+id/plantImage"
            android:adjustViewBounds="true"
            android:scaleType="fitXY"
            app:layout_widthPercent="80%"
            app:layout_heightPercent="50%"
            app:layout_marginTopPercent="10%"
            app:layout_marginLeftPercent="10%"
            app:srcCompat="@drawable/color_cursor_white" />

        <android.support.v7.widget.RecyclerView
            android:id="@+id/gallery"
            app:layout_widthPercent="100%"
            app:layout_heightPercent="10%"
            app:layout_marginTopPercent="62%"
            app:layout_marginLeftPercent="0%" />

        <RelativeLayout
            app:layout_widthPercent="40%"
            app:layout_heightPercent="5%"
            app:layout_marginTopPercent="75%"
            app:layout_marginLeftPercent="5%"
            android:onClick="showMap">

            <TextView
                android:id="@+id/googleMapsText"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_toRightOf="@+id/imageView6"
                android:layout_alignBottom="@+id/imageView6"
                android:text="Ver no mapa" />

            <ImageView
                android:id="@+id/imageView6"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:src="@drawable/ic_facebook_placeholder_for_locate_places_on_maps"
                android:tint="#F90101" />
        </RelativeLayout>

    </android.support.percent.PercentRelativeLayout>
        </LinearLayout>
</android.support.v4.widget.NestedScrollView>

The child view of a ScrollView should be set to wrap_content. If you set it to match_parent, it will fill the area of the ScrollView and never scroll, because it won't be larger than the ScrollView.

Try changing the child LinearLayout layout_height to either wrap_content or a specific size (in dp) instead of match_parent.

EDIT:

Also, try changing height of scroll view from match_parent to wrap_content. Because scroll view will enable scrolling only if its total height is more than the height of parent view.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM