滚动视图超出屏幕底部

10

我突然遇到了一个问题,即ScrollView扩展到屏幕底部之外,因此即使您滚动到最底部,它也不会显示所有内容。XML代码为:

<?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"
    android:background="#FFFFFFFF">
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="15dp"
        android:background="#FFBBBBBB"
        android:orientation="vertical" >
        <View
            android:layout_width="100dp"
            android:layout_height="300dp"
            android:layout_margin="15dp"
            android:layout_gravity="center_horizontal"
            android:background="#FFDDDDFF"/>
        <View
            android:layout_width="100dp"
            android:layout_height="300dp"
            android:layout_margin="15dp"
            android:layout_gravity="center_horizontal"
            android:background="#FFDDDDFF"/>
    </LinearLayout>
</ScrollView>
那就没有比这更简单的了。一旦您滚动到最底部(根据滚动条的形状指示),您应该看到底部的白色边距,但实际上它看起来是这样的: scrollview的底部 与顶部进行比较: scrollview的顶部 底部应该像顶部一样,只是反过来。 这在模拟器中、在真实设备上以及我尝试的几乎每个Android版本中都会发生。 我不知道我做错了什么(如果有的话…)。
请勿猜测和草率行事! 只有经过测试的答案。 我已经因为这件事浪费了足够的时间。谢谢。
3个回答

10

在走了许多死胡同后,我最终通过这个其他的SO线程找到了正确的方向:问题出在LinearLayout的布局边距上。显然ScrollView不喜欢这样,就像它不喜欢其子项被居中一样(这是许多人标记的问题,但不是我的问题),还有谁知道什么。非常挑剔的小部件。像这样的问题让我重新考虑我的Android承诺:相对于其他平台来说,它太费时了,即使你喜欢挑战,时间也是金钱。

无论如何,为了后来者的利益,这里展示了一个损坏的布局(与上面的版本相比更简单)的左侧和一个工作正常的布局的右侧。关键是通过额外容器上的填充来模拟被禁止的边距。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="#00FFFF"
    android:orientation="horizontal"
    android:baselineAligned="false">
    <ScrollView        
        android:layout_width="0dp"
        android:layout_weight="1"    
        android:layout_height="match_parent"
        android:background="#FFFFFFFF">
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_margin="15dp"
            android:background="#FFBBBB22"
            android:orientation="vertical">
            <View
                android:layout_width="100dp"
                android:layout_height="1000dp"
                android:layout_margin="15dp"
                android:layout_gravity="center_horizontal"
                android:background="#FFDDDDFF"/>
        </LinearLayout>
    </ScrollView>
    <View 
        android:layout_width="1dp"
        android:layout_height="match_parent"
        android:background="#FF000000"/>
    <ScrolllView 
        android:layout_width="0dp"
        android:layout_weight="1"
        android:layout_height="match_parent"
        android:background="#FFFFFFFF">
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:padding="15dp"
            android:background="#FFFFFF"
            android:orientation="vertical">
            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:background="#FFBBBB22"
                android:orientation="vertical">
                <View
                    android:layout_width="100dp"
                    android:layout_height="1000dp"
                    android:layout_margin="15dp"
                    android:layout_gravity="center_horizontal"
                    android:background="#FFDDDDFF"/>
            </LinearLayout>
        </LinearLayout>
    </ScrollView>
</LinearLayout>

0
尝试在滚动视图底部放置填充,这样你就可以看到底部的白线 - 你的视图确实可以滚动到最底部,我用了这段代码进行了尝试,结果如下:
<?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"

    android:background="#FFFFFFFF"
    android:padding="5dp" >

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_margin="15dp"
    android:background="#FFBBBBBB"
    android:orientation="vertical" >

    <View
        android:layout_width="100dp"
        android:layout_height="1500dp"
        android:layout_gravity="center_horizontal"
        android:layout_margin="15dp"
        android:background="#FFDDDDFF" />

    <View
        android:layout_width="100dp"
        android:layout_height="300dp"
        android:layout_gravity="center_horizontal"
        android:layout_margin="15dp"
        android:background="#FFDDDDFF" />

    <View
        android:layout_width="100dp"
        android:layout_height="10dp"
        android:layout_gravity="center_horizontal"
        android:layout_margin="15dp"
        android:background="#FF00FF00" />
</LinearLayout>

enter image description here


可以尝试像那样玩游戏,但那只是一种hack方法,而不是真正的解决方案。 - olefevre
PS: 在底部看到一些白色并不是最终目的!由于LinearLayout的所有边缘都有边距,因此在底部看到底部边距是你真正到达底部的标志;就这样。 - olefevre

0

尝试将LinearLayout放在另一个LinearLayout中 像这样:

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

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

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_margin="15dp"
            android:background="#FFBBBBBB"
            android:orientation="vertical">

            <View
                android:layout_width="100dp"
                android:layout_height="300dp"
                android:layout_gravity="center_horizontal"
                android:layout_margin="15dp"
                android:background="#FFDDDDFF" />

            <View
                android:layout_width="100dp"
                android:layout_height="300dp"
                android:layout_gravity="center_horizontal"
                android:layout_margin="15dp"
                android:background="#FFDDDDFF" />
        </LinearLayout>
    </LinearLayout>
</ScrollView>

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