Android-使用AppCompatDelegate.MODE_NIGHT_AUTO时如何检测夜间模式是否开启 [英] Android - How to detect if night mode is on when using AppCompatDelegate.MODE_NIGHT_AUTO

查看:460
本文介绍了Android-使用AppCompatDelegate.MODE_NIGHT_AUTO时如何检测夜间模式是否开启的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用内置的昼/夜模式功能的Android,我想为我的应用程序添加AppCompatDelegate.MODE_NIGHT_AUTO

I'm using Androids built in day/night mode functionality and I'd like to add an option to my app for AppCompatDelegate.MODE_NIGHT_AUTO

我有一个问题,因为我的应用程序要求某些内容需要以编程方式进行着色,而且我不知道如何检查该应用程序是否将自己视为夜间或白天模式.没有这些,我就无法设置标记来选择正确的颜色.

I'm having a problem though because my app requires certain things to be colored programmatically, and I can't figure out how to check if the app considers itself in night or day mode. Without that, I can't set a flag to choose the right colors.

调用AppCompatDelegate.getDefaultNightMode()只会返回无用的AppCompatDelegate.MODE_NIGHT_AUTO.

Calling AppCompatDelegate.getDefaultNightMode() just returns AppCompatDelegate.MODE_NIGHT_AUTO which is useless.

我看不到其他任何可以告诉我的东西,但是一定有东西吗?

I don't see anything else that would tell me, but there must be something?

推荐答案

int nightModeFlags =
    getContext().getResources().getConfiguration().uiMode &
    Configuration.UI_MODE_NIGHT_MASK;
switch (nightModeFlags) {
    case Configuration.UI_MODE_NIGHT_YES:
         doStuff();
         break;

    case Configuration.UI_MODE_NIGHT_NO:
         doStuff();
         break;

    case Configuration.UI_MODE_NIGHT_UNDEFINED:
         doStuff();
         break;
}

这篇关于Android-使用AppCompatDelegate.MODE_NIGHT_AUTO时如何检测夜间模式是否开启的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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