【问题标题】:Android button background is taking the primary colorAndroid按钮背景采用原色
【发布时间】:2025-12-09 15:35:01
【问题描述】:

我有这个问题,我不知道它来自哪里,我创建了这个带有自定义背景的按钮,但是背景颜色是原色,除非改变原色,否则无法改变它。

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

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


【解决方案1】:

由于您使用的是 Theme.MaterialComponents.*,因此您的 Buttonat runtime 替换为 MaterialButton

目前 backgroundTint 仍然是 默认 MaterialButton 样式。 这意味着如果您使用自定义android:background,则必须确保将backgroundTint 设为空,以避免自定义背景不会被主题中定义的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 也发布 style="@style/normalText"
  • 真的从心底里非常感谢你......❤
【解决方案2】:

当使用 Material 主题时,Button 视图被映射到 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>

【讨论】:

    【解决方案3】:

    试试这个。

    您的按钮:

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

    【讨论】:

    • 这是我的可绘制类,但没有任何变化:-------------------------------- ------------------------------------------------------- schemas.android.com/apk/res/android" >
    【解决方案4】:

    在你的 Gradle 中替换这一行

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

    有了这个

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

    【讨论】:

      【解决方案5】:

      您只需转到您的themes.xml 文件并将样式名称更改为:

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

      在 Light 主题模式下并:

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

      在您的 theme.xml(night) 文件中处于深色主题模式,然后您可以自定义按钮并更改其颜色。

      【讨论】:

        【解决方案6】:

        什么对我有用

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

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

        然后在您的布局中应用样式并使用您想要的 colordrawable

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

        【讨论】: