在使用CardVIew以前,要明白CardView是個什麼東西。CardView如Linearlayout、Framelayout同樣都是ViewGroup,即其餘控件的容器。CardView繼承於Framelayout,因此Framelayout的屬性他都有,同時CardView還有幾個特殊的屬性:android
在API21(Android L)等級以上擁有屬性elevation,意爲CardView的Z軸陰影,只有L平臺有效。只能經過xml中的elevation屬性指定;app
其他(2.0以上)有屬性cardBackgroundColor,意爲CardView的卡片顏色,只能經過xml的cardBackgroundColor進行指定;spa
其他(2.0以上)有屬性cardConerRadius,意爲CardView卡片的四角圓角矩形程度,單位dimen(dp px sp),能夠經過xml指定,也能夠經過代碼中的setRadius指定。code
<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" android:id="@+id/cardview" app:cardCornerRadius="8dp" app:cardBackgroundColor="@color/black" android:layout_margin="8dp" android:layout_height="80dp" android:layout_width="match_parent"> <TextView android:text="TextView in CardView" android:layout_gravity="center" android:textSize="26sp" android:textColor="@color/l_white" android:layout_width="wrap_content" android:layout_height="wrap_content" /> </android.support.v7.widget.CardView>
上面也提到CardView像一個FragmentLayout,因此他裏面的內容會有層級結構,你們要注意。而且與recyclerview聯合使用的話,會有不少意想不到的效果喲xml