效果
前言
先来看一下MaterialButton是什么
由上图可以看到MaterialButton也没有什么神秘的,不过是 Button 的一个子类而已,但是经过谷歌的封装之后,在符合Material Design的基础上,使用起来更加方便了,且容易实现预期效果。
使用
引入 material 包
implementation 'com.google.android.material:material:1.2.1'
复制代码
常规
<com.google.android.material.button.MaterialButton android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/app_name" android:textAllCaps="false" />
复制代码
图标
<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:icon="@mipmap/ic_launcher" />
复制代码
圆角
<com.google.android.material.button.MaterialButton android:id="@+id/btn" 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:icon="@mipmap/ic_launcher" app:iconGravity="end" />
复制代码
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" />
复制代码
文字按钮
<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/app_name" android:textAllCaps="false" />
复制代码
圆形 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 是没有效果的,而是icon和iconGravity配合使用来达到效果。
属性
关于 xml 属性,我做了一个整理
Github
https://github.com/yechaoa/MaterialDesign
感谢
官方文档
最后
写作不易,感谢点赞支持 ^ - ^
评论