2016-07-25 64 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