標籤:
在使用CardVIew之前,要明白CardView是個什麼東西。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指定。
<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聯合使用的話,會有很多意想不到的效果喲
《Android應用開發:CardView的使用》