【问题标题】:Can't change background color on MaterialButton without change colorAccent无法更改 MaterialButton 上的背景颜色而不更改 colorAccent
【发布时间】:2019-07-31 21:22:59
【问题描述】:

Android Studio 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 的 背景,我在 styles.xml

中更改 colorAccent
<item name="colorAccent">@color/colorAccent</item>

很好。这是工作。

但问题是:我不想更改 colorAccent。我想为 MaterialButton's 使用不同于 colorAccent

的背景颜色

属性:

android:background="#aabbcc"

没有帮助。

【问题讨论】:

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


    【解决方案1】:

    第一种解决方案

    您可以使用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。因此,您可以在主题中定义它并将其应用于材质按钮

    【讨论】:

    • 我不能使用 app:backgroundTint 因为 minSdkVersion 18
    • 尝试第二种解决方案
    • 我已经用最低版本 18 试过了。没有问题。我认为您没有使用androidX。如果您想使用新的材料组件,那么我建议您将您的项目迁移到 AndroidX
    • 我迁移到 AndroidX。如果您在 Android 5.0 上运行应用程序,则按钮不会更改背景颜色。在这种情况下 app:backgroundTint - 没有帮助。
    • 是的,你必须在 Lolipop 和 Pre Lolipop 版本上使用 AppCompat 主题,AFIK
    【解决方案2】:

    使用MaterialButton,您有两个选择:

    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 版本的库。

    【讨论】:

    • 需要材质1.1.0,如图here不存在
    • @DarioColetto 但解决方案有效。存在可以从中派生按钮样式的主题和样式。我刚刚试过了。
    • 嗯,答案是从 2019 年开始的,我的评论也是,问题是材料 1.1.0 还没有发布稳定...现在它当然可用:)
    • 这应该是公认的答案。在我看来,这是我们都应该知道的事情。创建样式,从父样式扩展,并且只更改一个属性。谢谢
    【解决方案3】:

    如果要设置自定义可绘制对象,则需要创建 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" />
    

    【讨论】:

      【解决方案4】:

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

      它应该在 1.2.0 beta 1 上发布,因为GitHub issue was closed as "Fixed"

      【讨论】:

        【解决方案5】:

        你可以按照下面的代码来做。

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

        【讨论】:

        • 那行得通。你拯救了我的一天,谢谢!
        【解决方案6】:

        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 并且想要更改所有按钮的主题:

        将此行添加到您的主题.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

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

        【讨论】:

          【解决方案7】:

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

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

          请检查此答案以了解 android:backgroundandroid:backgroundTintandroid:backgroundTintMode 之间的区别

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

          【讨论】:

            【解决方案8】:

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

            【讨论】:

              【解决方案9】:

              关于使用 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>
              

              【讨论】:

                【解决方案10】:

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

                在按钮标签上

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

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

                【讨论】:

                  猜你喜欢
                  • 1970-01-01
                  • 2013-03-14
                  • 2016-10-04
                  • 2014-09-25
                  • 1970-01-01
                  • 1970-01-01
                  • 2014-09-13
                  • 1970-01-01
                  • 1970-01-01
                  相关资源
                  最近更新 更多