Material Design系列教程(12) - CardView

簡(jiǎn)介

CardViewAndroid 5.0 引進(jìn)的一種卡片式布局控件椅野,是一個(gè)帶有圓角和陰影效果的FrameLayout螃概。

CardView

CardView 的文檔中可以看到掰读,它是存在于 support.v7 包中的扁凛,因此引進(jìn)方式為:

implementation 'com.android.support:cardview-v7:<version>'

CardView 具備 材料設(shè)計(jì)特性病附,陰影(Elevation)Z軸位移,目的就是突出不同元素之間的層次關(guān)系峰髓,看起來有立體的感覺傻寂。因此其使用場(chǎng)景一般是用在需要凸顯層次性內(nèi)容布局,比如在顯示列表或網(wǎng)格布局時(shí)携兵,使用 CardView 就可以方便用戶依據(jù)這些邊緣特征更容易去區(qū)分內(nèi)容主體疾掰。

CardView 屬性簡(jiǎn)析

下面列舉一些 CardView屬性特性:

屬性 釋義
CardView_cardBackgroundColor 設(shè)置背景顏色
CardView_cardCornerRadius 設(shè)置圓角大小
CardView_cardElevation 設(shè)置z軸的陰影
CardView_cardMaxElevation 設(shè)置z軸陰影的最大高度值
CardView_contentPadding 設(shè)置內(nèi)容的內(nèi)邊距
CardView_contentPaddingBottom 設(shè)置內(nèi)容的底內(nèi)邊距
CardView_contentPaddingLeft 設(shè)置內(nèi)容的左內(nèi)邊距
CardView_contentPaddingRight 設(shè)置內(nèi)容的右內(nèi)邊距
CardView_contentPaddingTop 設(shè)置內(nèi)容的上內(nèi)邊距
CardView_cardUseCompatPadding 是否使用CompatPadding
CardView_cardPreventCornerOverlap 是否取消圓角覆蓋

這里著重對(duì) CardView 的兩個(gè)屬性講解一下:

  • CardView_cardUseCompatPadding - 陰影內(nèi)邊距
    如果你為 CardView 指定了具體的大小,在 Android Lollipop 之前的系統(tǒng)徐紧,CardView 會(huì)自動(dòng)添加一些額外的padding空間來繪制陰影部分静檬,導(dǎo)致內(nèi)容元素在 Lollipop 之前與之后的系統(tǒng)中大小可能不同炭懊。
    要解決這個(gè)問題,一個(gè)方法是使用不同 AP I版本的 dimension 資源適配(也就是借助 valuesvalues-21 文件夾中不同的 dimens.xml 文件)拂檩,或者侮腹,如果你想要為 CardViewLollipop 及其之后的系統(tǒng)上同樣增加一個(gè)內(nèi)邊距,可以通過設(shè)置CardView_cardUseCompatPadding="true"(其默認(rèn)值為false)广恢。

  • CardView_cardPreventCornerOverlap - 圓角覆蓋
    在 pre-Lollipop 平臺(tái)(API 21版本)之前凯旋,CardView 不會(huì)裁剪內(nèi)容元素以滿足圓角需求,而是增加一個(gè)padding钉迷,從而使內(nèi)容元素不會(huì)覆蓋 CardView 的圓角至非。而控制這個(gè)行為的屬性就是cardPreventCornerOverlap,默認(rèn)值為true糠聪。
    :該屬性在 Lollipop 及以上版本的系統(tǒng)中沒有任何影響荒椭,除非設(shè)置了cardUseCompatPadding="true"。通常為了兼容低版本舰蟆,讓 CardView 在所有系統(tǒng)版本表現(xiàn)一致趣惠,則需要把CardView_cardPreventCornerOverlap設(shè)置為false,取消(低版本)自動(dòng)添加padding效果身害。但是這樣設(shè)置后味悄,內(nèi)容元素與 CardView 的圓角就會(huì)重疊到一起,導(dǎo)致圓角消失(被內(nèi)容元素覆蓋)塌鸯。因此侍瑟,如果還想要有圓角效果,考慮對(duì)內(nèi)容元素進(jìn)行圓角裁剪丙猬。

綜上涨颜,為了系統(tǒng)兼容,使得高低版本系統(tǒng)中茧球,CardView 的表現(xiàn)具備一致性庭瑰,通常要添加如下兩個(gè)配置:

//為 Lollipop 及其以上版本增加一個(gè)陰影padding
CardView_cardUseCompatPadding="true"
//取消 Lollipop 以下版本的padding
CardView_cardPreventCornerOverlap="false"

其他注意事項(xiàng)

  • CardView 無法使用android:background設(shè)置背景,可以通過app:cardBackgroundColor屬性進(jìn)行設(shè)置抢埋。

  • 在 Android Lollipop之前的系統(tǒng)中弹灭,CardView 自動(dòng)為內(nèi)容元素添加一個(gè)padding作為陰影效果。這個(gè)padding的大小為:
    ? 左右內(nèi)邊距:maxCardElevation + (1 - cos45) * cornerRadius
    ?上下內(nèi)邊距:maxCardElevation * 1.5 + (1 - cos45) * cornerRadius

由于padding已被用作陰影效果揪垄,因此鲤屡,你無法使用android:paddingCardView 增加一個(gè)內(nèi)邊距,而是應(yīng)當(dāng)通過配置XML方式:CardView_contentPaddingXXX或者代碼調(diào)用setContentPadding(int,int,int,int)進(jìn)行設(shè)置福侈。

CardView 使用

首先給出 CardView 最簡(jiǎn)布局:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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="match_parent"
    android:gravity="center"
    android:orientation="vertical">

    <android.support.v7.widget.CardView
        android:layout_width="300dp"
        android:layout_height="300dp"
        app:cardPreventCornerOverlap="false"
        app:cardUseCompatPadding="true">

        <TextView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="#FFFF00"
            android:gravity="center"
            android:text="I am in CardView"
            android:textSize="20sp" />
    </android.support.v7.widget.CardView>

</LinearLayout>

效果如下:

CardView

因?yàn)闆]有對(duì) CardView 進(jìn)行特殊設(shè)置,這里它表現(xiàn)出來的就是一個(gè)ViewGroup的效果卢未,只能用來容納子控件肪凛。

下面我們?yōu)?CardView 增加一些屬性設(shè)置堰汉,來看下效果:

  • 圓角效果:app:cardCornerRadius="10dp",效果如下:
CardView_radius
  • 陰影效果:app:cardElevation="10dp"伟墙,效果如下:
CardView_elevation
  • 漣漪效果(Ripple):
android:clickable="true"
android:foreground="?attr/selectableItemBackground"

漣漪效果需要點(diǎn)擊產(chǎn)生翘鸭,因此這里首先要求 Cards 是可點(diǎn)擊的(clickable="true")。

效果如下圖所示:

CardView_ripple

漣漪效果(Ripple)只在 Android 5.0 之后有效戳葵,在pre-Lollipop版本中就乓,則是一個(gè)普通的點(diǎn)擊變暗的效果。

如果想同時(shí)實(shí)現(xiàn)高低版本的漣漪效果(Ripple)拱烁,可以通過自定義 CardView 前景:具體原理是生蚁,在 Android 5.0 及其之后版本,直接使用ripple即可戏自。在 Android 5.0 之前邦投,沒有ripple,則采用inset進(jìn)行代替擅笔。

具體做法如下:

  • Android 5.0 及其之后版本
  1. 首先創(chuàng)建一個(gè) selector志衣,位置:drawable-v21/card_foreground_selector.xml
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_pressed="true">
        <shape android:shape="rectangle">
            <solid android:color="#18ffc400"/>
        </shape>
    </item>
    <item android:state_focused="true" android:state_enabled="true">
        <shape android:shape="rectangle">
            <solid android:color="#0f000000"/>
        </shape>
    </item>
</selector>
  1. 然后創(chuàng)建一個(gè)ripple,引用上面的 selector猛们,位置:drawable-v21/card_foreground.xml
<ripple xmlns:android="http://schemas.android.com/apk/res/android" 
android:color="#20000000"
android:drawable="@drawable/card_foreground_selector" />
  • Android 5.0 之前版本
  1. 同樣首先創(chuàng)建一個(gè) selector念脯,位置:drawable/card_foreground_selector.xml
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_pressed="true">
        <shape android:shape="rectangle">
            <solid android:color="#1838f508"/>
            <corners android:radius="@dimen/card_radius" />
        </shape>
    </item>
    <item android:state_focused="true" android:state_enabled="true">
        <shape android:shape="rectangle">
            <solid android:color="#0f000000"/>
            <corners android:radius="@dimen/card_radius" />
        </shape>
    </item>
</selector>
  1. 然后創(chuàng)建一個(gè)inset,引用上面的 selector弯淘,位置:drawable/card_foreground.xml
<inset xmlns:android="http://schemas.android.com/apk/res/android" 
    android:drawable="@drawable/card_foreground_selector"
    android:insetLeft="2dp"
    android:insetRight="2dp"
    android:insetTop="4dp"
    android:insetBottom="4dp"/>
  • 動(dòng)畫效果:根據(jù)官網(wǎng) Material motion 部分對(duì)交互動(dòng)作規(guī)范的指導(dǎo)绿店,Cards、Button 等視圖應(yīng)該有一個(gè)觸摸抬起( lift-on-touch)的交互效果耳胎,也就是在三維立體空間上的Z軸發(fā)生位移惯吕,從而產(chǎn)生一個(gè)陰影加深的效果,與Ripple效果共同使用怕午。
    要實(shí)現(xiàn) lift-on-touch 動(dòng)畫效果废登,首先需要設(shè)置一個(gè) selector,位于:res/drawable
<?xml version="1.0" encoding="utf-8"?>
<!-- animate the translationZ property of a view when pressed -->
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item
        android:state_enabled="true"
        android:state_pressed="true">
        <set>
            <objectAnimator
                android:duration="@android:integer/config_shortAnimTime"
                android:propertyName="translationZ"
                android:valueTo="6dp"
                android:valueType="floatType"/>
        </set>
    </item>
    <item>
        <set>
            <objectAnimator
                android:duration="@android:integer/config_shortAnimTime"
                android:propertyName="translationZ"
                android:valueTo="0"
                android:valueType="floatType"/>
        </set>
    </item>
</selector>

其實(shí)就是通過屬性動(dòng)畫動(dòng)態(tài)改變translationZ值郁惜,沿著Z軸堡距,從0dp到6dp變化。

最后通過android:stateListAnimator="@drawable/lift_on_touch"設(shè)置給 CardView兆蕉。

效果如下:

lift_on_touch

總結(jié)

一般使用 CardView 時(shí)羽戒,通常都會(huì)設(shè)置其漣漪效果(Ripple),lift_on_touch 動(dòng)畫效果以及高低版本系統(tǒng)兼容性等等虎韵,這些基本上算是標(biāo)準(zhǔn)配置屬性易稠,那么我們就可以將這些屬性配置放入到一個(gè) style 中,直接提供給 CardView 使用即可包蓝。

    <style name="AppCardView" parent="@style/CardView.Light">
        <item name="cardPreventCornerOverlap">false</item>
        <item name="cardUseCompatPadding">true</item>
        <item name="android:clickable">true</item>
        <item name="android:foreground">?attr/selectableItemBackground</item>
        <item name="android:stateListAnimator" tools:targetApi="lollipop">@drawable/lift_on_touch</item>
    </style>

最后通過style="@style/AppCardView"設(shè)置給 CardView 即可驶社。

示例

最后結(jié)合一個(gè)例子企量,來看下 CardView 的顯示效果。

例子:使用 RecyclerView 結(jié)合 CardView 進(jìn)行圖片展示亡电,效果如下所示:

參考

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末届巩,一起剝皮案震驚了整個(gè)濱河市,隨后出現(xiàn)的幾起案子份乒,更是在濱河造成了極大的恐慌恕汇,老刑警劉巖,帶你破解...
    沈念sama閱讀 216,919評(píng)論 6 502
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件或辖,死亡現(xiàn)場(chǎng)離奇詭異瘾英,居然都是意外死亡,警方通過查閱死者的電腦和手機(jī)孝凌,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,567評(píng)論 3 392
  • 文/潘曉璐 我一進(jìn)店門方咆,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人蟀架,你說我怎么就攤上這事瓣赂。” “怎么了片拍?”我有些...
    開封第一講書人閱讀 163,316評(píng)論 0 353
  • 文/不壞的土叔 我叫張陵煌集,是天一觀的道長(zhǎng)。 經(jīng)常有香客問我捌省,道長(zhǎng)苫纤,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 58,294評(píng)論 1 292
  • 正文 為了忘掉前任纲缓,我火速辦了婚禮卷拘,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘祝高。我一直安慰自己栗弟,他們只是感情好,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,318評(píng)論 6 390
  • 文/花漫 我一把揭開白布工闺。 她就那樣靜靜地躺著乍赫,像睡著了一般。 火紅的嫁衣襯著肌膚如雪陆蟆。 梳的紋絲不亂的頭發(fā)上雷厂,一...
    開封第一講書人閱讀 51,245評(píng)論 1 299
  • 那天,我揣著相機(jī)與錄音叠殷,去河邊找鬼改鲫。 笑死,一個(gè)胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的钩杰。 我是一名探鬼主播纫塌,決...
    沈念sama閱讀 40,120評(píng)論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼,長(zhǎng)吁一口氣:“原來是場(chǎng)噩夢(mèng)啊……” “哼讲弄!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起依痊,我...
    開封第一講書人閱讀 38,964評(píng)論 0 275
  • 序言:老撾萬榮一對(duì)情侶失蹤避除,失蹤者是張志新(化名)和其女友劉穎,沒想到半個(gè)月后胸嘁,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體瓶摆,經(jīng)...
    沈念sama閱讀 45,376評(píng)論 1 313
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,592評(píng)論 2 333
  • 正文 我和宋清朗相戀三年性宏,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了群井。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點(diǎn)故事閱讀 39,764評(píng)論 1 348
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡毫胜,死狀恐怖书斜,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情酵使,我是刑警寧澤荐吉,帶...
    沈念sama閱讀 35,460評(píng)論 5 344
  • 正文 年R本政府宣布,位于F島的核電站口渔,受9級(jí)特大地震影響样屠,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜缺脉,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,070評(píng)論 3 327
  • 文/蒙蒙 一痪欲、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧攻礼,春花似錦业踢、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,697評(píng)論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至深员,卻和暖如春负蠕,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背倦畅。 一陣腳步聲響...
    開封第一講書人閱讀 32,846評(píng)論 1 269
  • 我被黑心中介騙來泰國(guó)打工遮糖, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留,地道東北人叠赐。 一個(gè)月前我還...
    沈念sama閱讀 47,819評(píng)論 2 370
  • 正文 我出身青樓欲账,卻偏偏與公主長(zhǎng)得像屡江,于是被迫代替她去往敵國(guó)和親。 傳聞我的和親對(duì)象是個(gè)殘疾皇子赛不,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,665評(píng)論 2 354

推薦閱讀更多精彩內(nèi)容