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

文章目录

    背景

    经常会遇到在 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 按钮样式

    有三种内置的按钮样式

    • Text button
    • Outlined button 适合做 secondary 样式
    • Contained button (默认)

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

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

    参考

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

    关于作者 🌱

    我是来自山东烟台的一名开发者,有感兴趣的话题,或者软件开发需求,欢迎加微信 zhongwei 聊聊,或者关注我的个人公众号“大象工具”, 查看更多联系方式