【问题标题】:ScrollView adds large space at the bottomScrollView 在底部增加了大空间
【发布时间】:2013-09-06 13:13:28
【问题描述】:

我的主 Activity 有许多按钮,它们在大平板电脑屏幕上彼此下方排列,但在较小的手机屏幕上不适合。我添加了一个 ScrollView,以便用户可以在屏幕尺寸需要时向下滚动到其他按钮:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
          android:layout_width="match_parent"
          android:layout_height="wrap_content"
          android:orientation="vertical"
          android:background="@drawable/bg"
          android:gravity="center_horizontal">

    <TextView
        android:id="@+id/trackSelectorText"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:gravity="center_horizontal"/>

    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical"                
            android:gravity="center_horizontal">

        <Button                    
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="foo" />
        <!-- More buttons -->
        </LinearLayout>
    </ScrollView>
</LinearLayout>

这有点工作,但是我在最后一个按钮下方有一个很大的空白区域(即 ScrollView 可以向下滚动太远)。无论是在实际平板电脑上还是在具有电话配置的模拟器上。如果我 center 内部的 LinearLayout(而不是 center_horizontal),空间会均匀分布在顶部和底部之间,我也不想要。如何修复此布局?

我希望这张图片能让它更清晰,想象一下 ScrollView 已经完全向下滚动并显示最后一个按钮。我的意思是右图Button 6下方的空白:

【问题讨论】:

  • 你能画出你想要的吗?
  • 完成了,我希望这能让可视化变得更容易。
  • 尝试提供ScrollView android:layout_height="match_parent"。我怀疑空间在滚动视图内,但实际上在下面
  • 是的 Lenart,将 ScrollView 包裹在垂直的 LinearLayout 周围,放置 maxWeightSum=3,并为每个按钮提供 weigth=1。完成 :) LinearLayout 应该 fill_parent,滚动布局应该定义线性布局的视口大小。

标签: android android-scrollview


【解决方案1】:

尝试将此属性添加到您的 ScrollView 元素。 android:fillViewport="true"

<ScrollView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_weight="1"
    android:fillViewport="true" > ... </ScrollVIew>

参考:ScrollView Ref

【讨论】:

  • android:layout_height="match_parent" 应改为 android:layout_height="0dp"
【解决方案2】:

实际上,它与布局并没有真正的关系(除非我遗漏了一些东西)。我尝试注释掉归因的背景,它突然起作用了。图像的背景 (@drawable/bg) 太大而导致滚动。我将背景转换为 9-patch 图像,因此它可以自动缩放,瞧,额外的空间消失了。 不过,如果没有 9-patch 也能做到这一点,我仍然很想知道这是否可以在 XML 文件中完成。

【讨论】:

    【解决方案3】:

    你可以在滚动视图中使用这个属性来避免视图底部的额外填充

    android:overScrollMode="never"
    

    【讨论】:

      【解决方案4】:

      我已经检查了我的代码..完美运行:

      <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical"
            android:background="@drawable/bg"
            android:gravity="center_horizontal">
      
      <TextView
          android:id="@+id/trackSelectorText"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:gravity="center_horizontal"/>
      
      <ScrollView
          android:layout_width="match_parent"
          android:layout_height="match_parent"
          android:layout_gravity="center">
      
          <LinearLayout
              android:layout_width="match_parent"
              android:layout_height="wrap_content"
              android:orientation="vertical"                
              android:gravity="center_horizontal">
      
          <Button                    
                  android:layout_width="wrap_content"
                  android:layout_height="wrap_content"
                  android:text="foo" />
          <!-- More buttons -->
          </LinearLayout>
        </ScrollView>
      </LinearLayout>
      

      可能会帮助你.......!!

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-09-15
        • 2020-04-18
        • 2013-04-02
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多