Android Design Support Library v28 新增组件详解

封面

1.简介

Google在近期发布了最新的Design Support Library 28.0.0-alpha3版本,其中新增了一些非常实用的组件,本篇文章将会对其进行详细的介绍,一起来看下!

如果你对Material Design还不太了解,可以点击这篇文章《Design Support Library介绍》了解一下。

引入依赖库

在app根目录的buil.gradle文件中加入依赖:

dependencies {
    ...
    implementation 'com.android.support:design:28.0.0-alpha3'
}

2.MaterialButton

MaterialButton组件继承于Button,因此可以使用Button的大部分属性,可以更便捷的设置按钮圆角、边框、图标等属性,看下效果:

MaterialButton

对应的布局文件:

<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.design.button.MaterialButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:text="Material Button"
        android:textAllCaps="false"
        android:textAppearance="?android:attr/textAppearanceLargeInverse"
        app:rippleColor="@color/color_white" />

    <android.support.design.button.MaterialButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:backgroundTint="@color/colorPrimary"
        android:gravity="center"
        android:text="Material Button"
        android:textAllCaps="false"
        android:textAppearance="?android:attr/textAppearanceLargeInverse" />

    <android.support.design.button.MaterialButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:text="Material Button"
        android:textAllCaps="false"
        android:textAppearance="?android:attr/textAppearanceLargeInverse"
        app:cornerRadius="10dp" />

    <android.support.design.button.MaterialButton
        android:layout_width="wrap_content"
        android:layout_height="50dp"
        android:layout_marginTop="10dp"
        android:gravity="center"
        android:text="Material Button"
        android:textAllCaps="false"
        android:textAppearance="?android:attr/textAppearanceLargeInverse"
        app:icon="@mipmap/ic_launcher"
        app:iconSize="30dp" />

    <android.support.design.button.MaterialButton
        android:layout_width="wrap_content"
        android:layout_height="50dp"
        android:layout_marginTop="10dp"
        android:gravity="center"
        android:text="Material Button"
        android:textAllCaps="false"
        android:textAppearance="?android:attr/textAppearanceLargeInverse"
        app:icon="@mipmap/ic_launcher"
        app:iconSize="30dp"
        app:iconTint="@color/colorPrimary" />

    <android.support.design.button.MaterialButton
        android:layout_width="wrap_content"
        android:layout_height="50dp"
        android:layout_marginTop="10dp"
        android:gravity="center"
        android:text="Material Button"
        android:textAllCaps="false"
        android:textAppearance="?android:attr/textAppearanceLargeInverse"
        app:icon="@mipmap/ic_launcher"
        app:iconSize="30dp"
        app:iconTint="@color/colorPrimary"
        app:strokeColor="@color/colorPrimaryDark"
        app:strokeWidth="2dp" />

</LinearLayout>

注意:MaterialButton必须设置textAppearance属性,如果没有的话会报错,不知道是不是bug。

MaterialButton属性速查表:

属性 介绍
app:backgroundTint 按钮背景着色
app:backgroundTintMode 按钮背景的着色模式
app:icon 按钮图标(在文字左边,不能设置位置)
app:iconSize 按钮图标大小
app:iconPadding 按钮图标的内边距
app:iconTint 按钮图标着色
app:iconTintMode 按钮图标的着色模式
app:additionalPaddingStartForIcon 按钮图标的左内边距
app:additionalPaddingEndForIcon 按钮图标的右内边距
app:strokeColor 按钮边框颜色
app:strokeWidth 按钮边框宽度
app:cornerRadius 按钮圆角角度
app:rippleColor 按钮点击水波纹效果颜色

着色模式效果

着色模式效果

3.Chip & ChipGroup

在Chip组件出现之前,我们一直都是通过自定义控件的形式来实现关键字标签等UI效果的,比如《Android 流式布局FlowLayout 实现关键字标签》中实现的效果,现在Chip & ChipGroup组件可以对其进行原生支持了,非常Nice,看下效果:

Chip & ChipGroup

对应的布局文件:

<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.design.chip.ChipGroup
        android:id="@+id/chip_group"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="10dp"
        app:chipSpacing="10dp"
        app:singleSelection="false">

        <android.support.design.chip.Chip
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:checkable="true"
            android:clickable="true"
            android:text="Chip"
            android:textAllCaps="false" />

        <android.support.design.chip.Chip
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:clickable="true"
            android:text="水波纹颜色"
            android:textAllCaps="false"
            app:rippleColor="@color/colorAccent" />

        <android.support.design.chip.Chip
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:checkable="true"
            android:clickable="true"
            android:text="图标"
            android:textAllCaps="false"
            app:chipIcon="@mipmap/ic_launcher"
            app:chipIconEnabled="true"
            app:chipIconSize="20dp"
            app:iconStartPadding="5dp" />

        <android.support.design.chip.Chip
            android:id="@+id/chip_close"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:checkable="true"
            android:clickable="true"
            android:text="删除按钮"
            android:textAllCaps="false"
            app:closeIconEnabled="true" />

        <android.support.design.chip.Chip
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:checkable="true"
            android:clickable="true"
            android:text="背景"
            android:textAllCaps="false"
            app:chipBackgroundColor="@color/colorAccent" />

        <android.support.design.chip.Chip
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:checkable="true"
            android:clickable="true"
            android:text="边框"
            android:textAllCaps="false"
            app:chipStrokeColor="@color/colorAccent"
            app:chipStrokeWidth="2dp" />

        <android.support.design.chip.Chip
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:checkable="true"
            android:clickable="true"
            android:text="圆角角度"
            android:textAllCaps="false"
            app:chipCornerRadius="0dp" />

    </android.support.design.chip.ChipGroup>

</LinearLayout>

单选或多选功能需要设置android:checkable="true"才可以实现,官方给的设置方式是app:checkable=“true”,会报错找不到,可能是bug,发布正式版的时候再看看。

Chip组件默认不支持点击,需要设置android:clickable="true"才可以。

ChipGroup属性速查表:

属性 介绍
app:chipSpacing Chip在水平&垂直方向的间距
app:chipSpacingHorizontal Chip在水平方向的间距
app:chipSpacingVertical Chip在垂直方向的间距
app:singleLine 是否单行显示Chip,默认为false
app:singleSelection 是否为单选模式,默认为false

Chip属性速查表:

属性 介绍
app:chipBackgroundColor Chip背景颜色
app:chipCornerRadius Chip圆角角度
app:chipStrokeColor Chip边框颜色
app:chipStrokeWidth Chip边框宽度
app:rippleColor Chip点击水波纹效果颜色
app:chipIconEnabled 是否在Chip上显示图标,默认为true
app:chipIcon Chip图标(在文字左边,不能设置位置)
app:chipIconSize Chip图标大小
app:closeIconEnabled 是否显示Chip关闭按钮,默认为false
app:closeIcon Chip关闭按钮图标
app:closeIconTint Chip关闭按钮着色
app:closeIconSize Chip关闭按钮大小
app:checkedIconEnabled 是否显示Chip选中图标,默认为true
app:checkedIcon Chip选中图标
app:chipStartPadding Chip左内边距
app:chipEndPadding Chip右内边距
app:iconStartPadding Chip图标左内边距
app:iconEndPadding Chip图标右内边距
app:textStartPadding Chip文字左内边距
app:textEndPadding Chip文字右内边距
app:closeIconStartPadding Chip关闭图标左内边距
app:closeIconEndPadding Chip关闭图标右内边距

4.MaterialCardView

与普通CardView相比,可以设置边框样式,看下效果:

MaterialCardView

对应的布局文件:

<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.design.card.MaterialCardView
        android:layout_width="match_parent"
        android:layout_height="100dp"
        android:layout_margin="20dp"
        app:cardBackgroundColor="@color/colorAccent">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:text="MaterialCardView" />

    </android.support.design.card.MaterialCardView>

    <android.support.design.card.MaterialCardView
        android:layout_width="match_parent"
        android:layout_height="100dp"
        android:layout_margin="20dp"
        app:cardCornerRadius="10dp"
        app:cardElevation="5dp">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:text="MaterialCardView" />

    </android.support.design.card.MaterialCardView>

    <android.support.design.card.MaterialCardView
        android:layout_width="match_parent"
        android:layout_height="100dp"
        android:layout_margin="20dp"
        app:strokeColor="@color/colorAccent"
        app:strokeWidth="5dp">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:text="MaterialCardView" />

    </android.support.design.card.MaterialCardView>

</LinearLayout>

MaterialCardView属性速查表:

属性 介绍
app:strokeColor MaterialCardView边框颜色
app:strokeWidth MaterialCardView边框宽度

5.BottomAppBar

BottomAppBar组件通常会与FloatingActionButton组件一起使用,但是在开发过程中不是很常用,看下效果:

BottomAppBar

对应的布局文件:

<android.support.design.widget.CoordinatorLayout 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.support.design.widget.FloatingActionButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@mipmap/ic_add"
        app:fabSize="normal"
        app:layout_anchor="@id/bottom_app_bar" />

    <android.support.design.bottomappbar.BottomAppBar
        android:id="@+id/bottom_app_bar"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:layout_gravity="bottom"
        app:backgroundTint="@color/colorPrimary"
        app:fabAlignmentMode="end"
        app:fabAttached="true" />

</android.support.design.widget.CoordinatorLayout>

根布局需要使用CoordinatorLayout,FloatingActionButton通过layout_anchor属性与BottomAppBar绑定在一起。

BottomAppBar属性速查表:

属性 介绍
app:backgroundTint BottomAppBar背景着色
app:fabAlignmentMode FAB位置(居中或居右),默认为居右
app:fabAttached 是否绑定FAB,默认为true
app:fabCradleMargin BottomAppBar与FAB的距离,默认为5dp
app:fabCradleRoundedCornerRadius BottomAppBar与FAB相邻处的圆角角度,默认为8dp
app:fabCradleVerticalOffset FAB在BottomAppBar中的垂直偏移量,默认为0dp

6.写在最后

到这里Design Support Library v28新增组件就介绍完了,如有错误或者遗漏的地方可以给我留言评论,谢谢!

代码已上传至GitHub,欢迎Star、Fork!

GitHub地址:https://github.com/alidili/DesignSupportDemo

本文Demo的Apk下载地址:https://github.com/alidili/DesignSupportDemo/raw/master/DesignSupportDemo.apk

©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 159,015评论 4 362
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 67,262评论 1 292
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 108,727评论 0 243
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 43,986评论 0 205
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 52,363评论 3 287
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 40,610评论 1 219
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 31,871评论 2 312
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 30,582评论 0 198
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 34,297评论 1 242
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 30,551评论 2 246
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 32,053评论 1 260
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 28,385评论 2 253
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 33,035评论 3 236
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 26,079评论 0 8
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,841评论 0 195
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 35,648评论 2 274
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 35,550评论 2 270

推荐阅读更多精彩内容

  • 1、通过CocoaPods安装项目名称项目信息 AFNetworking网络请求组件 FMDB本地数据库组件 SD...
    X先生_未知数的X阅读 15,937评论 3 118
  • 发现 关注 消息 iOS 第三方库、插件、知名博客总结 作者大灰狼的小绵羊哥哥关注 2017.06.26 09:4...
    肇东周阅读 11,613评论 4 59
  • 1、自定义用户表 Django中自带了用户表,但是有时候我们需要的用户属性与自带表有差别,这时我们可以自定义用户表...
    勇不言弃92阅读 1,474评论 0 1
  • 我一直都很自卑的,从来有人向我表白都是拒绝,从来遇见喜欢的人都是退缩。因为我知道你不可能喜欢我的,只要你真正了解我...
    ff7295735936阅读 229评论 0 0
  • Kobalt是美国一家互联网音乐版权运营公司。 2001年,瑞典人Willard Ahdritz(上图)在伦敦创办...
    镜子啊啊阅读 667评论 0 0