Android 按钮样式区分,使用内置 Material Design 样式

发布时间: 2021-05-01 17:02:34 作者: 大象笔记

背景

经常会遇到在 Android 界面中需要添加两个并排的按钮,例如:

两个按钮需要有颜色的区分,能明显分辨出 Primary, Secondary 的属性。

每次都现找颜色,是非常浪费时间的事情。所以我需要一套主题。

而 Google 的 Material Design 恰好为 Android 的主题而生。

首先添加依赖

https://github.com/material-components/material-components-android/blob/master/docs/getting-started.md

implementation 'com.google.android.material:material:<version>'

但我看用目前最新的 Android Studio 4.1.2 新建的 Kotlin 项目已经自动添加了这个依赖。

那就非常方便了,直接设置按钮外观类型即可。

Android 按钮样式

有三种内置的按钮样式

所以,设置一个 outlined button snippet 就能提升开发效率了。

snippet button_style_secondary "xml_android" i
style="?attr/materialButtonOutlinedStyle"
endsnippet

参考

https://material.io/components/buttons/android#using-buttons

我是一名山东烟台的开发者,联系作者