利用gradle引進支持包:
compile 'com.android.support:cardview-v7:21.+'
因爲V7支持包將cardview支持包單獨拿出來了,所以不必擔心引入太多的類:
總共只有這些類,其中一些類還是爲了兼容性做了特別的處理,可以看到源碼中關於這些的處理:
private static final CardViewImpl IMPL;
static {
if (Build.VERSION.SDK_INT >= 21) {
IMPL = new CardViewApi21();
} else if (Build.VERSION.SDK_INT >= 17) {
IMPL = new CardViewJellybeanMr1();
} else {
IMPL = new CardViewEclairMr1();
}
IMPL.initStatic();
}
具體的可以進去看詳細的。
CardView如Linearlayout、Framelayout一樣都是ViewGroup,即其他控件的容器。CardView繼承於Framelayout,所以Framelayout的屬性他都有,同時CardView還有幾個特殊的屬性:
在API21(Android L)等級以上擁有屬性elevation,意爲CardView的Z軸陰影,只有L平臺有效。只能通過xml中的elevation屬性指定;
其餘(2.0以上)有屬性cardBackgroundColor,意爲CardView的卡片顏色,只能通過xml的cardBackgroundColor進行指定;
其餘(2.0以上)有屬性cardConerRadius,意爲CardView卡片的四角圓角矩形程度,單位dimen(dp px sp),可以通過xml指定,也可以通過代碼中的setRadius指定。
示例:
需在頭部引入:
xmlns:app="http://schemas.android.com/apk/res-auto"
<android.support.v7.widget.CardView
android:layout_width="200dip"
android:layout_height="34dip"
android:layout_marginTop="110dip"
android:layout_centerHorizontal="true"
android:gravity="center"
app:cardCornerRadius="5dp"
app:cardElevation="7dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="4dip"
android:layout_gravity="center"
android:text="測試"
android:textColor="#e8753e"
android:textSize="16sp" />
</android.support.v7.widget.CardView>
如果你的項目中採用了CardView請注意在低版本中多測試
1.注意4.2.2及以下版本、4.2.2版本以上版本、5.0版本;
2.我遇到的問題有:
a.高度在5.0版本及以下版本的顯示效果不一樣(要設置的足夠高,不然顯示不全,講上面的改爲54dp的高度在低版本上的顯示效果更好)
b.實現圓形的效果在5.0上的效果很好,在以下版本的效果會很差,簡直不能忍受。。。