Theme.MaterialComponents.DayNight 在 Firebase UI 上无法正常工作



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

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>

这些颜色使用不同的十六进制代码,该十六进制代码使用文件夹values和包含两个colors.xml文件的values-night分隔。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

对于按钮着色,它仅使用colorPrimaryAuthStyle而不是我定义的带有叠加层的颜色值,但这仅在光照模式下。深色/夜间模式似乎并没有真正遵循定义的颜色。

愚蠢的,我错过了使用SharedPreference在应用程序中选择带有AppCompatDelegate的主题的部分.class。

如果您的设备在新安装的应用程序上启用Dark Mode或基于设备设置。 您还需要使用UIModeAPI 以及用户首选项进行检查。

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或设备设置的用户首选项。

最新更新