【问题标题】:Theme.MaterialComponents.DayNight not working properly on Firebase UITheme.MaterialComponents.DayNight 在 Firebase UI 上无法正常工作
【发布时间】:2021-07-13 02:02:25
【问题描述】:

我正在使用 Firebase 用户界面并使用此代码覆盖其主题。

val builder = AuthUI.getInstance().createSignInIntentBuilder()
            .setTheme(R.style.AuthStyle)
            .setLogo(R.mipmap.ic_launcher)
            .setIsSmartLockEnabled(true)
            .setAvailableProviders(getProviderList())
        builder.setTosAndPrivacyPolicyUrls(
            "https://firebase.google.com/terms/",
            "https://firebase.google.com/policies/analytics"
        )

很遗憾,在使用 深色模式 时,windowBackground 不适用,backgroundTintmaterialThemeOverlay 也不适用于按钮。

这是我完整的theme.xml 代码

<style name="AuthStyle" parent="Theme.MaterialComponents.DayNight">

        <!--Override action bar and status bar-->
        <item name="colorAccent">@color/colorAccent</item>
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>

        <item name="android:colorControlNormal">@color/colorAccent</item> <!--Spinner or non focus field underline color-->
        <item name="android:colorFocusedHighlight">@color/colorAccent</item>

        <!--Override Toolbar title text when using MaterialComponent-->
        <item name="toolbarStyle">@style/AppToolbar</item>

        <item name="materialButtonStyle">@style/AuthButton</item> <!--Customize Firebase UI button when using MaterialComponent-->

        <!--Override main background-->
        <item name="android:windowBackground">@color/whitePrimaryDark</item>

        <!--Description text color-->
        <item name="android:textColorTertiary">@color/primaryDarkWhite</item>
<!--        <item name="android:textColorPrimary">@color/colorPrimary</item>-->

        <!--Override resend code text color-->
        <item name="android:textColorSecondary">?android:textColorTertiary</item> <!--This override spinner item text too when using MaterialComponent-->

    </style>

    <!--Extend MaterialComponent Button when using MaterialComponents as app main theme-->
    <style name="AuthButton" parent="Widget.MaterialComponents.Button">
        <item name="android:minHeight">48dip</item>
        <item name="android:minWidth">64dip</item>
        <item name="android:textStyle">bold</item>
        <item name="android:textColor">@color/whitePrimaryDark</item>
        <item name="materialThemeOverlay">@style/ThemeOverlay.App.Button</item>
    </style>

    <!--Override-->
<style name="ThemeOverlay.App.Button" parent="">
    <item name="colorPrimary">@color/primaryDarkAccent</item>
</style>

这些颜色使用不同的十六进制代码,使用文件夹valuesvalues-night 分隔,其中包含两个colors.xml 文件。 Firebase UI(电话身份验证)在夜间模式下根本不尊重它。我还尝试使用与 themes.xml 相同的方法将 AuthStyle 光和夜分开,但效果不佳。

primaryDarkAccent //This color switch between (light) colorPrimaryDark and (night) colorAccent
whitePrimaryDark //This color switch between (light) @android:color/white and (night) colorPrimaryDark
primaryDarkWhite //This color switch between (light) colorPrimaryDark and (night) colorAccent @android:color/white

对于按钮着色,它只使用在AuthStyle 上定义的colorPrimary,而不是我定义的带有叠加层的颜色值,但这仅在浅色模式期间使用。黑暗/夜间模式似乎并没有真正遵循定义的颜色。

【问题讨论】:

  • 我不确定,但 AuthUI 应该适用于 AppCompat 主题。尝试覆盖 ThemeOverlay.App.Button 中的 colorAccent
  • @GabrieleMariotti 没有任何变化,它似乎不承认我在values-night 文件夹下的颜色值,并且只使用values 这是光而不是夜晚。
  • 另外windowBackground 在黑暗模式下也使用普通的深色而不是我定义的颜色。
  • 使用布局检查器检查按钮的类型。如果它是 AppCompatButton,则不会在应用主题中使用 materialButtonStyle
  • @GabrieleMariotti 谢谢我已经弄清楚出了什么问题

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


【解决方案1】:

愚蠢,我错过了在 Application.class 中使用SharedPreference 选择主题的部分,AppCompatDelegate

如果您的设备在新安装的应用或基于设备设置启用Dark Mode。 您还需要使用UIMode API 和用户偏好来检查它。

例子

val systemNightMode = (resources.configuration.uiMode
            and Configuration.UI_MODE_NIGHT_MASK)

    val prefs = getSharedPreferences(getString(R.string.app_name), MODE_PRIVATE)
    val userPrefModeIsNight = prefs.getBoolean(getString(R.string.action_settings), false)

    // This will be the top level handling of theme
    AppCompatDelegate.setDefaultNightMode(
        if (userPrefModeIsNight || systemNightMode == Configuration.UI_MODE_NIGHT_YES)
            AppCompatDelegate.MODE_NIGHT_YES
        else
            AppCompatDelegate.MODE_NIGHT_NO)

正如您在此处看到的,我们还通过设备设置检查应用是否被强制使用Dark/Night。现在由您决定关注哪一个,使用SharedPref 或设备设置的用户偏好。

【讨论】:

    猜你喜欢
    • 2019-01-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-01-19
    相关资源
    最近更新 更多