下面详细讲解一下“Android MaterialButton使用实例详解(告别shape、selector)”的完整攻略,过程中包含两条示例说明。
Android MaterialButton使用实例详解(告别shape、selector)
什么是MaterialButton
MaterialButton是Android Material Design中的一种控件,它是Button控件的一种更加现代、美观的实现方式。相比于普通Button,MaterialButton的阴影效果、形状、颜色等方面更加丰富。
MaterialButton的使用
1. 导入MaterialButton控件
在build.gradle文件中添加Material Design支持库:
implementation 'com.google.android.material:material:1.2.1'
2. 布局文件中使用MaterialButton
在布局文件中使用MaterialButton控件,例如:
<com.google.android.material.button.MaterialButton
android:id="@+id/btn_save"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Save"
app:backgroundTint="@color/colorPrimary"
app:cornerRadius="4dp"
app:rippleColor="@color/colorAccent"
style="@style/Widget.MaterialComponents.Button.OutlinedButton" />
MaterialButton的属性包括:
- backgroundTint:按钮的背景色
- cornerRadius:按钮圆角度数
- rippleColor:按钮点击时的水波纹效果颜色
- style:按钮样式
3. MaterialButton的样式
MaterialButton提供了3种样式:textButton、outlinedButton、raisedButton。
<style name="Widget.MaterialComponents.Button" parent="android:Widget.Material.Button">
<item name="backgroundTint">@color/mtrl_btn_bg_color_selector</item>
<item name="cornerRadius">4dp</item>
<item name="rippleColor">@color/mtrl_btn_ripple_color</item>
</style>
<style name="Widget.MaterialComponents.Button.TextButton" parent="Widget.MaterialComponents.Button">
<item name="android:textAppearance">@style/TextAppearance.MaterialComponents.Button</item>
<item name="android:minHeight">36dp</item>
</style>
<style name="Widget.MaterialComponents.Button.OutlinedButton" parent="Widget.MaterialComponents.Button">
<item name="android:background">@drawable/mtrl_outlined_button_background</item>
<item name="android:textAppearance">@style/TextAppearance.MaterialComponents.Button</item>
<item name="android:minHeight">36dp</item>
</style>
<style name="Widget.MaterialComponents.Button.UnelevatedButton" parent="Widget.MaterialComponents.Button">
<item name="backgroundTint">@color/mtrl_btn_bg_color_selector</item>
<item name="android:textAppearance">@style/TextAppearance.MaterialComponents.Button</item>
<item name="android:minHeight">36dp</item>
</style>
<style name="Widget.MaterialComponents.Button.RaisedButton" parent="Widget.MaterialComponents.Button">
<item name="android:background">@drawable/mtrl_button_background</item>
<item name="android:textAppearance">@style/TextAppearance.MaterialComponents.Button</item>
<item name="stateListAnimator">@animator/mtrl_btn_state_list_anim</item>
<item name="android:minHeight">36dp</item>
</style>
示例1:修改MaterialButton的shape和selector样式
1. 修改Button的shape样式
在res/drawable文件夹下新建一个文件button_shape.xml,写入以下代码:
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<solid android:color="@color/colorPrimary"/>
<corners android:radius="4dp"/>
</shape>
这会将MaterialButton的背景色和圆角设置为蓝色和4dp。
2. 修改Button的selector样式
在res/drawable文件夹下新建一个文件button_selector.xml,写入以下代码:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_enabled="false" android:drawable="@drawable/button_disable"/>
<item android:state_pressed="true" android:drawable="@drawable/button_press"/>
<item android:drawable="@drawable/button_normal"/>
</selector>
其中,button_disable是按钮不可用时的样式,button_press是按钮按下时的样式,button_normal是按钮正常状态下的样式。
3. 将新的shape和selector样式应用到MaterialButton控件
在MaterialButton的布局文件中,通过以下方式将shape和selector样式应用:
<com.google.android.material.button.MaterialButton
android:id="@+id/btn_save"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Save"
android:textColor="@color/white"
android:background="@drawable/button_selector"
style="@style/Widget.MaterialComponents.Button.UnelevatedButton" />
其中,background属性将selector样式应用到按钮,将原本的背景色改成了selector样式的背景色;而style属性则将按钮的样式指定为UnelevatedButton。
示例2:自定义MaterialButton的颜色
1. 新建颜色资源文件
在res/values文件夹下新建一个colors.xml文件,写入要使用的颜色:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="colorPrimary">#2C7CE5</color>
<color name="colorPrimaryDark">#2F4F4F</color>
<color name="colorAccent">#FF4081</color>
<color name="button_background_color">#2C7CE5</color>
<color name="button_text_color">#FFFFFF</color>
</resources>
2. 自定义MaterialButton的颜色
在MaterialButton的布局文件中,通过以下方式自定义颜色:
<com.google.android.material.button.MaterialButton
android:id="@+id/btn_save"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Save"
android:textColor="@color/button_text_color"
app:backgroundTint="@color/button_background_color"
app:rippleColor="@color/colorAccent"
style="@style/Widget.MaterialComponents.Button.UnelevatedButton" />
其中,backgroundTint属性将背景色设置为button_background_color,而textColor属性将文本颜色设置为button_text_color。
以上就是本文的全部内容。希望通过本文的介绍,你能够了解到MaterialButton的基本用法,以及如何自定义样式和颜色。
本站文章如无特殊说明,均为本站原创,如若转载,请注明出处:Android MaterialButton使用实例详解(告别shape、selector) - Python技术站