Android按钮背景色采用了主色

24

我有这个问题,不知道它从哪里来,我已经创建了这些带有自定义背景的按钮,但是背景颜色使用了主要颜色,除非更改主要颜色,否则无法更改。

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

我有很多不同颜色的按钮,所以我不能更改主要颜色

这是我的可绘制背景

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

我正在使用谷歌的全新材料设计

implementation "com.google.android.material:material:1.3.0-alpha02"
我该如何覆盖这个颜色? 这是按钮的图片

发布 @drwable/btn_round_stroke 的 xml。 - chand mohd
6个回答

40

因为你正在使用 Theme.MaterialComponents.*,所以你的 Button 在运行时被替换成了 MaterialButton

目前的 backgroundTint 仍然是默认的 MaterialButton 样式。 这意味着如果你使用了自定义的 android:background,你必须确保将 backgroundTint 设置为 null,以避免自定义背景与主题中定义的 attr/colorPrimary 进行染色。

你需要添加 app:backgroundTint="@null"

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

在任何情况下,您都不需要在您的情况下使用自定义背景(btn_rounded_stroke)。您只是使用自定义背景来定义圆角。这个功能默认由MaterialButton提供,只需使用cornerRadius属性即可。

使用标准的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"

输入图像描述


尝试过了,但不起作用,它将其变成蓝色而不是我的自定义背景。 - Fath.Saif
@Fath.Saif的帖子也有样式="@style/normalText" - Gabriele Mariotti
1
真的非常感谢你,从我内心深处发自肺腑地感谢你....❤ - Aviroxi
非常感谢!父样式 Theme.MaterialComponents.NoActionBar 是罪魁祸首! - 6rchid

5

当使用材料主题时,按钮视图映射到MaterialButton。 在这种情况下,还有一种设置按钮背景的方法。

    <style name="AppTheme" parent="Theme.MaterialComponents.DayNight.DarkActionBar"
...
        <item name="materialButtonStyle">@style/ColoredButton</item>
    </style>

    <style name="ColoredButton" parent="Widget.MaterialComponents.Button.TextButton">
        <item name="backgroundTint">@color/customButtonColor</item>
    </style>

2

我的做法

首先在你的styles.xmlthemes.xml中创建btn_style

<style name="btn_style" parent="Widget.MaterialComponents.Button">
    <item name="backgroundTint">@null</item>
</style>

然后在您的布局中应用样式并使用您想要的颜色可绘制对象

        <Button
            android:id="@+id/btn"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:textStyle="bold"
            android:background="@color/Red"
            style="@style/btn_style"
            android:text="Button"/>

0
你所需要做的就是打开themes.xml文件,然后将样式名称更改为:
style name="Theme.AnimatedLogin" 
parent="Theme.AppCompat.Light.NoActionBar"

在明亮主题模式下并且到:

style name="Theme.AnimatedLogin"
parent="Theme.AppCompat.DayNight.NoActionBar"

在您的theme.xml(夜间)文件中启用暗黑主题模式,然后您可以自定义按钮并更改其颜色。

0
在你的Gradle中,只需替换这一行即可。
implementation "com.google.android.material:material:1.3.0-alpha02"

使用这个

 implementation 'com.google.android.material:material:1.2.1'

-1

试试这个。

你的按钮:

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textColor="#FFFFFF"
    android:layout_margin="5dp"
    android:background="@drawable/round_btn"
    android:padding="20dp"
    android:text="My Text" />

这是你的round_btn可绘制对象:

<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
    <solid android:color="#008577" />
    <corners android:radius="20dp" />
</shape>

这是我的drawable类,但没有任何变化: <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> - Fath.Saif

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接