Android MaterialButton使用实例详解(告别shape、selector)

下面详细讲解一下“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技术站

(0)
上一篇 2023年5月16日
下一篇 2023年5月16日

相关文章

  • git-github 子模块仓库更新(git submodule)及git中submodule子模块的添加、使用和删除操作

    1. git中submodule子模块的添加、使用和删除操作 添加子模块 添加子模块的命令格式为: git submodule add <repository> [<path>] 其中repository表示子模块的远程仓库地址,path表示子模块在当前仓库中的路径,默认为代码库根目录下的repository名称。 以在当前仓库下添加…

    GitHub 2023年5月16日
    00
  • js按条件生成随机json:randomjson实现方法

    下面我来详细讲解“js按条件生成随机json:randomjson实现方法”的完整攻略,包含两条示例说明。 1. 什么是randomjson? randomjson是一种通过 JavaScript 来按照给定条件生成随机 JSON 对象的方法。 2. 安装和使用方法 randomjson可以通过在目标项目中使用 npm 来安装,也可以通过从 GitHub 上…

    GitHub 2023年5月16日
    00
  • 利用dep代替go get获取私有库的方法教程

    当我们需要使用私有库时,一般使用go get命令获取,但是这种方法有一个很大的缺点,就是必须知道私有库的git地址才能获取到,因此在团队协作的时候并不是非常方便。使用dep可以解决这个问题,其支持直接下载私有库。 步骤1:安装dep 在终端执行以下命令安装dep: $ go get -u github.com/golang/dep/cmd/dep 确保dep…

    GitHub 2023年5月16日
    00
  • IDEA2020配置Git的方法步骤

    IDEA2020配置Git的方法步骤 1. 下载并安装Git 下载Git安装包,然后运行安装程序,一路点击下一步,直到完成。 2. 配置Git 打开Git Bash或者其他终端工具,在命令行输入以下命令,配置用户名和邮箱: $ git config –global user.name "Your Name" $ git config -…

    GitHub 2023年5月16日
    00
  • Android Studio 常见问题及解决方法(推荐)

    Android Studio 常见问题及解决方法(推荐) 1. 安装问题 1.1 安装失败 如果 Android Studio 安装过程中失败,通常情况下是由于环境变量或系统权限的问题。为了解决此问题,你可以尝试以下步骤: 确认您的系统符合 Android Studio 的最低要求。 确认你的系统没有被安装其他版本的 JDK(Java Development…

    GitHub 2023年5月16日
    00
  • Visual Studio Code(vscode) git的使用教程

    下面我将详细讲解Visual Studio Code(以下简称 VS Code)与 Git 的使用教程。全文分为以下几个部分: VS Code 安装 Git 扩展 添加 Git 仓库 新建分支 提交代码 查看和比较提交记录 示例说明 1. VS Code 安装 Git 扩展 首先,我们需要在 VS Code 中安装 Git 扩展,这样才能在 VS Code …

    GitHub 2023年5月16日
    00
  • VSCode配置Go插件和第三方拓展包的详细教程

    下面我将为你提供“VSCode配置Go插件和第三方拓展包的详细教程”: 配置Go插件和第三方拓展包 在VSCode中打开终端(可以通过“终端”菜单或按下“Ctrl+`”打开),使用以下命令安装Go: sudo apt-get install golang-go 这是在Linux系统下的安装命令,其他操作系统可以上官网查找对应的安装方法。 在VSCode中安装…

    GitHub 2023年5月16日
    00
  • 史上最好用的远程桌面工具(附源码)

    下面是关于“史上最好用的远程桌面工具(附源码)”的完整攻略以及两条示例说明: 史上最好用的远程桌面工具(附源码)完整攻略 简介 本文介绍了一款史上最好用的远程桌面工具,该工具是使用Python语言编写的,并提供了源代码。该工具可以帮助用户远程操作另一台计算机,实现远程控制的功能。 使用方法 1. 下载源代码 首先,你需要从GitHub上下载源代码。在命令行中…

    GitHub 2023年5月16日
    00
合作推广
合作推广
分享本页
返回顶部