【Material Design】MaterialButton只看这一篇就足够了

这篇博客介绍了如何使用Material组件中的MaterialButton替代传统的shape和selector XML,以更简洁的方式设置按钮的样式,包括图标、颜色、圆角、边框等属性,并展示了不同样式的实现,如加图标、修改颜色和大小、设置圆角和边框以及创建单选按钮组。

前言

先写一些废话。

以前我们写一些登录按钮,注册按钮,提交按钮等等,都会新建一个xml文件,写shape,selector。

normal:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:width="279dp" android:height="38dp">
        <shape android:shape="rectangle">
            <solid android:color="@color/white"/>
            <corners android:radius="4dp" />
        </shape>
    </item>
</selector>

pressed:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:width="279dp" android:height="38dp">
        <shape android:shape="rectangle">
            <solid android:color="@color/black"/>
            <corners android:radius="4dp" />
        </shape>
    </item>
</selector>

selector:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@drawable/pressed" android:state_pressed="true" />
    <item android:drawable="@drawable/normal" android:state_pressed="false" />
</selector>

以上是最简单的按钮举例了吧,需要写3个xml。如果我们的项目小,按钮样式统一还好,如果按钮多变的话,就是3N个xml了,找起来很头疼,对吧。当然也有大佬也写出了BackgroundLibrary ,来替代shape,selector等。

题外话:推荐个插件Android Drawable Preview,看名字也知道的吧,图片预览

言归正传

我们这次的主题是Material组件中的MaterialButton。可以直接配边框,圆角,甚至形状等。

首先我们先来看此控件的使用方式,首先依赖material组件

  implementation 'com.google.android.material:material:1.3.0'

1.默认

<com.google.android.material.button.MaterialButton
            android:layout_width="wrap_content"
            android:layout_h
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值