【问题标题】:Scrollview extends beyond screen bottom滚动视图超出屏幕底部
【发布时间】:2013-06-02 06:19:18
【问题描述】:

我突然遇到 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>

没有比这更简单的了。一直向下滚动后(如滚动条的形状所示),您应该会看到底部的白色边距,但实际上是这样的:

与顶部比较:

底部应该看起来像顶部,只是颠倒了。这发生在模拟器、真实设备以及我尝试过的几乎每个 Android 版本中。我不知道我做错了什么(如果有的话......)。

请不要猜测,也不要随意射击!只有经过测试的答案。我已经在这上面浪费了足够的时间。谢谢。

【问题讨论】:

    标签: android android-scrollview


    【解决方案1】:

    在死胡同里浪费了很多时间之后,我终于被这个other SO thread 带上了正确的轨道:问题是LinearLayout 上的布局边距。显然 ScrollView 不喜欢这样,就像它不喜欢它的孩子居中一样(许多其他人标记的问题,但这里不是我的问题),谁知道还有什么。非常挑剔的小部件。正是这样的问题让我重新考虑了我对 Android 的承诺:与其他平台相比,它太耗时了,即使你喜欢挑战,时间就是金钱。

    无论如何,为了那些稍后会在这里失败的人的利益,这里是左侧的一个损坏布局(上面的一个更简单的版本)和右侧的一个有效布局的并排演示。诀窍是在一个额外的容器上使用填充来模拟verboten margin。

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

    【讨论】:

      【解决方案2】:

      尝试在滚动视图的底部放置一个填充,以便您在底部看到一条白线 - 您的视图确实一直向下滚动,我用这段代码尝试过,结果是这样的:

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

      【讨论】:

      • Once 确实可以尝试玩这样的游戏,但这是一种 hack,而不是真正的解决方案。
      • PS:在底部看到一些白色本身并不是结束!由于LinearLayout的四面都有边距,看到底部边距是你真正到达底部的标志;就是这样。
      【解决方案3】:

      试着把 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>
      

      【讨论】:

        猜你喜欢
        • 2019-05-02
        • 2023-03-30
        • 1970-01-01
        • 2016-10-06
        • 1970-01-01
        • 2015-11-05
        • 1970-01-01
        • 1970-01-01
        • 2017-07-31
        相关资源
        最近更新 更多