【问题标题】:How to set a gradient background to a Material Button?如何为材质按钮设置渐变背景?
【发布时间】:2019-02-21 14:01:41
【问题描述】:

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

<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

【问题讨论】:

  • 什么是gradiant_blue?你应该发布它的代码
  • @Pavan 你可以通过这个link。希望对你有帮助

标签: android android-drawable android-button material-components-android


【解决方案1】:

要使用带有MaterialButton 的自定义可绘制背景,您可以使用android:background 属性:

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

注意:它需要至少版本 1.2.0-alpha06

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

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

【讨论】:

    【解决方案2】:

    LE:在我看来,我建议你使用Button 或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>
    

    button.xml

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

    结果:

    使用 corner radius 将 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" />
        <corners android:radius="32dp"/>
    </shape>
    

    结果:

    【讨论】:

    • 您使用的是哪个版本。我试过你的代码,但还是不行
    • 背景没有变化?
    • 你可以尝试一下 - " 'implementation 'com.google.android.material:material:1.0.0-rc02' "
    • 没有错误,背景没有变化。它甚至在预览模式下都没有变化,在实际设备中也没有变化
    • 对于材料 btn 不要忘记添加app:backgroundTint="@null"
    【解决方案3】:

    请阅读材料按钮的文档,来自here

    它说, “对于填充按钮,您的主题的 colorPrimary 提供组件的默认背景颜色,文本颜色为 colorOnPrimary。对于未填充的按钮,您的主题的 colorPrimary 提供组件的默认文本颜色,并且背景颜色默认为透明。”

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

    提及以下属性:

    app:backgroundTint

    app:backgroundTintMode

    【讨论】:

    • 我需要放渐变背景。 background-tint 不允许绘制。
    【解决方案4】:

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

    Theme.AppCompat.Light.NoActionBar

    而不是

    Theme.MaterialComponents.Light.NoActionBar

    【讨论】:

    • 如果您的主题不是从 MaterialComponents 扩展而来的,则不能使用 Material Button
    • com.google.android.material.button.MaterialButton 仅适用于 materialComponents 主题而非 appcompat 主题
    猜你喜欢
    • 1970-01-01
    • 2019-03-25
    • 2016-10-20
    • 2012-08-26
    • 2015-03-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多