【问题标题】:Android Theming/Colors: Accent color completely invisible when setting a primary colorAndroid 主题/颜色:设置原色时,强调色完全不可见
【发布时间】:2020-07-29 17:55:50
【问题描述】:

关于我的应用的一些概述:我正在使用 Google Material Components 库,所以我的应用主题有父 Theme.MaterialComponents.DayNight

我遇到的问题是我的主题的颜色。这是一个问题的主要地方是在我定义的 DialogFragment 中。如果我这样声明我的主题,并设置明确的主要颜色、主要颜色和强调颜色:

<style name="DarkTheme" parent="Theme.MaterialComponents.DayNight">
    <item name="colorPrimary">@color/colorDarkPrimary</item>
    <item name="colorPrimaryDark">@color/colorDarkPrimaryDark</item>
    <item name="colorAccent">@color/colorDarkAccent</item>
</style>

那么我的 DialogFragment 的按钮是完全不可见的(它们应该是 colorAccent):

如果我将所有颜色声明从主题中注释掉,那么按钮就会出现:

问题当然是我希望能够定义自己的颜色。我试过只定义colorPrimary 而没有定义colorPrimaryDarkcolorAccent,但这会导致重音文本由于某种原因完全不可见的相同问题。

【问题讨论】:

    标签: android styles material-design


    【解决方案1】:

    我将保留此内容,以便其他人可以找到它,但答案始终在文档中。

    继承任何Theme.MaterialComponents 会更改您必须赋予颜色的名称。您必须使用colorSecondary,而不是使用默认 Android 主题的 colorAccent

    见: https://github.com/material-components/material-components-android/blob/master/docs/theming/Color.md

    这是问题的一半。另一半是,在使用 MaterialComponents 时,最好使用MaterialAlertDialogBuilder。所以我将我的对话框生成器切换为这样定义:

    val builder = MaterialAlertDialogBuilder(it, R.style.AlertDialogTheme)
    

    最后一步是R.style.AlertDialogTheme 声明。在styles.xml 中,我定义了该主题来设置警报对话框的样式,它看起来像这样:

    <style name="AlertDialogTheme" parent="Theme.MaterialComponents.DayNight.Dialog.Alert">
        <item name="buttonBarPositiveButtonStyle">@style/AlertButton</item>
        <item name="buttonBarNegativeButtonStyle">@style/AlertButton</item>
        <item name="buttonBarNeutralButtonStyle">@style/AlertButton</item>
    </style>
    

    @style/AlertButton 所做的只是将文本颜色设置为我们想要的颜色:

    <style name="AlertButton" parent="Widget.MaterialComponents.Button.TextButton">
        <item name="android:textColor">@color/colorDarkAccent</item>
    </style>
    

    最后,对话框按钮使用你想要的颜色。

    【讨论】:

    • 使用MaterialAlertDialog,您应该使用ThemeOverlay.MaterialComponents.MaterialAlertDialog 作为父级,并且按钮样式应该继承自Widget.MaterialComponents.Button.TextButton.Dialog
    猜你喜欢
    • 2012-07-31
    • 2018-11-05
    • 2021-11-29
    • 2019-10-27
    • 1970-01-01
    • 1970-01-01
    • 2017-08-12
    • 1970-01-01
    • 2018-02-14
    相关资源
    最近更新 更多