2017-07-25 135 views
0

我在我的应用程序中有一个活动,我希望用户能够垂直滚动LinearLayout中包含的内容,而该内容又位于ScrollView之内。这里是什么这项活动布局XML看起来像一个总结:LinearLayout在ScrollView Android中被切断

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 

    <ScrollView 
     android:layout_width="fill_parent" 
     android:layout_height="match_parent"> 
     <LinearLayout 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:layout_margin="20dip" 
      android:orientation="vertical"> 

      <!-- a bunch of standard widgets, omitted for brevity --> 

      <!-- everything renders but starting in this TextView --> 
      <!-- content begins to get truncated --> 
      <TextView 
       android:layout_width="fill_parent" 
       android:layout_height="wrap_content" 
       android:paddingLeft="20dp" 
       android:gravity="left" 
       android:textSize="20dip"/> 

      <!-- this View, really just a trick for a horizontal row, is --> 
      <!-- completely cutoff --> 
      <View 
       android:layout_width="fill_parent" 
       android:layout_height="2dip" 
       android:layout_marginTop="10dp" 
       android:layout_marginBottom="10dp" 
       android:background="@color/green" /> 
     </LinearLayout> 
    </ScrollView> 
</LinearLayout> 

我所观察是在内部LinearLayout内容正被该ScrollView内侧截止。在上面的最后TextView中,当它包含很少的文本时,那么它下面的View会以纵向渲染,但不会横向渲染。当这个TextView包含大量文本时,它会在纵向模式下截断。

我尝试了我在Stack Overflow上找到的建议。将底部填充添加到ScrollView不能解决问题,也不会将ScrollView替换为NestedScrollView

任何有用的建议将受到欢迎。这实际上是一个阻碍者。

+0

添加屏幕截图 –

+0

@OussemaAroua除非绝对必要,否则我宁愿不分享屏幕截图。你为什么认为这会有所帮助?基本上你会看到我在问题中描述的内容,除了底部的一些东西正在被切断。我希望你能看到我看不到的缺陷。 –

+0

尝试将一些内部视图高度设置为match_parent,例如LinearLayout的 – stan0

回答

6

将您的内部LinearLayout的边距改为填充。或者,如果您真的需要它作为保证金(也许您使用的是自定义背景),请将您的LinearLayout换成FrameLayout

ScrollView正在从LinearLayout其高度(或更准确地说,它正在计算它的可滚动范围)。此值不包括保证金,因此您的ScrollView将由“LinearLayout”的上下保证金总和“过短”。

+0

换句话说,ScrollView被分配了一个太短的高度,然后当它实际查看它的LinearLayout时,它停止缩短并截断顶部和底部边距的总和,我的情况是40dp。 –

+0

我相信它就像CSS盒子模型一样简单;填充是视图尺寸的一部分,边距不是。看看'ScrollView.canScroll()'...它比较'ScrollView'的高度和第一个孩子的身高。 –

+0

感谢您的帮助,您创造了我的一天。我很惊讶,我在这上面找不到任何东西......也许我正在寻找错误的东西。希望你的回答能够帮助另一个疲惫的Android开发者:-) –

0

边距被忽略,而测量,见this

,这样可以提供填充到您的ScrollView并从LinearLayout

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:orientation="vertical" 
android:layout_width="match_parent" 
android:layout_height="match_parent"> 

<ScrollView 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:padding="20dp"> 
    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:orientation="vertical"> 

     <!-- a bunch of standard widgets, omitted for brevity --> 

     <!-- everything renders but starting in this TextView --> 
     <!-- content begins to get truncated --> 
     <TextView 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:paddingLeft="20dp" 
      android:gravity="left" 
      android:textSize="20dip"/> 

     <!-- this View, really just a trick for a horizontal row, is --> 
     <!-- completely cutoff --> 
     <View 
      android:layout_width="match_parent" 
      android:layout_height="2dip" 
      android:layout_marginTop="10dp" 
      android:layout_marginBottom="10dp" 
      android:background="@color/green" /> 
    </LinearLayout> 
</ScrollView> 

此外fill_parent去除利润率已被弃用,并更名为match_parent API等级8以上

+1

在滚动视图上使用填充会产生一些不一定令人失望的后果。首先是内容将被填充剪辑(尽管你可以通过android:clipToPadding =“false”来解决这个问题)。另一个(我不知道如何解决)是滚动条将在填充内。 –

+0

是的,我同意,滚动条将在填充内。感谢您指出了这一点 –