如果Android设备处于黑暗模式,我如何以编程方式进行检测? [英] How can I detect programmatically if the Android Device is in Dark Mode?

查看:121
本文介绍了如果Android设备处于黑暗模式,我如何以编程方式进行检测?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试为我的Android应用程序支持Android Q Dark主题,但我不知道如何根据当前使用的主题导入不同的资产.

I'm trying to support the Android Q Dark theme for my Android app and I can't figure out how to import different assets based on the theme I'm currently in.

我使用正式的DayNight主题制作深色/浅色版本和可绘制对象很容易指向XML,并且它将根据启用的内容从值或值-night中选择正确的值.

Im using the official DayNight theme for making the dark/light versions and for drawables is very easy to just point to the XML and it will choose the correct value either from values or values-night depending on what is enabled.

我想做类似的事情,具体取决于主题,它将加载资产"priceTag_light.png"或"priceTag_dark.png".

I wanted to do something similar where depending on the theme it would load either the asset "priceTag_light.png" or "priceTag_dark.png".

val inputStream = if(darkIsEnabled) { 
                    assets.open("priceTag_dark.png")
                  } else {
                    assets.open("priceTag_light.png")
                  }

有没有办法获得该标志?

Is there a way I get that flag?

推荐答案

好吧,终于找到了我想要的解决方案.正如 @ deepak-s-gavkar 所指出的那样,该参数使我们知道配置中的信息.因此,经过一番搜索,我发现文章提供了此示例方法完全可以满足我的需求:

Okay finally found the solution I was looking for. As @deepak-s-gavkar points out the parameter that gives us that information is on the Configuration. So, after a small search I found this article that gives this example method that has worked perfectly for what I wanted:

fun isDarkTheme(activity: Activity): Boolean {
        return activity.resources.configuration.uiMode and
                Configuration.UI_MODE_NIGHT_MASK == Configuration.UI_MODE_NIGHT_YES
    }

这篇关于如果Android设备处于黑暗模式,我如何以编程方式进行检测?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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