Android CardView的使用詳解(和RecycView一樣5.0以後出現的控制元件)
阿新 • • 發佈:2018-12-12
一、引入CardView
CardView和
RecyclerView
一樣是Android5.0以後新出現的控制元件,伴隨著Material Design設計而誕生。
在AS新建專案中需要使用CardView元件,和RecyclerView一樣,需要在Gradle裡面提前加入CardView的
包,直接在Gradle中加入:
compile 'com.android.support:cardview-v7:25.3.1'
(CardView版本號與專案裡面V7版本號相同即可)
二、為何使用CardView CardView是繼承自FramLayout的一個佈局控制元件,官網裡對CardView的註釋為: A FrameLayout with a rounded corner background and shadow.(CardView為帶圓角和陰影的FramLayout) 以往,我們需要自定義Shape來實現圓角和陰影效果;現在,這些效果整合到了CardView的屬性裡。同時, CardView應該被使用在顯示層次性的內容時,在顯示列表或網格時更應該被選擇,因為這些邊緣可以使得使用者
三、CardView屬性
app:cardBackgroundColor 設定背景顏色 app:cardCornerRadius 設定圓角大小 app:cardElevation 設定z軸陰影高度 app:cardMaxElevation 設定z軸最大高度值
app:contentPadding 內容與邊距的間隔
app:contentPaddingLeft 內容與左邊的間隔
app:contentPaddingTop 內容與頂部的間隔
app:contentPaddingRight 內容與右邊的間隔
app:contentPaddingBottom 內容與底部的間隔
app:paddingStart 內容與邊距的間隔起始
app:paddingEnd 內容與邊距的間隔終止
app:cardUseCompatPadding 設定內邊距,在API21及以上版本和之前的版本仍舊具有一樣的計算方式
app:cardPreventConrerOverlap 在API20及以下版本中新增內邊距,這個屬性為了防止內容和邊角的重疊
注意:CardView中使用android:background設定背景顏色無效。
Ps:多說一句,有時候部分人為了突出這些屬性是在CardView中使用的,會在CardView內重新命名屬性名,
如:xmlns:card_view="http://schemas.android.com/apk/res-auto",那麼app就會變成card_view,但是
直接使用app不影響什麼,只是一個屬性字首名的變化
四、點選CardView出現波紋(Ripple)效果
在CardView佈局中加入:android:foreground=”?android:attr/selectableItemBackground”即可實現 點選CardView出現波紋效果.
二、為何使用CardView CardView是繼承自FramLayout的一個佈局控制元件,官網裡對CardView的註釋為: A FrameLayout with a rounded corner background and shadow.(CardView為帶圓角和陰影的FramLayout) 以往,我們需要自定義Shape來實現圓角和陰影效果;現在,這些效果整合到了CardView的屬性裡。同時, CardView應該被使用在顯示層次性的內容時,在顯示列表或網格時更應該被選擇,因為這些邊緣可以使得使用者
app:cardBackgroundColor 設定背景顏色 app:cardCornerRadius 設定圓角大小 app:cardElevation 設定z軸陰影高度 app:cardMaxElevation 設定z軸最大高度值
在CardView佈局中加入:android:foreground=”?android:attr/selectableItemBackground”即可實現 點選CardView出現波紋效果.
友情連結想了解更過CardView可以訪問下面網站:
http://yifeng.studio/2016/10/18/android-cardview/