簡介:ardView繼承自FrameLayout,它是一個帶圓角背景和陰影的FrameLayout
一些什么實現方式就不贅述了,直接上用法
CardView的常用屬性
1、設置背景顏色
app:cardBackgroundColor=" "
2、設置padding
app:contentPadding=" " app:contentPaddingTop=" " app:contentPaddingBottom=" " app:contentPaddingLeft=" " app:contentPaddingRight=" "
注意啊:上面是CardView設置背景顏色和padding的方式,如果你直接通過android:padding=" " 和android:background=" "設置,是無效的
3、設置Z軸的最大高度
app:cardMaxElevation=" "
4、點擊之后的漣漪效果
android:clickable="true" android:foreground="android:attr/selectableItemBackground" 注意:如果你給CardView設置了點擊事件,就不需要設置android:clickable="true"了
5.設置CardView的圓角半徑
app:cardCornerRadius =""
6設置CardView的前景
android:foreground =""
7設置內邊距
app:contentPadding ="
例如在項目中的用法
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
... >
<!-- A CardView that contains a TextView -->
<android.support.v7.widget.CardView
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:id="@+id/card_view"
android:layout_gravity="center"
android:layout_width="200dp"
android:layout_height="200dp"
card_view:cardCornerRadius="4dp">
<TextView
android:id="@+id/info_text"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</android.support.v7.widget.CardView>
</LinearLayout>
總結:注意這個控件在5.0之下會有包裹不均勻的現象,但是在5.0之上會很好用
可以控制這個屬性來適配app:cardPreventCormorOverlap
true:防止子View被CardView的圓角進行剪切 false: 允許剪切,主要用於適配Lollipop以下
by磊磊tua