CardView之MD風格卡片式布局使用

介紹

??CardView 是 Google 官方發(fā)布 MD 風格卡片布局控件速兔,開發(fā)者可以很方便的使用它將布局做成卡片效果。CardView 繼承自 FrameLayout活玲,并在其基礎上添加了圓角和陰影等效果涣狗。也就是說,你只需要在xml文件中一行代碼就可以實現圓角+陰影效果了舒憾,這會為你的UI增色不少镀钓,快學習運用起來吧!

效果預覽


控件常用屬性

  • 設置卡片圓角大卸朴亍:

??app:cardCornerRadius="10dp"

  • 設置卡片Z軸高度:高度越高則陰影效果越重

??app:cardElevation="10dp"

  • 設置卡片背景色:

??app:cardBackgroundColor="@color/blue"
??Tips:設置android:background="@color/blue"不會起作用丁溅。

  • 設置卡片內邊距:

??app:contentPadding="1dp"

  • 設置卡片內部上側邊距:(其他類似)

??app:contentPaddingTop="1dp"


布局文件(具體如何使用?)

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".blog.Case50"
    tools:ignore="MissingConstraints">

    <TextView
        android:id="@+id/title"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:background="@color/green"
        android:gravity="center"
        android:text="CardView卡片式布局(圓角+陰影效果)"
        android:textColor="@color/white"
        android:textSize="20sp" />

    <androidx.cardview.widget.CardView
        android:id="@+id/cardView1"
        android:layout_width="300dp"
        android:layout_height="150dp"
        app:cardBackgroundColor="@color/blue"
        app:layout_constraintBottom_toTopOf="@id/cardView2"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/title">

        <TextView
            android:id="@+id/title1"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:gravity="center"
            android:text="CardView示例1:默認效果(圓角+陰影)"
            android:textColor="@color/white"
            android:textSize="20sp" />
    </androidx.cardview.widget.CardView>

    <androidx.cardview.widget.CardView
        android:id="@+id/cardView2"
        android:layout_width="300dp"
        android:layout_height="150dp"
        app:cardBackgroundColor="@color/blue"
        app:cardCornerRadius="10dp"
        app:layout_constraintBottom_toTopOf="@+id/cardView3"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toBottomOf="@id/cardView1">

        <TextView
            android:id="@+id/title2"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:gravity="center"
            android:text="CardView示例2:指定圓角效果"
            android:textColor="@color/white"
            android:textSize="20sp" />
    </androidx.cardview.widget.CardView>

    <androidx.cardview.widget.CardView
        android:id="@+id/cardView3"
        android:layout_width="300dp"
        android:layout_height="150dp"
        app:cardCornerRadius="10dp"
        app:cardBackgroundColor="@color/blue"
        app:cardElevation="10dp"
        app:cardMaxElevation="20dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toBottomOf="@id/cardView2">

        <TextView
            android:id="@+id/title3"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:gravity="center"
            android:text="CardView示例3:指定圓角+指定陰影高度"
            android:textColor="@color/white"
            android:textSize="20sp" />
    </androidx.cardview.widget.CardView>
</androidx.constraintlayout.widget.ConstraintLayout>

大功告成探遵!另一篇會將RecyclerView跟CardView結合起來使用窟赏,實現超級好看的UI布局。

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
  • 序言:七十年代末箱季,一起剝皮案震驚了整個濱河市涯穷,隨后出現的幾起案子,更是在濱河造成了極大的恐慌规哪,老刑警劉巖求豫,帶你破解...
    沈念sama閱讀 217,657評論 6 505
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件塌衰,死亡現場離奇詭異诉稍,居然都是意外死亡蝠嘉,警方通過查閱死者的電腦和手機,發(fā)現死者居然都...
    沈念sama閱讀 92,889評論 3 394
  • 文/潘曉璐 我一進店門杯巨,熙熙樓的掌柜王于貴愁眉苦臉地迎上來蚤告,“玉大人,你說我怎么就攤上這事服爷《徘。” “怎么了?”我有些...
    開封第一講書人閱讀 164,057評論 0 354
  • 文/不壞的土叔 我叫張陵仍源,是天一觀的道長心褐。 經常有香客問我,道長笼踩,這世上最難降的妖魔是什么逗爹? 我笑而不...
    開封第一講書人閱讀 58,509評論 1 293
  • 正文 為了忘掉前任,我火速辦了婚禮嚎于,結果婚禮上掘而,老公的妹妹穿的比我還像新娘。我一直安慰自己于购,他們只是感情好袍睡,可當我...
    茶點故事閱讀 67,562評論 6 392
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著肋僧,像睡著了一般斑胜。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上嫌吠,一...
    開封第一講書人閱讀 51,443評論 1 302
  • 那天伪窖,我揣著相機與錄音,去河邊找鬼居兆。 笑死覆山,一個胖子當著我的面吹牛,可吹牛的內容都是我干的泥栖。 我是一名探鬼主播簇宽,決...
    沈念sama閱讀 40,251評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼吧享!你這毒婦竟也來了魏割?” 一聲冷哼從身側響起,我...
    開封第一講書人閱讀 39,129評論 0 276
  • 序言:老撾萬榮一對情侶失蹤钢颂,失蹤者是張志新(化名)和其女友劉穎钞它,沒想到半個月后,有當地人在樹林里發(fā)現了一具尸體,經...
    沈念sama閱讀 45,561評論 1 314
  • 正文 獨居荒郊野嶺守林人離奇死亡遭垛,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內容為張勛視角 年9月15日...
    茶點故事閱讀 37,779評論 3 335
  • 正文 我和宋清朗相戀三年尼桶,在試婚紗的時候發(fā)現自己被綠了。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片锯仪。...
    茶點故事閱讀 39,902評論 1 348
  • 序言:一個原本活蹦亂跳的男人離奇死亡泵督,死狀恐怖,靈堂內的尸體忽然破棺而出庶喜,到底是詐尸還是另有隱情小腊,我是刑警寧澤,帶...
    沈念sama閱讀 35,621評論 5 345
  • 正文 年R本政府宣布久窟,位于F島的核電站秩冈,受9級特大地震影響,放射性物質發(fā)生泄漏斥扛。R本人自食惡果不足惜漩仙,卻給世界環(huán)境...
    茶點故事閱讀 41,220評論 3 328
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望犹赖。 院中可真熱鬧队他,春花似錦、人聲如沸峻村。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,838評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽粘昨。三九已至垢啼,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間张肾,已是汗流浹背芭析。 一陣腳步聲響...
    開封第一講書人閱讀 32,971評論 1 269
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留吞瞪,地道東北人馁启。 一個月前我還...
    沈念sama閱讀 48,025評論 2 370
  • 正文 我出身青樓,卻偏偏與公主長得像芍秆,于是被迫代替她去往敵國和親惯疙。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當晚...
    茶點故事閱讀 44,843評論 2 354