如何为材质按钮设置渐变背景?

2024-03-29

我目前正在使用此代码,但背景没有改变。它仍然显示强调色作为背景。

<com.google.android.material.button.MaterialButton
    android:id="@+id/materialButton"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Hello"
    app:cornerRadius="32dp"
    android:background="@drawable/gradiant_blue"/>

渐变_蓝色.xml

<shape
   xmlns:android="http://schemas.android.com/apk/res/android"
   android:shape="rectangle">

   <gradient
       android:angle="-45"
       android:startColor="#2979FF"
       android:endColor="#7C4DFF"/>
</shape>

我目前正在使用

材料组件版本:1.0.0-rc02


使用自定义可绘制背景MaterialButton你可以使用android:background属性:

<MaterialButton
    app:backgroundTint="@null"
    android:background="@drawable/bkg_button_gradient"
    ... />

NOTE: It requires至少版本1.2.0-alpha06 https://github.com/material-components/material-components-android/commit/309d71860f0686137277915fb09e7502cbafc2b4

目前非常重要的是添加app:backgroundTint="@null"以避免默认情况下自定义背景不会着色backgroundTint color.

对于较低版本的材料组件库,您必须使用AppCompatButton.

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何为材质按钮设置渐变背景? 的相关文章

随机推荐