探索Android Design Support Library v28的新控件

Design 28 最近发布一系列新组件,目前还在alpha版本中,本文是在Kotlin下针对这些组件使用的示例,效果图如下:

Android Design Support Library v28

从上到下依次为:MaterialButton 、Chip、ChipGroup、MaterialCardView、BottomAppBar

环境与配置

  1. android studio>= 3.1
  2. sdk platforms = Android P
  3. sdk tools 勾选 Build-tools 28-rc2
  4. 在build.gradle(app)中
android {
    compileSdkVersion "android-P"
   ......
}
dependencies {
    .....
    implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
    implementation 'com.android.support:appcompat-v7:28.0.0-alpha1'
    implementation 'com.android.support:design:28.0.0-alpha1'
}

5.更改styles主题

 <!-- Base application theme. -->
    <style name="AppTheme" parent="Theme.MaterialComponents.Light.NoActionBar">
        <!-- Customize your theme here. -->
        
    </style>

1.MaterialButton

我们可以将这个按钮添加到布局文件中,如下所示:

 <android.support.design.button.MaterialButton
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_horizontal"
            android:text="MaterialButton"
            app:backgroundTint="@color/colorPrimary"
            app:icon="@drawable/ic_adb_white_24dp"
            app:iconTint="#e9a404"
            app:cornerRadius="7dp"
            app:rippleColor="@color/colorAccent"
            />
MaterialButton

属性说明:

app:icon :定义MaterialButton的图标
app:iconTint :对图标进行染色
app:iconTintMode :定义图标的色调模式
app:iconPadding :图标内边距
app:additionalPaddingLeftForIcon :图标左边额外的边距
app:additionalPaddingRightForIcon  :图标右边额外的边距(图标距离文字的距离)
app:rippleColor :按钮点击时的波纹效果颜色
app:backgroundTint  :以避免破坏按钮样式,使用此属性更改按钮的背景色
app:backgroundTintMode  :背景色色调模式
app:strokeColor :描边的颜色值
app:strokeWidth  :描边的颜色宽度
app:cornerRadius  :圆角半径

2. Chip

自带原型背景的用户可选择的建议列表,我们可以像这样将Chip添加到布局中,使用app:chipText属性设置要显示在Chip上的文本::

//布局
 <android.support.design.chip.Chip
            android:layout_marginTop="30dp"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_horizontal"
            android:id="@+id/single_chip"
            app:chipText="A single Chip"
            app:closeIcon="@drawable/ic_close_circle_black_24dp"
            app:closeIconEnabled="true"
            />

//代码中设置点击事件
 single_chip.setOnCloseIconClickListener {
            single_chip.visibility= View.INVISIBLE
        }
Chip

属性说明:

app:checkable :用于声明Chip是否可以切换为选定/不选定。如果禁用,则检查的行为方式与按钮相同。
app:chipIcon  :用于显示Chip图标
app:closeIcon :显示关闭图标(chipIcon 在前,closeIcon在文字后)
closeIconEnabled :默认为true

2.1 ChipGroup

如果我们向用户显示一组Chip,我们希望确保它们在我们的视图中正确地组合在一起。如果我们想使用ChipGroup,我们只需要将我们的芯片视图封装在一个父ChipGroup组件中:

 <android.support.design.chip.ChipGroup
            android:layout_gravity="center_horizontal"
            android:layout_marginTop="30dp"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content">
            <android.support.design.chip.Chip
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                app:chipText="Chip"
                android:id="@+id/ChipGroupChip1"
                app:chipBackgroundColor="@color/colorAccent"
                app:chipIcon="@drawable/ic_adb_white_24dp"
                app:closeIcon="@drawable/ic_close_circle_black_24dp"
                app:closeIconEnabled="true"
                />
            <android.support.design.chip.Chip
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                app:chipText="Group"
                />
            <android.support.design.chip.Chip
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                app:chipText="good"
                />
        </android.support.design.chip.ChipGroup>
ChipGroup

属性说明:

app:chipSpacing  :水平和垂直轴上chip间距
app:chipSpacingHorizontal :水平间距
app:chipSpacingVertical  :垂直间距
app:singleLine :chip显示在ChipGroup容器内的一行中,需要将ChipGroup包装在滚动视图(如HorizontalScrollView)中,这样用户就可以滚动显示芯片。

3. Material Card View

另一种样式的CardView组件,在布局中添加:

  <android.support.design.card.MaterialCardView
            android:layout_gravity="center_horizontal"
            android:layout_marginTop="60dp"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            app:strokeColor="@color/colorPrimary"
            app:strokeWidth="1dp"
            >
            <TextView
                android:padding="10dp"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="2018/5/21\nThis is a MaterialCardView "
                android:textAlignment="center"
                />

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

属性说明

app:strokeColor :描边画笔颜色
app:strokeWidth  :描边画笔宽度
app:cardBackgroundColor :背景颜色

4.Bottom App Bar

底部的应用程序栏是一个新的组件,它允许我们在布局的底部显示一个类似工具栏的组件。这允许我们以比标准工具栏更容易与其交互的方式向用户显示组件。将BottomAppBar添加到布局文件中,如下所示:

<android.support.design.widget.FloatingActionButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="36dp"
        app:fabSize="normal"
        android:src="@drawable/ic_add_white_24dp"
        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="@dimen/mtrl_bottomappbar_height"
        android:layout_gravity="bottom"
        app:backgroundTint="@color/colorPrimary"
        app:fabAttached="true"
        app:fabAlignmentMode="end"
        />
BottomAppBar+FloatingActionButton

BottomAppBar配合FloatingActionButton一起使用,属性说明:

app:layout_anchor:声明FloatingActionButton固定到BottomAppBar
app:fabAttached  :声明是否有一个FAB已被附加到底部的应用程序栏。
app:fabAlignmentMode :FloatingActionButton在BottomAppBar上的位置声明(center/end)
app:fabCradleVerticalOffset  :标记用于FB的垂直偏移量。默认情况下0dp
app:backgroundTint  ;BottomAppBar背景色

在Activity代码中添加菜单

  override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        
        setSupportActionBar(bottom_app_bar)
    }

    override fun onCreateOptionsMenu(menu: Menu?): Boolean {
        menuInflater.inflate(R.menu.bottom_app_bar_menu,menu)
        return true
    }

    override fun onOptionsItemSelected(item: MenuItem?): Boolean {
        if (item?.itemId==R.id.action_show_toast){
            Toast.makeText(this,"你好",Toast.LENGTH_LONG).show()
        }
        return true
    }

bottom_app_bar_menu.xml

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto">
    <item
        android:id="@+id/action_show_toast"
        android:title="Show toast"
        android:icon="@drawable/ic_more_vert_black_24dp"
        app:showAsAction="ifRoom"
        />
    <item
        android:id="@+id/item2"
        android:title="item2"
        android:icon="@drawable/ic_adb_white_24dp"
        app:showAsAction="ifRoom"
        />

</menu>

代码已打包上传Csdn下载

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

推荐阅读更多精彩内容