從Android5.0開始,google引入了Material Design,那么我們今天就來講講CardView
首先要明白CardView能夠帶來哪些效果:
1、四周陰影效果
2、四周圓角效果
下面看張圖片:
要想實現如上cardView顯示效果,分以下步驟:
1、引入庫,在android studio中導入v7兼容包:
compile'com.android.support:cardview-v7:23.1.1'//cardview
2、在布局文件中加入cardview
注意CardView和Scroview一樣僅允許有一個孩子view,你懂得
3、注意到上面有兩個屬性
app:cardElevation 和? card_view:cardBackgroundColor,使用這兩個屬性時需要在xml中引入以下schemas:
4、關于各個屬性介紹
android:cardCornerRadius ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?在xml文件中設置card圓角的大小
CardView.setRadius ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?在代碼中設置card圓角的大小
android:cardBackgroundColor ? ? ? ? ? ? ? ? ? ? ? ? ? ?在xml文件中設置card背景顏色
android:elevation ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ??在xml文件中設置陰影的大小
card_view:cardElevation ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ??在xml文件中設置陰影的大小
card_view:cardMaxElevation ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?在xml文件中設置陰影最大高度
card_view:cardCornerRadius ? ? ? ? ? ? ? ? ? ? ? ? ? ? ??在xml文件中設置卡片的圓角大小
card_view:contentPadding ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?在xml文件中設置卡片內容于邊距的間隔
card_view:contentPaddingBottom ? ? ? ? ? ? ? ? ? ? ??在xml文件中設置卡片內容于下邊距的間隔
card_view:contentPaddingTop ? ? ? ? ? ? ? ? ? ? ? ? ? ??在xml文件中設置卡片內容于上邊距的間隔
card_view:contentPaddingLeft ? ? ? ? ? ? ? ? ? ? ? ? ? ??在xml文件中設置卡片內容于左邊距的間隔
card_view:contentPaddingRight ? ? ? ? ? ? ? ? ? ? ? ? ?在xml文件中設置卡片內容于右邊距的間隔
card_view:contentPaddingStart ? ? ? ? ? ? ? ? ? ? ? ? ??在xml文件中設置卡片內容于邊距的間隔起始
card_view:contentPaddingEnd ? ? ? ? ? ? ? ? ? ? ? ? ??在xml文件中設置卡片內容于邊距的間隔終止
card_view:cardUseCompatPadding ? ? ? ? ? ? ? ? ?在xml文件中設置內邊距,V21+的版本和之前的版本仍舊具有一樣的計算方式
card_view:cardPreventConrerOverlap ? ? ? ? ? ? ??在xml文件中設置內邊距,在V20和之前的版本中添加內邊距,這個屬性為了防止內容和邊角的重疊