10

有没有办法我可以从谷歌材质库为 MaterialButton 设置渐变颜色。app:backgroundTint 只设置颜色但不设置渐变颜色

4

4 回答 4

5

MaterialButtonandroid:background直到 release被忽略1.2.0-alpha06

在此版本中,您可以使用以下内容:

<Button
    android:background="@drawable/bg_button_gradient"
    app:backgroundTint="@null"
    ... />

如果您在早期版本的库中需要此功能,您可以使用AppCompatButton. 就像是:

<androidx.appcompat.widget.AppCompatButton
    android:background="@drawable/bg_button_gradient"
于 2020-05-05T10:00:45.207 回答
3

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

于 2019-02-22T08:32:34.140 回答
1

没有办法通过使用默认方法来做到这一点。更好地使用渐变 drawable

创建一个新的xml文件并将其放入drawable中,然后将其添加到按钮作为背景

渐变.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>

布局.xml

 <Button
    android:id="@+id/button"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="@drawable/gradient"
    android:text="YourText" >
于 2018-07-27T16:50:07.603 回答
-2

例如,创建可绘制文件

<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<gradient
    android:angle="135"
    android:centerColor="#6200ee"
    android:endColor="#6200ee"
    android:startColor="#3700b3"
    android:type="linear" />
 </shape>

并将此文件设置为按钮的背景

于 2018-07-27T16:46:01.613 回答