我遇到了由包含LinearLayout的ScrollView组成的片段的问题。我试图创建一个效果,其中LinearLayout具有白色背景,看起来像是在彩色背景上滚动的一张纸。我试图达到这个目标的方式是让ScrollView占据片段的全部空间,然后LinearLayout里面有android:layout_margin="16dp"
来创建“纸张”周围的空间。ScrollView不滚动到内部LinearLayout的底部边距的末尾
这样,ScrollView的滚动条会出现在彩色背景区域中,顶部的边距会随着内容一起滚动并且底部的边距只会在到达结尾时滚动。
不幸的是,在这个配置中,ScrollView不会一直滚动到最后,实际上会在底部切掉很少量的文本。我怀疑ScrollView在垂直滚动距离中没有考虑到它的孩子的边距。为了解决这个问题,我将LinearLayout包装在一个FrameLayout中,它解决了这个问题,但似乎是多余的。任何指针如何消除这个不需要的容器将不胜感激。
注意:在滚动视图上设置android:padding="16dp"
并删除边距不会产生所需的效果,因为无论滚动位置如何,连续出现在所有四个边上的填充。
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
tools:context=".ArticleFragment" >
<!-- This FrameLayout exists purely to force the outer ScrollView to respect
the margins of the LinearLayout -->
<FrameLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="10dp"
android:layout_margin="16dp"
android:background="@color/page_background" >
<TextView
android:id="@+id/article_title"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textIsSelectable="true" />
<TextView
android:id="@+id/article_content"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textIsSelectable="true" />
</LinearLayout>
</FrameLayout>
</ScrollView>
尝试设置'机器人:layout_height =“FILL_PARENT”'的'ScrollView'? – 2013-05-08 14:50:46
@AdilSoomro是的,没有喜悦。 ScrollView的大小并不是一个真正的问题 - 它会适当地填充可用的空间。这是它提供的滚动区域 - 当它的直接孩子有余量时,它不允许滚动到其内容的最后。我相信这个问题是由于它在计算滚动区域时没有考虑到孩子的边际。 – 2013-05-09 10:06:27