Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/jpa/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 如何为材质按钮设置渐变背景?_Android_Android Drawable_Android Button_Material Components Android - Fatal编程技术网

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

Android 如何为材质按钮设置渐变背景?,android,android-drawable,android-button,material-components-android,Android,Android Drawable,Android Button,Material Components Android,我目前正在使用这段代码,但是背景没有改变。它仍然显示强调色作为背景 <com.google.android.material.button.MaterialButton android:id="@+id/materialButton" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Hello" app:cornerRadiu

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

<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"/>

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


请阅读材料按钮的文档,从

上面说,, 对于填充的按钮,主题的colorPrimary提供组件的默认背景色,文本颜色为colorOnPrimary。对于未填充的按钮,主题的colorPrimary提供组件的默认文本颜色,默认情况下背景色是透明的

我想这就是你看到其他颜色(强调色)的原因,也请检查设置背景色的属性

提到了以下属性:

app:backgroundTint


app:backgroundTintMode

LE:从我的角度来看,我建议您使用
按钮
AppCompatButton

试试这个:

gradient.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
    <gradient
        android:angle="0"
        android:endColor="#027FEE"
        android:startColor="#2CC6F2" />
</shape>
<!-- replace MaterialButton with Button or AppCompatButton -->
<com.google.android.material.button.MaterialButton
    android:id="@+id/materialButton"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@drawable/gradient"
    android:text="Hello" />
结果:


要设置背景,您需要扩展基本主题

Theme.AppCompat.Light.NoActionBar

而不是

Theme.MaterialComponents.Light.NoActionBar


要通过
MaterialButton
使用自定义可绘制背景,可以使用
android:background
属性:

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

注意:至少需要版本

当前,添加
app:backgroundTint=“@null”
非常重要,以避免默认情况下自定义背景不会使用
backgroundTint
颜色着色


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

什么是
渐变蓝
?你应该把它贴出来code@Pavan你可以经历这一切。希望这有助于你我需要把梯度背景。背景色调不允许使用drawables。您使用的是哪个版本。我试过你的代码,但仍然不工作背景没有改变?你能试一下-“'implementation'com.google.android.material:material:1.0.0-rc02”没有错误,背景没有改变。它甚至在预览模式下不会改变,在实际设备中也不会改变。如果主题不从材质组件延伸,则“渐变”可以与其他视图和按钮配合使用,不能使用Material Button
com.google.android.Material.Button.MaterialButton
仅适用于materialComponents主题,而不适用于appcompat主题
<MaterialButton
    app:backgroundTint="@null"
    android:background="@drawable/bkg_button_gradient"
    ... />