材料3材料thereoverlay生成没有主题的明亮粉红色按钮

发布于 2025-01-21 16:25:42 字数 1394 浏览 0 评论 0 原文

我正在从M2迁移到M3,而我的基本材料themeoverlay 不再有效。我在 doc 紧密相匹配的情况下,我几乎与我所拥有的东西匹配对于m2:

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

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

”在此处输入图像说明“

lib版本是 com.google.android.material:材料:1.6.0-beta01 ,活动是AppCompatactivity。

App主题父母是 theme.material3.daynight.noactionbar

另请注意,设置 backgroundTint 正常工作。

I am migrating from M2 to M3 and my basic materialThemeOverlay no longer works. I follow the code at the bottom of the doc closely, it matched pretty much what I had for M2:

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

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

enter image description here

lib version is com.google.android.material:material:1.6.0-beta01, and the Activity is an AppCompatActivity.

App theme parent is Theme.Material3.DayNight.NoActionBar.

Also note that setting the backgroundTint works fine.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

烙印 2025-01-28 16:25:42

您应该使用 themoverlay 材料themeoverlay 之类的上。

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

在主题中使用它。

<item name="materialButtonStyle">@style/AppButtonStyle</item>

不幸的是,文档中的样本一如既往地不正确。

You should use ThemeOverlay on materialThemeOverlay like this.

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

Use it like this in you main theme.

<item name="materialButtonStyle">@style/AppButtonStyle</item>

Unfortunately the sample in the documentation seems incorrect as always.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文