Material 3materialThemeOverlay 生成没有主题的亮粉色按钮

Dan*_*son 5 android android-theme material-design material-components material-components-android

我正在从 M2 迁移到 M3,但我的基本功能materialThemeOverlay不再起作用。我密切关注文档底部的代码,它与我对 M2 的代码非常匹配:

样式.xml

<style name="SecondaryThemeOverlay" parent="">
    <item name="colorPrimary">@color/md_theme_light_secondary</item>
    <item name="colorOnPrimary">@color/md_theme_light_onSecondary</item>
</style>

<style name="Widget.Button.Secondary" parent="Widget.Material3.Button">
    <item name="materialThemeOverlay">@style/SecondaryThemeOverlay</item>
</style>
Run Code Online (Sandbox Code Playgroud)

布局.xml:

<Button
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:text="Standard Button" />

<Button
  style="@style/Widget.Button.Secondary"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:text="Standard Button Secondary" />
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

lib 版本是com.google.android.material:material:1.6.0-beta01,Activity 是 AppCompatActivity。

应用程序主题父级是Theme.Material3.DayNight.NoActionBar.

另请注意,设置backgroundTint效果很好。

Bit*_*EVS 5

你应该ThemeOverlaymaterialThemeOverlay这样使用。

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <!-- Button style on both light and night mode -->
    <style name="AppButtonStyle" parent="Widget.Material3.Button">
        <item name="materialThemeOverlay">@style/ThemeOverlay.App.Button</item>
        <item name="android:textAppearance">@style/TextAppearance.App.Button</item>
        <item name="shapeAppearanceOverlay">@style/ShapeAppearance.App.SmallComponent</item>
    </style>

    <!-- Button with text style on both light and night mode -->
    <style name="AppButtonTextStyle" parent="Widget.Material3.Button.TextButton">
        <item name="materialThemeOverlay">@style/ThemeOverlay.App.Button.TextButton</item>
        <item name="android:textAppearance">@style/TextAppearance.App.Button</item>
        <item name="shapeAppearanceOverlay">@style/ShapeAppearance.App.SmallComponent</item>
    </style>

    <!-- Button outline style on both light and night mode -->
    <style name="AppOutlinedButtonStyle" parent="Widget.Material3.Button.OutlinedButton">
        <item name="materialThemeOverlay">@style/ThemeOverlay.App.Button.OutlinedButton</item>
        <item name="android:textAppearance">@style/TextAppearance.App.Button</item>
        <item name="shapeAppearanceOverlay">@style/ShapeAppearance.App.SmallComponent</item>
    </style>

    <style name="ThemeOverlay.App.Button" parent="ThemeOverlay.Material3.Button">
        <!-- Background color -->
        <item name="colorPrimary">@color/primaryDarkAccent</item>
        <!-- Text color -->
        <item name="colorOnPrimary">@android:color/white</item>
    </style>

    <style name="ThemeOverlay.App.Button.TextButton" parent="ThemeOverlay.Material3.Button.TextButton">
        <!-- Background color -->
        <item name="colorPrimary">@color/primaryDarkAccent</item>
        <!-- Text color -->
        <item name="colorOnSurface">@android:color/white</item>
    </style>

    <style name="ThemeOverlay.App.Button.OutlinedButton" parent="ThemeOverlay.Material3.Button">
        <!-- Background color -->
        <item name="colorOnSurface">@color/primaryDarkAccent</item>
        <!-- Text color -->
        <item name="colorPrimary">@color/primaryDarkAccent</item>
    </style>

    <style name="TextAppearance.App.Button" parent="TextAppearance.Material3.LabelLarge">
        <item name="fontFamily">sans-serif-medium</item>
        <item name="android:fontFamily">sans-serif-medium</item>
    </style>
</resources>
Run Code Online (Sandbox Code Playgroud)

在你的主题中像这样使用它。

<item name="materialButtonStyle">@style/AppButtonStyle</item>
Run Code Online (Sandbox Code Playgroud)

不幸的是,文档中的示例似乎一如既往地不正确。