簡(jiǎn)介 :
CardView 是Android5.0推出來的新控件,同樣遵循Material Design 扁平化的風(fēng)格, 通常使用于卡片類型帶圓角的UI布局,起來立體感明顯,效果很好,由于是5.0推出的控件,為了兼容性,需要在app下 build.gradle下添加依賴
compile 'com.android.support:cardview-v7:24.2.1基本使用
<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:cardview="http://schemas.android.com/apk/res-auto"
cardview:cardElevation="5dp"
cardview:cardCornerRadius="6dp"
android:layout_width="match_parent"
android:layout_height="match_parent">
</android.support.v7.widget.CardView>
cardview:cardElevation 表示cardView 的陰影的 高度
cardview:cardCornerRadius 表示cardView 的圓角的邊緣弧度數(shù)
- 拓展使用 屬性介紹
cardBackgroundColor : 設(shè)置CardView的背景顏色
cardMaxElevation : 設(shè)置最大高度
cardUseCompatPadding :設(shè)置內(nèi)邊距
cardPreventCornerOverlap : 是否添加內(nèi)邊距,為了防止卡片內(nèi)容和邊角的重疊
contentPadding : 設(shè)置CardView邊界跟內(nèi)部的間距
contentPaddingLeft :設(shè)置CardView邊界跟內(nèi)部的左間距
contentPaddingRight:設(shè)置CardView邊界跟內(nèi)部的右間距
contentPaddingTop:設(shè)置CardView邊界跟內(nèi)部的上間距
contentPaddingBottom:設(shè)置CardView邊界跟內(nèi)部的下間距
-
一些問題和一些實(shí)現(xiàn)
- 在API21(5.0) 以上,使用起來沒有問題,圖片:
- 但是在API21以下,在CardView 與內(nèi)部view會(huì)有默認(rèn)的邊距, 圖 , 原因是因?yàn)?在API21以下,為了防止內(nèi)容與CardView 重疊, 默認(rèn)使用cardPreventCornerOverlap =true 使會(huì)有默認(rèn)邊距 ,解決這個(gè)問題, 只需要添加代碼
cardPreventCornerOverlap =false即可 - CardView設(shè)置不了與屏幕的間距, 只需要在CardView外面再套一層布局 再設(shè)置CardView的margin值即可.
- CardView繼承于FrameLayout 可以作為根布局來使用, 具有FrameLayout的一切特點(diǎn),但是要注意第三點(diǎn)提到的問題,同時(shí)也要注意子View的位置.
- 去除陰影
card_view:cardElevation="0dp"即可 - 設(shè)置點(diǎn)擊水波紋效果
android:foreground="?attr/selectableItemBackground"但是在5.0以下就沒有效果 - 實(shí)現(xiàn)Material Design 點(diǎn)擊陰影效果 需要借助5.0的屬性
android:stateListAnimator, 5.0以下沒有效果
創(chuàng)建一個(gè)Z軸方向的動(dòng)畫,設(shè)置屬性為android:stateListAnimator="@anim/xxx
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_enabled="true" android:state_pressed="true">
<objectAnimator
android:duration="@android:integer/config_shortAnimTime"
android:propertyName="translationZ"
android:valueTo="@dimen/touch_raise"
android:valueType="floatType" />
</item>
<item>
<objectAnimator
android:duration="@android:integer/config_shortAnimTime"
android:propertyName="translationZ"
android:valueTo="0dp"
android:valueType="floatType" />
</item>
</selector>