12

我遇到了由包含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> 
+1

尝试设置'机器人:layout_height =“FILL_PARENT”'的'ScrollView'? – 2013-05-08 14:50:46

+0

@AdilSoomro是的,没有喜悦。 ScrollView的大小并不是一个真正的问题 - 它会适当地填充可用的空间。这是它提供的滚动区域 - 当它的直接孩子有余量时,它不允许滚动到其内容的最后。我相信这个问题是由于它在计算滚动区域时没有考虑到孩子的边际。 – 2013-05-09 10:06:27

回答

0

我摆脱FrameLayout的,使LinearLayoutScrollView的孩子match_parent集作为布局的高度。如果仍然不起作用,请考虑用另一个任意View替换底部边距,并将所需高度和背景作为LinearLayout中的最后一项。

+0

Match_parent必须位于scrollView中。并且scrollView的子元素必须有高度=“wrap_content”。 – 2013-05-08 15:10:00

+0

啊好点。后一种选择可能是最好的。 – Wenger 2013-05-08 16:40:04

+2

这些组合都不能解决问题。说实话,在LinearLayout的末尾添加一个额外的期望边距高度的视图要比将其包装在FrameLayout中要粗糙一些。为什么ScrollView以这种方式运行并忽略顶部和底部边距? – 2013-05-09 09:19:44

5

我记得ScrollView在内容布局有一个上边距时不知何故没有达到它的内容的末尾。我解决了这个问题用少许劈,添加一个空的视图到LinearLayout的末尾:

<?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="fill_parent" 
    tools:context=".ArticleFragment" > 

    <LinearLayout 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:orientation="vertical" 
     android:padding="10dp" 
     android:layout_margin="16dp" > 

     <TextView 
      android:id="@+id/article_title" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:textAppearance="?android:attr/textAppearanceLarge" 
      android:textIsSelectable="true" 
      android:background="@color/page_background" /> 

     <TextView 
      android:id="@+id/article_content" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:textIsSelectable="true" 
      android:background="@color/page_background" /> 


     <!-- Add a little space to the end --> 
     <View 
      android:layout_width="fill_parent" 
      android:layout_height="30dp" /> 

    </LinearLayout> 
</ScrollView> 

我使用了类似的空视图也在LinearLayout的开头,以避免顶部/底部边缘完全。

编辑:我只是意识到,你也希望“论文”的结束时显示出来,当达到视图的结尾。在这种情况下,您可能需要将背景颜色设置为TextView而不是布局本身。然后确保标题和文章之间没有空白(使用填充作为分隔)。

编辑2:我应该学会检查问题的提出时间......好吧,也许这仍然有助于某人。 :)

1

问题是旧的,但我已经有问题,当包装FrameLayout ScrollView时行为不端。它似乎也没有考虑包含布局的边际。您可以将FrameLayout替换为另一个应该正确测量的独子LinearLayout。我已经完全去除的FrameLayout和简单地添加填充总结保证金在内的布局:

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

     <LinearLayout 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:orientation="vertical" 
      android:padding="26dp" 
      android:background="@color/page_background" > 
<!--...-->