Android按钮背景成为主要颜色 [英] Android button background is taking the primary color

查看:56
本文介绍了Android按钮背景成为主要颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到了这个问题,我不知道它来自何处,我创建了具有自定义背景的按钮,但是背景颜色代表原色,除非更改原色,否则无法更改.

I've this issue, I don't know where it come from, I've created this buttons with custom background, but the background color talking the primary color and cannot change it unless change the primary color.

<Button
    android:id="@+id/btn_teacher"
    style="@style/normalText"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginBottom="@dimen/_16sdp"
    android:background="@drawable/btn_rounded_stroke"
    android:text="@string/txt_teacher" />

    <resources>
        <color name="colorPrimary">#008577</color>
        <color name="colorPrimaryDark">#00574B</color>
        <color name="colorAccent">#D81B60</color>
        <color name="bg_color">#FFD7A4</color>        
    </resources>

我有许多颜色不同的按钮,所以我不能更改原色

I have many buttons with different colors, so i can't change the primary color

这是我的绘画背景

 <layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
    <item >
        <shape android:shape="rectangle"  >
            <size android:height="@dimen/_48sdp" />
            <corners android:radius="@dimen/_24sdp" />
            <stroke android:width="@dimen/_1sdp" android:color="#59CECE" />
            <solid android:color="@android:color/white"/>
        </shape>
    </item>

</layer-list>

我正在使用Google的新材料设计

I'm using new material design by google

implementation "com.google.android.material:material:1.3.0-alpha02"

如何覆盖此颜色?

推荐答案

由于使用的是材料组件主题,因此 Button MaterialButton 替换.

Since you are using a Material Components theme your Button is replaced by a MaterialButton.

您必须添加 app:backgroundTint =" @ null" :

You have to add app:backgroundTint="@null":

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

当前, backgroundTint 仍然是默认的MaterialButton样式.如果您使用的是自定义 android:background ,则必须确保将 backgroundTint 清空,以免自定义背景被着色.

Currently the backgroundTint is still the default MaterialButton style. If you are using a custom android:background, you have to make sure to null out backgroundTint to avoid that the custom background doesn't get tinted.

在任何情况下,您都不需要自定义背景( btn_rounded_stroke ).
只需使用标准的 MaterialButton :

In any case you don't need a custom background (btn_rounded_stroke) in your case.
Just use the standard MaterialButton:

    <com.google.android.material.button.MaterialButton
        android:layout_width="wrap_content"
        android:layout_height="48dp"
        style="@style/Widget.MaterialComponents.Button.OutlinedButton"
        app:strokeColor="#59CECE"
        app:cornerRadius="24dp"

这篇关于Android按钮背景成为主要颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆