Android材质按钮采用原色而不是色彩强调

问题描述

我的布局按钮为-

<com.google.android.material.button.MaterialButton
  android:id="@+id/save_button"
  style="@style/buttonView"
  android:text="Save"
  app:layout_constraintBottom_toBottomOf="parent"
  app:layout_constraintStart_toStartOf="parent" />

在我的styles.xml中,我有-

<style name="buttonView" parent="Theme.MyTheme">
        <item name="android:layout_width">match_parent</item>
        <item name="android:layout_height">wrap_content</item>
        <item name="android:layout_marginStart">16dp</item>
        <item name="android:layout_marginLeft">16dp</item>
        <item name="android:layout_marginTop">16dp</item>
        <item name="android:layout_marginEnd">16dp</item>
        <item name="android:layout_marginRight">16dp</item>
    </style>

在我的themes.xml中,我有-

<resources xmlns:tools="http://schemas.android.com/tools">
    <!-- Base application theme. -->
    <style name="Theme.MyTheme" parent="Theme.MaterialComponents.DayNight.DarkActionBar">
        <!-- Primary brand color. -->
        <item name="colorPrimary">@color/purple_500</item>
        <item name="android:textColorPrimary">@color/black</item>
        <item name="colorPrimaryVariant">@color/purple_700</item>
        <item name="colorOnPrimary">@color/white</item>
        <!-- Secondary brand color. -->
        <item name="colorSecondary">@color/teal_200</item>
        <item name="colorSecondaryVariant">@color/teal_700</item>
        <item name="colorOnSecondary">@color/black</item>
        <!--- Accent color. -->
        <item name="colorAccent">@color/red</item>
        <!-- Status bar color. -->
        <item name="android:statusBarColor" tools:targetApi="l">?attr/colorPrimaryVariant
        </item>
        <!-- Customize your theme here. -->
    </style>
</resources>

根据Android文档,所有UI元素(例如FAB,textview,edit text,button)都带有颜色。因此,我希望我的按钮认使用colorAccent,但为什么要使用colorPrimary。我做错什么了吗?

解决方法

您可以检查official doc

填充的按钮基于{strong> backgroundTint 具有colorPrimary
同样,以您的buttonView样式,您应该扩展所提供的样式之一:

Default style           Widget.MaterialComponents.Button
Icon style              Widget.MaterialComponents.Button.Icon
Unelevated style.       Widget.MaterialComponents.Button.UnelevatedButton
Unelevated icon style   Widget.MaterialComponents.Button.UnelevatedButton.Icon

示例:

<style name="buttonView" parent="Widget.MaterialComponents.Button">

</style>

如果要更改背景颜色,可以:

  • 以自定义样式使用backgroundTint属性

  • 使用 colorPrimary (最佳解决方案)

    以自定义样式覆盖materialThemeOverlay属性

示例:

<style name="buttonView"parent="Widget.MaterialComponents.Button">
   <item name="materialThemeOverlay">@style/CustomButtonThemeOverlay</item>
</style>

<style name="CustomButtonThemeOverlay">
  <item name="colorPrimary">@color/...</item>
</style>
,

对于我来说,将android studio更新到版本4.1 ..按钮的背景色时,会出现此问题,默认情况下始终将颜色设置为primary_purple_500。您的代码没有问题。我将backgroundTint属性设置为null并将background属性设置为喜欢的颜色,或者只是将backgroundTint设置为喜欢的颜色。

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...