ScrollView无法滚动到底部。

54
我在Activity中遇到了一个问题,ScrollView无法滚动到底部。
这里有一张截图。 enter image description here 如果你看一下scrollView的滚动条,你会发现它没有滚动到底部。
这是scrollView的XML布局:
<ScrollView
    android:id="@+id/scrollView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_alignParentLeft="true"
    android:layout_alignParentRight="true"
    android:fillViewport="true"
    android:layout_below="@+id/step2_header" >

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="10dp" >

        <TextView
            android:id="@+id/step2_headerText"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_alignParentTop="true"
            android:layout_alignParentRight="true"
            android:gravity="center"
            android:text="@string/Schritt2"
            android:textAppearance="?android:attr/textAppearanceMedium"
            android:textColor="@color/dark_blue"
            android:textStyle="bold|italic" />

        <ImageView
            android:id="@+id/step2_image"
            android:layout_width="wrap_content"
            android:layout_height="100dp"
            android:layout_below="@+id/step2_headerText"
            android:layout_alignParentLeft="true"
            android:layout_alignParentRight="true"
            android:layout_marginTop="10dp"
            android:src="@drawable/menu_leiste" />

        <TextView
            android:id="@+id/step2_infoText"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="10dp"
            android:layout_alignParentLeft="true"
            android:layout_below="@+id/step2_image"
            android:text="@string/step2Text"
            android:textAppearance="?android:attr/textAppearanceSmall" />

       <ImageView
            android:id="@+id/step2_but1Img"
            android:layout_width="match_parent"
            android:layout_height="35dp"
            android:layout_below="@+id/step2_infoText"
            android:layout_marginTop="10dp"
            android:src="@drawable/menu_leiste_selector" />

        <TextView
            android:id="@+id/step2_but1Text"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignBottom="@+id/step2_but1Img"
            android:layout_alignParentLeft="true"
            android:layout_alignTop="@+id/step2_but1Img"
            android:layout_marginLeft="10dp"
            android:gravity="center"
            android:text="@string/step2But1Text"
            android:textAppearance="?android:attr/textAppearanceMedium"
            android:textColor="@color/white" />

        <ImageView
            android:id="@+id/step2_but1ArrowImg"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_margin="3dp"
            android:layout_alignBottom="@+id/step2_but1Img"
            android:layout_alignParentRight="true"
            android:layout_alignTop="@+id/step2_but1Img"
            android:src="@drawable/location_web_site" />

        <ImageView
            android:id="@+id/step2_but2Img"
            android:layout_width="match_parent"
            android:layout_height="35dp"
            android:layout_below="@+id/step2_but1Img"
            android:layout_marginTop="10dp"
            android:src="@drawable/menu_leiste_selector" />

        <TextView
            android:id="@+id/step2_but2Text"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignBottom="@+id/step2_but2Img"
            android:layout_alignParentLeft="true"
            android:layout_alignTop="@+id/step2_but2Img"
            android:layout_marginLeft="10dp"
            android:gravity="center"
            android:text="@string/step2But2Text"
            android:textAppearance="?android:attr/textAppearanceMedium"
            android:textColor="@color/white" />

        <ImageView
            android:id="@+id/step2_but2ArrowImg"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_margin="3dp"
            android:layout_alignBottom="@+id/step2_but2Img"
            android:layout_alignParentRight="true"
            android:layout_alignTop="@+id/step2_but2Img"
            android:src="@drawable/location_web_site" />

    </RelativeLayout>

</ScrollView>

我该如何修复它?

7个回答

157
问题出在ScrollView中RelativeLayout的android:layout_margin="10dp"。请替换。
 <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="10dp">

使用

<RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:padding="10dp" >

8
layout_margin 和 padding 的不同之处在于,layout_margin 是布局的外边距,而 padding 是布局内容的内边距。 - Kirit Vaghela
1
许多人在使用ScrollView时遇到了无法滚动到底部的问题,并且有各种问题被标记出来(正确或不正确),但这是我所知道的唯一一个标记了这个特定边距问题的帖子。你真的救了我的命。在SO#16880311中,我发布了一个完整的布局示例,该示例由于这个非常边距问题而破坏,并提供了可能的解决方法;这可能对某些人有用。简而言之,关键是您可以使用额外的填充容器来模拟边距。 - olefevre
在ScrollView的下方添加一些填充对我很有效。谢谢。 - Manpreet
2
虽然添加填充可以解决问题,但在我看来似乎有点笨拙。更好的解决方案是将“ScrollView”替换为“NesdtedScrollView”。 - Slartibartfast

7

尝试使用NestedScrollView替代。

我自己遇到过这个问题多次,虽然简单地向底部添加额外的填充来隐藏滚动视图在底栏后面的情况是有效的,但更好的解决方案是使用NestedScrollView,正如在此回答中提到的:https://stackoverflow.com/a/36871385/6573127


7

在scrollView中使用的XML:

android:paddingBottom="10dp"

它将会使滚动视图的内容向上移动10dp,而不是VIEW。


在scrollView右侧添加padding有效。在scrollView中添加无效。 - Manpreet
@medphys_muc 在末尾添加一个空视图即可解决问题。请参考链接-https://dev59.com/s-o6XIcBkEYKwwoYFQLr#46359834 - Akshatha S R

4

对于我来说,如果ScrollView的父布局是ConstraintLayout,我有另一种特定的解决方案。如果是这样的话,我们不需要设置填充或边距。

<androidx.constraintlayout.widget.ConstraintLayout 
....>
    <ScrollView
            android:layout_width="match_parent"
            android:layout_height="0dp"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintTop_toBottomOf="<if there is any element before this this scrollview >">

            <RelativeLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content" >

0

我曾经遇到过同样的问题。尝试在scrollView底部添加更多的padding,这对我有用。

android:paddingBottom="50dp"

0

对我来说,为我的一些内部元素设置明确的高度有所帮助。


-3

这可能与您的布局设计有关。如果您为视图添加边距,则它将清晰可见。所以

在滚动视图中添加

android:layout_marginBottom="30dp"

滚动视图是您的父级视图吗? - Ram kiran Pachigolla
尝试给你的相对布局设置底部间距。 - Ram kiran Pachigolla
这并没有帮助我。问题在于RelativeLayout中的android:layout_margin="10dp"。 - medphys

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接