Android Q中的深色主题配置更改 [英] Dark theme configuration change in Android Q

查看:168
本文介绍了Android Q中的深色主题配置更改的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在我的应用中实现android 10 dark主题,我有以下几种情况:

I want to implement android 10 dark theme in my app , I have these following cases:

SYSTEM_DEFAULT, NIGHT_MODE, LIGHT_MODE

问题是当我从应用程序内部将主题从夜或光更改为system_default时,它无法理解系统是处于亮模式还是暗模式.因此主题将不会更新.

The problem is when I change theme from night or light to system_default from inside the app and it can not understand whether the system is in light mode or dark mode. so the theme won't be updated.

我已经尝试过Google的深色主题 https://developer.android.com/guide/topics/ui/look-and-feel/darktheme

I've tried the dark theme by google https://developer.android.com/guide/topics/ui/look-and-feel/darktheme

并实施配置仍然对我不利,因为如果用户将day更改为system default且当系统默认值为day时,我不想重新创建活动.

and implementing the configuration still won't be good for me because I don't want to recreate my activity if user changes day to system default when system default is day.

反正我可以应付吗?

when(id) {
  NIGHT - > theme = Theme.NIGHT_MODE
  DAY - > theme = Theme.LIGHT_MODE
  SYSTEM_DEFAULT - > theme = Theme.SYSTEM_DEFAULT
}

context ? .clearCachedDrawables()
activity ? .recreate()
}

when (themeStatus) {
            Theme.LIGHT_MODE ->
                AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO)
            Theme.NIGHT_MODE ->
                AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES)
            Theme.SYSTEM_DEFAULT ->
               AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_FOLLOW_SYSTEM)
        }

推荐答案

您无需在活动中设置主题并重新创建.如果您正确设置了应用主题,则会自动完成.

You don't need to set the theme in your activity and recreate it. It's done automatically if you've setup your app theme right.

要在应用程序中使用Dark,应将DayNight主题扩展为您的应用程序主题.

To use the Dark in your app, you should extend the DayNight theme as your app theme.

<style name="AppTheme" parent="Theme.AppCompat.DayNight"> 

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

例如,如果要在黑暗模式下使用其他颜色,则应创建一个名为values-night的"Android资源目录",并使用一个名为colors.xml的资源文件

If you want for example a different color during dark mode, you should create a 'Android resource directory' called values-night with a 'resource file called colors.xml

在colors.xml中,您可以为现有颜色之一设置不同的颜色十六进制.

In the colors.xml you can set a different color hex for one of your existing colors.

例如:

values/colors.xml包含

values/colors.xml contains

<color name="myColor">#000000</color>

values-night/colors.xml包含

values-night/colors.xml contains

<color name="myColor">#FFFFFF</color>

编辑

您可以通过调用在应用中的暗/亮模式之间切换

You can switch between Dark/light mode in app by calling

AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_FOLLOW_SYSTEM)

AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES)

AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO)

这篇关于Android Q中的深色主题配置更改的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆