21

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

<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。但是当我滚动到底部时,滚动视图不会滚动到边距,所以滚动的底部是图像视图而不是粉红色边距。

我怎样才能防止这种情况?这使用户认为页面还没有结束,并让他想要滚动更多。

4

3 回答 3

53

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

添加一个额外的 LinearLayout 围绕当前 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>
于 2013-06-18T16:10:00.087 回答
19

@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/>
于 2016-05-05T15:34:16.700 回答
1

使用android:fillViewport="true"ScrollView可以了。

此线程中的示例。

于 2013-06-15T14:36:00.067 回答