不改变 colorAccent 就不能改变 MaterialButton 的背景颜色

问题描述 投票:0回答:11

安卓工作室 3.2.1 这是我的布局:

<com.google.android.material.button.MaterialButton
                android:id="@+id/bittrexJsonViewButton"
                android:layout_width="0dp"
                android:layout_height="@dimen/min_height"
                android:layout_marginStart="@dimen/half_default_margin"
                android:layout_marginEnd="@dimen/half_default_margin"
                android:text="@string/json_view"
                app:layout_constraintBottom_toBottomOf="@+id/binanceJsonViewButton"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintStart_toEndOf="@+id/binanceJsonViewButton"
                app:layout_constraintTop_toTopOf="@+id/binanceJsonViewButton" />

改变MaterialButton的背景我改变colorAccentstyles.xml

<item name="colorAccent">@color/colorAccent</item>

不错。这是工作。

但问题是:我不想改变colorAccent。我想使用不同于 colorAccentMaterialButton

的背景颜色

属性:

android:background="#aabbcc"

没有帮助。

android material-design android-button material-components-android material-components
11个回答
76
投票

第一个解决方案

您可以使用

app:backgroundTint
来改变
MaterialButton

的背景颜色
<com.google.android.material.button.MaterialButton
                android:id="@+id/bittrexJsonViewButton"
                android:layout_width="0dp"
                android:layout_height="@dimen/min_height"
                android:layout_marginStart="@dimen/half_default_margin"
                android:layout_marginEnd="@dimen/half_default_margin"
                app:backgroundTint="@android:color/holo_orange_dark"
                android:text="@string/json_view"
                app:layout_constraintBottom_toBottomOf="@+id/binanceJsonViewButton"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintStart_toEndOf="@+id/binanceJsonViewButton"
                app:layout_constraintTop_toTopOf="@+id/binanceJsonViewButton" />

第二个解决方案

MaterialButton
在按钮处于活动状态时使用
colorPrimary
作为背景,在禁用时使用
colorOnSurface
。所以,你可以在你的主题中定义它并将它应用到材质按钮上


51
投票

随着

MaterialButton
你有2个选项:

  1. 按照

    Zaid Mirza
    的建议使用backgroundTint

    属性
  2. 如果您想覆盖默认样式中的某些主题属性,您可以使用新的

    materialThemeOverlay
    属性。在我看来这是最好的选择。

类似的东西:

<style name="Widget.App.ButtonStyle"
 parent="Widget.MaterialComponents.Button">
   <item name="materialThemeOverlay">@style/GreenButtonThemeOverlay</item>
</style>

<style name="GreenButtonThemeOverlay">
  <item name="colorPrimary">@color/green</item>
</style>

然后:

<com.google.android.material.button.MaterialButton
   style="Widget.App.ButtonStyle"
   ../>

至少需要 1.1.0 版本的库。


40
投票

如果你想设置自定义绘图,你需要制作

app:backgroundTint="@null"
。只是改变背景颜色
app:backgroundTint="@color/yourColor"

我目前正在使用

1.3.0-alpha01

<com.google.android.material.button.MaterialButton
            android:id="@+id/bittrexJsonViewButton"
            android:layout_width="0dp"
            android:layout_height="@dimen/min_height"
            android:layout_marginStart="@dimen/half_default_margin"
            android:layout_marginEnd="@dimen/half_default_margin"
            app:backgroundTint="@null"
            android:background="@drawable/your_custom_drawable"
            android:text="@string/json_view"
            app:layout_constraintBottom_toBottomOf="@+id/binanceJsonViewButton"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toEndOf="@+id/binanceJsonViewButton"
            app:layout_constraintTop_toTopOf="@+id/binanceJsonViewButton" />

12
投票

您可以按照以下代码进行操作。

                android:background="@color/black"
                app:backgroundTint="@null"

5
投票

2020:看来他们刚刚在 2020 年 4 月 1 日修复了这个问题。

它应该在 1.2.0 beta 1 上发布,因为 GitHub 问题已关闭 “固定”


2
投票

backgroundTint
也改变了禁用状态的颜色所以对我不好

我能找到的最佳解决方案是通过叠加样式覆盖

MaterialButton
(仅)的原色

将此代码添加到您的样式中。

?attr/colorSecondary
替换成你想要的任何颜色

<style name="MyButtonTheme" parent="Widget.MaterialComponents.Button">
    <item name="materialThemeOverlay">@style/ButtonStyleTextColor</item>
</style>

<style name="ButtonStyleTextColor">
    <item name="colorPrimary">?attr/colorSecondary</item>
</style>

为按钮添加主题

<com.google.android.material.button.MaterialButton
//..
android:theme="@style/MyButtonTheme"/>

如果您使用 MDC 并且想要更改所有按钮的主题:

将此行添加到您的 themes.xml

<item name="materialButtonStyle">@style/Button.MyTheme</item>

并将这些行添加到您的 type.xml

<style name="Button.MyTheme" parent="Widget.MaterialComponents.Button">
    <item name="materialThemeOverlay">@style/ButtonStyleTextColor</item>
</style>

<style name="ButtonStyleTextColor">
    <item name="colorPrimary">?attr/colorSecondary</item>
</style>

在那种情况下,您不需要将

android:theme="@style/MyButtonTheme"
添加到您的
MaterialButton

如有错误请告诉我,不要着急降级


2
投票

对我有用的解决方案如下所述:

在按钮标签上

<Button
     android:id="@+id/login_btn"
     style="@style/PrimaryButtonStyle"
     app:backgroundTint="@null"
     android:enabled="true"
     android:text="@string/txtBtnLogin" />

@Style/PrimaryButtonStyle

<style name="PrimaryButtonStyle" parent="@style/Widget.MaterialComponents.Button">
    <item name="android:layout_width">match_parent</item>
    <item name="android:layout_height">wrap_content</item>
    <item name="android:layout_marginTop">5dp</item>
    <item name="android:textColor">@color/colorPrimary</item>
    <item name="android:background">@drawable/base_button_style</item>
    <item name="textAllCaps">false</item>
    <item name="android:textSize">16sp</item>
</style>

此输出 - 按钮背景(浅蓝色)不同于布局背景(相对深蓝色)


1
投票

BackgroundTint 始终适用于材料按钮,但首先,请卸载应用程序并重新安装。有时,在您重新安装应用程序之前,更改可能不会反映出来。

android:backgroundTint
应用于
android:background
和 它们的组合可以通过
android:backgroundTintMode

来控制

请检查此答案以了解

android:background
android:backgroundTint
android:backgroundTintMode

之间的区别

https://stackoverflow.com/a/38080463/14289342


0
投票

backgroundTintMode
更改为
add
,然后将显示您的
background
属性。请参见下面的示例:

<com.google.android.material.button.MaterialButton
                android:id="@+id/bittrexJsonViewButton"
                android:layout_width="0dp"
                android:layout_height="@dimen/min_height"
                android:layout_marginStart="@dimen/half_default_margin"
                android:layout_marginEnd="@dimen/half_default_margin"
                android:text="@string/json_view"
                android:background:"#aabbcc"
                app:backgroundTintMode="add"
                app:layout_constraintBottom_toBottomOf="@+id/binanceJsonViewButton"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintStart_toEndOf="@+id/binanceJsonViewButton"
                app:layout_constraintTop_toTopOf="@+id/binanceJsonViewButton" />


0
投票

评论询问使用 colorOnSurface 禁用颜色你需要使用主题设置,

像这样:

<style name="MaterialRedButton"
    parent="Widget.MaterialComponents.Button">
    <item name="materialThemeOverlay">@style/MaterialRedButtonThemeOverlay</item>
</style>

<style name="MaterialRedButtonThemeOverlay">
    <item name="colorPrimary">@android:color/holo_red_dark</item>
    <item name="colorOnSurface">@color/white</item>
</style>

0
投票

我有同样的问题,这是它做了什么: 使用材料按钮样式中的一种样式的父级创建一个新样式,并更改其中的背景颜色和背景色调..希望它能与您一起使用..

<style name="DialogMaterialButtonOkay" parent="Widget.Material3.Button.UnelevatedButton">
        <item name="background">@color/button_background_color_main</item>
        <item name="backgroundTint">@color/button_background_color_main</item>  
</style>
© www.soinside.com 2019 - 2024. All rights reserved.