文章目录
效果图

关键代码
xml布局文件代码如下
<com.google.android.material.button.MaterialButton
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/app_name"
    android:textAllCaps="false"
    app:icon="@drawable/ic_baseline_bookmarks_24"
    app:iconGravity="end"
    app:strokeColor="@color/teal_200"
    app:strokeWidth="3dp"
    app:layout_constraintTop_toTopOf="parent"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toRightOf="parent"/>
 
MaterialButton是Google在SDK28推出的新控件,它继承自AppCompatButton,新增了圆角、描边、颜色设置等属性,在xml布局文件中直接设置即可。
app:icon表示需要显示的图标
 app:iconGravity表示图标位于文本的位置,end表示将图标放在文本的右侧
 app:strokeColor表示按钮描边的颜色,这里颜色是teal_200
 app:strokeWidth表示按钮描边的宽度,这里是3dp
MaterialButton其他常用属性如下:
| 属性 | 含义 | 
|---|---|
app:backgroundTint | 背景着色 | 
app:backgroundTintMode | 着色模式 | 
app:cornerRadius | 圆角大小 | 
app:rippleColor | 按压水波纹颜色 | 
app:strokeColor | 描边颜色 | 
app:strokeWidth | 描边宽度 | 
app:icon | 设置图标 | 
app:iconSize | 图标大小 | 
app:iconTint | 图标着色 | 
app:iconTintMode | 图标着色模式 | 
app:iconGravity | 图标相对文本的位置 | 
app:iconPadding | 图标与文本之间的间距 | 
源码地址
https://github.com/yurensan/MaterialDesignDemo










