Skip to main content
 首页 » 编程设计

android-layout之Scrollview不会滚动到底部的空白处

2025年02月15日22insus

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

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

滚动 View 的背景为粉红色,内部的线性布局具有android图标图像,高度为800dp(不适合屏幕)。我期望看到的是imageview漂浮在粉红色的背景中,每边(顶部,底部,左侧,右侧)的边距为10dp。但是当我滚动到底部时,scrollview不会滚动到边距,因此滚动的底部是imageview而不是粉红色的边距。

我该如何预防?这使用户认为页面尚未结束,并使他想滚动更多。

请您参考如下方法:

后来我发现,@ olefevre在以下线程https://stackoverflow.com/a/16885601/1474471中已经回答了类似情况。

添加一个额外的LinearLayout,用填充将当前的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>