【问题标题】:Scrollview doesn't scroll to the margin at the bottomScrollview 不会滚动到底部的边距
【发布时间】:2016-05-05 23:34:16
【问题描述】:

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

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

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

如何防止这种情况发生?这会让用户认为页面还没有结束,并让他想要滚动更多。

【问题讨论】:

    标签: android-layout scrollview


    【解决方案1】:

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

    添加一个额外的 LinearLayout,用 padding 围绕当前 LinearLayout 并移除内部 LinearLayout 的 layout-margin 解决了这个问题:

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

    【讨论】:

    • 仅供参考,不一定是LinearLayout。我正在使用RelativeLayout
    • 这就是解决方案......它就像一个魅力......! @Mehmet Katircioglu
    • 正如@mattblang 所提到的,为了提高性能,最好使用 FrameLayout 而不是较重的线性和相对布局。
    【解决方案2】:

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

    【讨论】:

      【解决方案3】:

      ScrollView 上使用android:fillViewport="true" 可以做到这一点。

      this thread 中的示例。

      【讨论】:

      • 我知道这个属性,但在这种情况下不起作用(自己试试)。我们可能会遗漏一些东西。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-02-14
      • 1970-01-01
      • 2020-03-26
      • 1970-01-01
      • 2022-11-15
      • 1970-01-01
      • 2011-02-19
      相关资源
      最近更新 更多