2017-02-22 97 views
1

我使用CardView作爲RecyclerView的自定義項目。它們在Android 5+上看起來不錯,但在較舊的Android版本上卻很不一樣。Pre-Lollipop設備上的「醜陋」CardView

在Android上5 + enter image description here

在Android enter image description here

的代碼是一樣的:

<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:card_view="http://schemas.android.com/apk/res-auto" 
android:layout_width="match_parent" 
android:layout_height="wrap_content" 
android:minHeight="?android:attr/listPreferredItemHeight" 
card_view:cardCornerRadius="1dp" 
card_view:cardElevation="1dp"> 

... other items ... 

</android.support.v7.widget.CardView> 

有沒有辦法實現了Android 5+預行爲 - 棒棒糖設備?

回答

1

使用支持CardView?編號

就我個人而言,我認爲支持CardView是壞的,不應該使用。它看起來和Lollipop和舊系統有點不同。陰影是不同的,填充不同,內容剪輯不適用於前棒棒糖設備等。該API也是奇怪和混亂。這就是爲什麼在所有平臺上都很難取得好成績的原因。如果你可以沒有卡片生活,我會這樣。

當然可以創建一個自定義,漂亮,向後兼容的卡,但這有點複雜。要自己創建卡,必須執行以下操作:

  • 帶內容剪輯的圓角(在支持CardView中不起作用)。 Here's how to do it properly
  • 卡外的陰影(不在裏面,就像支持CardView一樣)。這個取決於你的需求。我會在父容器中覆蓋drawChild(...),在那裏我可以自由地在卡周圍繪製陰影。陰影生成方法並不重要 - 它可以是漸變,靜態9貼片或RenderScript - 黑色模糊。

我對CardView的外觀和API也感到沮喪,所以我創建了自己的實現。它可以在GitHub上找到 - 該庫名爲Carbon,並且使用它可能是獲得體面牌的最簡單方法。導入庫後,只需將style="?attr/carbon_cardViewStyle"添加到任何佈局以使其看起來像一張卡片即可:

<carbon.widget.RelativeLayout 
    style="?attr/carbon_cardViewStyle" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content"/>