Android Material Design 控件 CardView

  • 開始使用
compile 'com.android.support:cardview-v7:25.3.1'
  • 常用屬性

app:cardBackgroundColor    背景顏色
app:cardElevation  Z軸陰影高度
app:cardMaxElevation   Z軸的陰影最大高度
app:cardUseCompatPadding  是否在不同系統中使用相同的padding值
app:cardPreventCornerOverlap   是否阻止圓角被覆蓋,默認為true,為了防止卡片內容和邊角的重疊
app:cardCornerRadius  圓角半徑


//點擊效果
android:clickable="true" //設置了點擊事件可省略
android:foreground="?android:attr/selectableItemBackground"


app:contentPadding   邊距
app:contentPaddingLeft   左邊距
app:contentPaddingRight  右邊距
app:contentPaddingTop  上邊距
app:contentPaddingBottom   下邊距
app:contentPaddingStart
app:contentPaddingEnd
  • 注意問題
  1. CardView設置不了與屏幕的間距, 只需要在CardView外面再套一層布局 再設置CardView的margin值
    2.api 5.0以下,使用一個兼容方案實現點擊陰影效果( 另見下方第二個參考文檔詳解)
創建一個Z軸方向的動畫,設置屬性為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>
最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容