ScrollView无法滚动到底部边缘

21

我有一个简单的布局如下:

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#D23456" >

    <LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_margin="10dp"
    android:background="#FFFFFF" >

        <ImageView
            android:layout_width="match_parent"
            android:layout_height="800dp"
            android:src="@drawable/ic_launcher" />
    </LinearLayout>

</ScrollView>

ScrollView 的背景为粉色,内部的线性布局中有 Android 图标图片,高度为 800dp(不适应屏幕)。我期望看到的是 ImageView 在粉色背景中浮动,并且四周都有 10dp 的边距(上、下、左、右)。但是当我滚动到底部时,ScrollView 没有滚动到边缘,因此滚动到底部后显示的是 ImageView 而非粉色边距。

如何解决这个问题?这会让用户认为页面还没有结束,想要继续滚动。

3个回答

53
我后来发现,一个类似的情况已经在以下线程中得到解答:https://dev59.com/32Qn5IYBdhLWcg3wY2PK#16885601,由@olefevre回答。

添加一个额外的LinearLayout,用padding包围当前的LinearLayout,并删除内部LinearLayout的布局边距即可解决问题:

<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="#D23456"
    android:padding="10dp" >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="#FFFFFF" >

        <ImageView
            android:layout_width="match_parent"
            android:layout_height="800dp"
            android:src="@drawable/ic_launcher" />
    </LinearLayout>
</LinearLayout>

</ScrollView>

1
只是提醒一下,它不必是 LinearLayout。我正在使用 RelativeLayout - theblang
这是解决方案...它像魔法一样奏效了..!@Mehmet Katircioglu - TheFlash
3
正如@mattblang所提到的,为了更好的性能,最好使用FrameLayout而不是较重的线性布局和相对布局。 - TheIT

19

@Mehmet Katircioglu发布的解决方案很好,但您可以通过将android:layout_margin更改为android:padding来简单地解决问题,而不需要额外的任何视图。像这样:

<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >

   <LinearLayout
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:background="#D23456"
      android:padding="10dp" >

      <!-- Your content (ImageView, buttons...) -->
  <LinearLayout/>

1
使用android:fillViewport="true"ScrollView上可能会起作用。
例如,在此主题中

我知道这个属性,但在这种情况下不起作用(请自行尝试)。我们可能漏掉了什么。 - Mehmet Katircioglu

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