Android Meterial Design 之 CardView

  1. 簡(jiǎn)介 :
    CardView 是Android5.0推出來的新控件,同樣遵循Material Design 扁平化的風(fēng)格, 通常使用于卡片類型帶圓角的UI布局,起來立體感明顯,效果很好,由于是5.0推出的控件,為了兼容性,需要在app下 build.gradle下添加依賴


    compile 'com.android.support:cardview-v7:24.2.1

  2. 基本使用

<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ù)

  1. 拓展使用 屬性介紹
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)部的下間距
  1. 一些問題和一些實(shí)現(xiàn)
    1. 在API21(5.0) 以上,使用起來沒有問題,圖片:
    2. 但是在API21以下,在CardView 與內(nèi)部view會(huì)有默認(rèn)的邊距, 圖 , 原因是因?yàn)?在API21以下,為了防止內(nèi)容與CardView 重疊, 默認(rèn)使用cardPreventCornerOverlap =true 使會(huì)有默認(rèn)邊距 ,解決這個(gè)問題, 只需要添加代碼 cardPreventCornerOverlap =false 即可
    3. CardView設(shè)置不了與屏幕的間距, 只需要在CardView外面再套一層布局 再設(shè)置CardView的margin值即可.
    4. CardView繼承于FrameLayout 可以作為根布局來使用, 具有FrameLayout的一切特點(diǎn),但是要注意第三點(diǎn)提到的問題,同時(shí)也要注意子View的位置.
    5. 去除陰影 card_view:cardElevation="0dp" 即可
    6. 設(shè)置點(diǎn)擊水波紋效果 android:foreground="?attr/selectableItemBackground" 但是在5.0以下就沒有效果
    7. 實(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>
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容