一、簡(jiǎn)介
- CardView 是卡片式布局的控件,繼承自 FrameLayout ,不過(guò)提供了一些屬性,如圓角、陰影效果等,使其具有立體感,有種卡片的感覺(jué)。
二、使用
使用比較簡(jiǎn)單,可以當(dāng)成 FrameLayout 使用,不過(guò)需要添加一些立體感的屬性
需要添加依賴:
//版本與 V7 包一致
compile 'com.android.support:cardview-v7:25.3.1'
2.1 在 xml 中簡(jiǎn)單使用如下所示:
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:cardBackgroundColor="@color/colorAccent"
app:cardCornerRadius="5dp"
app:cardElevation="10dp">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageView
android:id="@+id/card_image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:src="@mipmap/ic_launcher" />
<TextView
android:id="@+id/card_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:layout_marginLeft="50dp"
android:layout_toRightOf="@id/card_image"
android:text="珞神"
android:textColor="#fff"
android:textSize="30dp" />
</RelativeLayout>
</android.support.v7.widget.CardView>
由于 CardView 本身是個(gè) FrameLayout ,所以指定一個(gè) RelativeLayout 作為它的布局方式。
app:cardBackgroundColor="@color/colorAccent" 設(shè)置背景色
app:cardCornerRadius="5dp" 設(shè)置圓角弧度半徑,值越大,圓角越大
app:cardElevation="10dp" 設(shè)置陰影高度,值越大,陰影效果越明顯
三、 CardView 的其他用途
- CardView 單獨(dú)作為布局用的很少,一般是作為 ListView 或者 RecyclerView 的條目填充布局使用。
3.1 在配合 RecyclerView 使用的時(shí)候,Android5.0 以前版本和以后版本的顯示效果不同,需要做 demins 適配,先看下效果。
- RecyclerView 的 item 布局如下:
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:cardCornerRadius="5dp"
android:foreground="?android:attr/selectableItemBackground"
app:cardBackgroundColor="@color/colorAccent"
app:cardElevation="10dp">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageView
android:id="@+id/card_image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:src="@mipmap/ic_launcher" />
<TextView
android:id="@+id/card_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:layout_marginLeft="50dp"
android:layout_toRightOf="@id/card_image"
android:text="珞神"
android:textColor="#fff"
android:textSize="30dp" />
</RelativeLayout>
</android.support.v7.widget.CardView>
- Android 7.0 手機(jī)顯示如下:

image
- Android 4.4 手機(jī)顯示如下:

image
可以看到,4.4 每個(gè)條目之間有很大的空隙,供陰影顯示,而 7.0 是僅僅的擠在一塊,如果設(shè)置 margin 屬性增大空隙后,4.4 上的空隙會(huì)繼續(xù)加大
解決辦法: dimens 適配,創(chuàng)建一個(gè) value-v21 文件夾,在其下創(chuàng)建一個(gè) dimens.xml 文件,分別設(shè)置不同的 dp 值。

image
- values 中的 dimens.xml如下:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<dimen name="cardview_margin" >0dp</dimen>
<dimen name="cardview_margin_left_right" >0dp</dimen>
</resources>
- values-v21 中的 dimens.xml如下:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<dimen name="cardview_margin" >14dp</dimen>
<dimen name="cardview_margin_left_right" >10dp</dimen>
</resources>
- 然后在 Cardv 中添加 margin 屬性
android:layout_marginBottom="@dimen/cardview_margin"
android:layout_marginTop="@dimen/cardview_margin"
android:layout_marginLeft="@dimen/cardview_margin_left_right"
android:layout_marginRight="@dimen/cardview_margin_left_right"
- 系統(tǒng)會(huì)自動(dòng)選擇對(duì)應(yīng)的值,這樣就完成了 dimens 適配
注意: CardView 官方不建議 app 中多處使用,只有在一些比較突出的地方使用即可。
四、CardView 添加水波紋點(diǎn)擊效果
- 需要在 CardView 下添加如下代碼:
android:clickable="true"
android:foreground="?android:attr/selectableItemBackground"
- 不過(guò)在 5.0 以上顯示效果正常,在 5.0 以下顯示為 灰色陰影加深或其他的效果。