效果图
关键代码
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 | 图标与文本之间的间距 |