ContextCompat.getColor方法将忽略夜间模式 [英] ContextCompat.getColor method ignores Night Mode

查看:501
本文介绍了ContextCompat.getColor方法将忽略夜间模式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为我的Android应用程序使用夜间模式. 我正在使用ContextCompat.getColor以编程方式获取某些UI元素的颜色,但是这种方法无法获取正确的颜色. 当应用程序处于夜间模式时,为了遵守夜间资源限定符,ContextCompat从values/colors.xml而不是从values-night/colors.xml中获取颜色.

I am working on Night mode for my Android app. I'm using ContextCompat.getColor to get colours programmatically for some UI elements, however this method it's not fetching the correct colour. When the app is in Night mode, so adhering to the night resource qualifier, ContextCompat fetches the color from values/colors.xml and not values-night/colors.xml.

好奇地,如果我从Activity调用ContextCompat.getColor,它将为我返回正确的颜色

Curiosly, if I call ContextCompat.getColor from an Activity, it returns me the correct color

//get Actual Theme Colors
String bgColor = String.format("#%06X", (0xFFFFFF & ContextCompat.getColor(this, R.color.dialogBackground)));
String txtColor = String.format("#%06X", (0xFFFFFF & ContextCompat.getColor(this, R.color.dialogText)));

相反,如果我从Activity内的Fragment调用ContextCompat.getColor,它将返回错误的颜色

Instead, if I call ContextCompat.getColor from a Fragment inside an Activity, it returns the wrong color

//get Actual Theme Colors
bgColor = String.format("#%06X", (0xFFFFFF & ContextCompat.getColor(getActivity(),R.color.dialogBackground)));
txtColor = String.format("#%06X", (0xFFFFFF & ContextCompat.getColor(getActivity(),R.color.dialogText)));

我正在使用'androidx.appcompat:appcompat:1.0.2'lib

I am using 'androidx.appcompat:appcompat:1.0.2' lib

我不知道它是否与此错误

我希望您能找到一种解决方案或至少一种解决方法,例如从特定配置的资源(如果可能)中获取getColor.

I hope you will find a solution or at least a workaround, such as getColor from a resource for a specific configuration (if possibile).

推荐答案

解决了问题,但我在这里是为了帮助其他遇到相同问题的人.

Solved the problem, but I am here to help others with same issues.

奇怪的行为是由Webview引起的.如此链接所述,当您首次打开Webview然后交换片段时或在用户界面中添加新视图,它们将使用错误的颜色,而不尊重夜间主题. 如果以编程方式获取颜色,则会出现相同的问题. 我尝试了一些变通办法,例如重新创建活动或使资源缓存无效,它们实际上解决了问题,但在性能方面确实很差.

The strange behavior was caused by a Webview. As stated on this link, when you open for the first time a Webview and then swapping a fragment or adding new views to the UI, they will use the wrong colors, without respecting the night theme. Same issue if you get the color programmatically. I tried some workarounds such as recreate Activity or invalidate resources cache and they actually solved the problem but they are really bad in terms of performance.

至少部分可以升级到 androidx.appcompat:appcompat:1.1.0-rc01 . 如果您在应用程序的主题或布局xml中有一些未定义颜色的视图,则它们在首次启动Webview并更改片段时仍会出现故障并使用错误的颜色. 因此,您应该使用资源变体(-night)作为夜晚的颜色,在应用程序的主题中定义视图的每种颜色. 如果您这样做,则视图将遵循您的主题并使用正确的颜色

Upgrading to androidx.appcompat:appcompat:1.1.0-rc01 fixes the issue, at least partially. If you have some Views without color defined, either in the theme of the app or in the layout xml, then they will still glitch and use wrong colors when launching a webview for the first time and then change fragment. So, you should define every color of your views in the theme of the app using resource variant (-night) for the night colors. If you do this, Views will follow your theme and use the correct colors

TL; DR

  • 升级到 androidx.appcompat:appcompat 1.1.0-rc01 或最新版本

检查是否仍然存在问题

如果是,则使用-night变体为深色主题为主题或xml中尚未定义颜色的每个视图设置颜色

If yes, then set colors for each view you have not defined a color in the theme or xml, using the -night variant for the dark theme

这篇关于ContextCompat.getColor方法将忽略夜间模式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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