2016-07-25 111 views
8

我将cardview放在滚动视图内,我们希望看到在底部应显示边框(请参见下图)。但不是。问题是我无法滚动到底部查看cardview的边框。CardView底部边框在ScrollView内切断android

SO上的所有解决方案都是将layout_margins更改为填充,但如果我们想显示边框,则不适用cardview。我基本上尝试了一切。但仍然没有工作。 scroll to bottom cannot see the border

图片1.滚动到底看不到边界

we can see the top border

图2我们可以看到,顶部边框

以下是XML代码

<LinearLayout 
xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:custom="http://schemas.android.com/apk/res-auto" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:orientation="vertical" 
> 
    <ScrollView 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:fillViewport="true"> 

      <android.support.v7.widget.CardView 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       android:layout_margin="8dp"> 
        <LinearLayout 
         android:layout_width="match_parent" 
         android:layout_height="wrap_content" 
         android:orientation="vertical" 
         > 
        ... 
        </LinearLayout> 
      </CardView> 
    </LinearLayout> 

引用: ScrollView doesn't scroll to the bottom

ScrollView cuts off the top and leaves space at the bottom

I can't show LinearLayout at bottom to scroll view

Android ScrollView refuses to scroll to bottom

+1

你可以附加一个屏幕截图,帮助我们更好地了解发生了什么事? – Vucko

+0

@Vucko添加了屏幕截图,谢谢 – Cheng

回答

4

一种解决方案我刚发现是包装CardView用的LinearLayout或RelativeLayout的并设置其填充。例如,如果您想在cardView中使用一些阴影效果,可以说8dp,则可以为CardView的LinearLayout或RelativeLayout和4dp layout_margin设置4dp填充。

+0

是的,但是添加一个额外的图层将需要额外的时间来遍历未优化的视图树。那么,如果你不关心性能,这应该工作 – Cheng

+0

我甚至没有设置填充。刚刚添加了线性布局,它解决了问题 –

9

我遇到了同样的问题,需要做以下(关键是围绕在我加入paddingBottom来的cardview中的LinearLayout包装):

<ScrollView 
    android:id="@+id/item_scrollview" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:scrollbars="none" 
    tools:visibility="visible"> 

    <LinearLayout 
     android:id="@+id/item_wrapper_layout" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_margin="@dimen/content_margin" 
     android:paddingBottom="32dp" 
     android:orientation="vertical"> 

     <android.support.v7.widget.CardView 
      android:id="@+id/item_cardview" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      card_view:cardBackgroundColor="@color/colorPrimary" 
      card_view:cardCornerRadius="4dp" 
      card_view:cardUseCompatPadding="true"> 

添加的LinearLayout包装各地cardview是什么为我工作。

另外请注意,我不得不添加card_view:cardUseCompatPadding =“真”在cardview得到边界阴影寻找正确的。

下面是最终结果,其中红色方框显示卡片视图展开和向上滚动时添加的填充位置。

screenshot

+0

我不是加入额外图层来包装卡片视图的忠实粉丝,它需要花费额外的时间来遍历视图树,特别是当视图很深的时候真的是如此。如果您不关心性能,那应该起作用 – Cheng

+0

将LinearLayout添加为包装不会导致此设计性能的巨大提升。如果这全部包含在RecyclerView中,那么我可以看到它可能是一个问题。像这样的观点不应该是那么深 - 尤其是在卡片上方。 –

+0

如果您看到上面的屏幕截图,则显然存在边距或填充问题。滚动条即使到达底部也不能滚动。所以修正这个填充/保证金问题是正确的做法。包装一个线性布局只是一种黑客方式,或恕我直言,它巧妙地工作。 – Cheng