简体   繁体   English

ScrollView在其他设备上不起作用

[英]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. 我在RelativeLayout和LinearLayout中创建了一个简单的ScrollView,该ScrollView在较新的设备上运行良好,我在Nexus 5上进行了测试,一切正常,但是当我在较旧的设备(不是很旧的设备)中对其进行测试时,滚动无法正常工作,布局仅显示视图的顶部,并且不允许我进行滚动。

So my xml is like this: 所以我的xml是这样的:

<?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. 我试图用线性布局包围我的布局,试图遵循stackoverflow用户的建议方法,但是没有用。

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. ScrollView的子视图应设置为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. 如果将其设置为match_parent,它将填充ScrollView的区域并且永远不会滚动,因为它不会大于ScrollView。

Try changing the child LinearLayout layout_height to either wrap_content or a specific size (in dp) instead of match_parent. 尝试将子级LinearLayout layout_height更改为wrap_content或特定大小(以dp为单位),而不是match_parent。

EDIT: 编辑:

Also, try changing height of scroll view from match_parent to wrap_content. 另外,尝试将滚动视图的高度从match_parent更改为wrap_content。 Because scroll view will enable scrolling only if its total height is more than the height of parent view. 因为滚动视图仅在其总高度大于父视图的高度时才启用滚动。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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