【问题标题】:Material colorOnSurface ignored in dark theme在深色主题中忽略材质 colorOnSurface
【发布时间】:2021-05-03 18:03:54
【问题描述】:

Material 主题似乎仅在暗模式下忽略 colorOnSurface

值/themes.xml:

<style name="Theme.MyApp.Base" parent="Theme.MaterialComponents.DayNight">
...
</style>
<style name="Theme.MyApp" parent="Theme.MyApp.Base">
    <item name="colorSurface">@color/white</item>
    <item name="colorOnSurface">@color/black</item>
</style>

values-night/themes.xml:

<style name="Theme.MyApp" parent="Theme.MyApp.Base">
    <item name="colorSurface">@color/black</item>
    <item name="colorOnSurface">@color/white</item>
</style>

在三星平板电脑 (Android 10) 和模拟器 (Android 10) 上进行测试

  • 在浅色模式下,MaterialToolbar 菜单按预期呈现:白色背景上的黑色文本。
  • 在深色模式下,MaterialToolbar 菜单以黑色背景上的 黑色 文本呈现。

colorOnSurface 似乎完全被忽略了。

救命!!

【问题讨论】:

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


    【解决方案1】:

    找到了答案。问题是在我的基本风格中,我定义了:

    <style name="Theme.MyApp.Base" parent="Theme.MaterialComponents.DayNight"/>
    <style name="Theme.MyApp" parent="Theme.MyApp.Base">
        <item name="colorSurface">@color/white</item>
        <!-- Probably WOULD have controlled menu item color if we didn't override on the next line-->
        <item name="colorOnSurface">@color/black</item>
        <!-- potentially override colorOnSurface for all subtitle 1's. WHICH components use subtitle1? You'll have to guess -->
        <item name="textAppearanceSubtitle1">@style/MyApp.TextAppearance.Subtitle1</item>
    </style>
    <!-- Default style for menu items. Intuition be damned, the only way to learn this is by scouring the material design docs line by line -->
    <style name="MyApp.TextAppearance.Subtitle1" parent="TextAppearance.MaterialComponents.Subtitle1">
        <item name="android:textColor">@color/black</item>
    </style>
    

    显然,如果您使用颜色定义 textAppearanceSubtitle1,则该颜色将覆盖 colorOnSurface

    为了支持深色模式,我还必须在我的深色主题中定义 textAppearanceSubtitle1

    values-night/themes.xml

    <style name="Theme.MyApp" parent="Theme.MyApp.Base">
        <item name="colorSurface">@color/black</item>
        <!-- probably WOULD control menu color, if we hadn't defined textAppearanceSubtitle1 -->
        <item name="colorOnSurface">@color/white</item>
    </style>
    <!-- override light theme style -->
    <style name="MyApp.TextAppearance.Subtitle1" parent="TextAppearance.MaterialComponents.Subtitle1">
        <item name="android:textColor">@color/white</item>
    </style>
    

    我不禁再次想知道,究竟如何期望任何理智的开发人员学习如何正确设置 Android 应用程序的样式,其主题和样式相互干扰和重叠,并且材料设计无穷无尽猜测和痛苦的泥潭。

    【讨论】:

    • 这不是很明显,通过在主题中设置 textAppearanceSubtitle1 ,您会覆盖整个主题的所有 Subtitle1 元素的 textAppearances 吗?为 Android 应用程序设计样式,尤其是使用 MaterialComponents,相当困难,但事实并非如此
    • @d.u.a.l 你说得对,情况并非如此。我的观察是设置 textAppearanceSubtitle1 似乎覆盖了 colorOnSurface,这对我来说并不明显。
    猜你喜欢
    • 2016-01-25
    • 2021-08-14
    • 2018-10-12
    • 2019-11-19
    • 1970-01-01
    • 2021-07-20
    • 1970-01-01
    • 1970-01-01
    • 2021-02-07
    相关资源
    最近更新 更多