材料按钮背景渐变

问题描述 投票:10回答:4

有没有一种方法可以为Google材质库中的MaterialButton设置渐变颜色。app:backgroundTint仅设置颜色,但不设置渐变颜色

android android-button material-components
4个回答
2
投票

不确定,但根据developer.android.com我们甚至不能添加android:background,似乎无法将drawable添加到materialButton


1
投票

没有使用默认方法的方法。最好使用gradient drawable

创建一个新的xml文件并将其放在可绘制的图像中,然后将其作为背景添加到按钮中

gradient.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
   <gradient
      android:startColor="#000000"
      android:centerColor="#ffffff"
      android:endColor="#cfcfcf"
      android:angle="270" />
</shape>

layout.xml

 <Button
    android:id="@+id/button"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="@drawable/gradient"
    android:text="YourText" >

1
投票

[MaterialButton忽略android:background,直到发布1.2.0-alpha06

在此版本中,您可以使用类似:

1.2.0-alpha06

如果您需要在库的早期版本中使用此功能,则可以使用<Button android:background="@drawable/bg_button_gradient" app:backgroundTint="@null" ... /> 。类似于:

AppCompatButton

-1
投票

例如创建可绘制文件

<androidx.appcompat.widget.AppCompatButton
    android:background="@drawable/bg_button_gradient"

并设置为该文件的按钮背景

© www.soinside.com 2019 - 2024. All rights reserved.