Android MaterialButton使用详解,告别shape、selector(1)

app:iconGravity=“end” />

  • app:cornerRadius属性指定圆角大小。

Button描边

在这里插入图片描述

<com.google.android.material.button.MaterialButton

android:layout_width=“wrap_content”

android:layout_height=“wrap_content”

android:layout_marginTop=“20dp”

android:text="@string/app_name"

android:textAllCaps=“false”

app:cornerRadius=“25dp”

app:strokeColor="@color/black"

app:strokeWidth=“3dp” />

  • app:strokeColor 描边颜色

  • app:strokeWidth 描边宽度

文字描边

在这里插入图片描述

<com.google.android.material.button.MaterialButton

style="@style/Widget.MaterialComponents.Button.OutlinedButton"

android:layout_width=“wrap_content”

android:layout_height=“wrap_content”

android:layout_marginTop=“20dp”

android:text="@string/app_name"

android:textAllCaps=“false”

app:cornerRadius=“5dp”

app:rippleColor="@color/red"

app:strokeColor="@color/red"

app:strokeWidth=“3dp” />

  • 与上面不同的是,这里用了style,区别在于上面的是常规Button状态下的描边,这个是文字Button状态下的描边。

  • app:rippleColor 点击波纹颜色

文字按钮

在这里插入图片描述

<com.google.android.material.button.MaterialButton

style="@style/Widget.MaterialComponents.Button.TextButton"

android:layout_width=“wrap_content”

android:layout_height=“wrap_content”

android:layout_marginTop=“20dp”

android:text="@string/ap

《Android学习笔记总结+最新移动架构视频+大厂安卓面试真题+项目实战源码讲义》

【docs.qq.com/doc/DSkNLaERkbnFoS0ZF】 完整内容开源分享

p_name"

android:textAllCaps=“false” />

  • 与常规使用方法一样,但是加了style,这个style在未选中的情况下,对背景色设置了透明

圆形Button

在这里插入图片描述

<com.google.android.material.button.MaterialButton

android:layout_width=“100dp”

android:layout_height=“100dp”

android:layout_marginTop=“20dp”

android:insetTop=“0dp”

android:insetBottom=“0dp”

android:text="@string/login"

android:textAllCaps=“false”

android:textSize=“20sp”

app:cornerRadius=“999dp”

app:strokeColor="@color/orange"

app:strokeWidth=“5dp” />

这里为什么来个圆形Button呢,是因为涉及到两个属性

  • android:insetTop 上边距

  • android:insetBottom 下边距

这两个参数默认是6dp,不设置为0dp的话,就不是一个规则的圆。

在这里插入图片描述

关于其他属性的默认参数,可以在xml文件的右上角,选中Design面板,选择要查看的View即可。

在这里插入图片描述

author:yechaoa

源码分析icon

唯一不足的是MaterialButton不能像chip一样给icon设置事件。

来看看源码中 icon具体是什么实现的:

public void setIcon(@Nullable Drawable icon) {

if (this.icon != icon) {

this.icon = icon;

updateIcon(/* needsIconUpdate = */ true);

}

}

这里比较简单,继续看调用的updateIcon方法

private void updateIcon(boolean needsIconUpdate) {

// Forced icon update

if (needsIconUpdate) {

resetIconDrawable(isIconStart);

return;

}

if (hasIconChanged) {

resetIconDrawable(isIconStart);

}

}

有省略,看关键两段代码都调用了resetIconDrawable方法,继续

private void resetIconDrawable(boolean isIconStart) {

if (isIconStart) {

TextViewCompat.setCompoundDrawablesRelative(this, icon, null, null, null);

} else {

TextViewCompat.setCompoundDrawablesRelative(this, null, null, icon, null);

}

}

相信到这里很多人就明白了,icon的实现等同于drawableStart

只不过在MaterialButton中drawableStart是没有效果的,而是iconiconGravity配合使用来达到效果。

属性

=============================================================

关于xml属性,我做了一个整理

| 属性 | 含义 |

| — | — |

| insetBottom | 下边距,默认6dp |

| insetTop | 上边距,默认6dp |

| cornerRadius | 圆角大小 |

| icon | 图标 |

| iconGravity | 图标位置,只能前后 |

| iconPadding | 图标距文字距离,默认8dp |

| iconSize | 图标大小 |

| iconTint | 图标着色 |

| iconTintMode | 图标着色模式 |

| rippleColor | 点击波纹颜色 |

| strokeColor | 描边颜色 |

| strokeWidth | 描边宽度 |

| app:backgroundTint | 背景色(注意命名空间) |

Github

=================================================================

https://github.com/yechaoa/MaterialDesign

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值