简体   繁体   English

React Native实时改变配色方案问题android

[英]React native change color scheme in real time problem android

What I'm doing is giving the user the possibility to change the color of the device scheme ( light or dark ) in real time.我正在做的是让用户可以实时更改设备方案的颜色( light or dark )。

On ios it works, when on the device I press the key combination: Command + Uppercase + A .ios上它可以工作,当我在设备上按下组合键时: Command + Uppercase + A

Event is intercepted, and the theme is changed in real time.事件被截取,主题实时变化。

When on Android I try to change the theme color from the button menu, as you can see from the image.Android上时,我尝试从按钮菜单中更改主题颜色,如图所示。

在此处输入图像描述

The event is not intercepted I have to exit the app for a moment and then re-enter to make the changes happen.事件未被拦截我必须退出应用程序一会儿,然后重新进入以进行更改。

I'm using import {useColorScheme} from 'react-native' .我正在使用import {useColorScheme} from 'react-native'

I also tried import {useColorScheme} from 'react-native-appearance' , but on ios the realtime thing doesn't work.我也试过import {useColorScheme} from 'react-native-appearance' ,但在 ios 上,实时的东西不起作用。

How can I solve?我该如何解决?

I had a similar issue, on iOS the theme change worked fine however on Android it did not trigger a re-render.我有一个类似的问题,在 iOS 上,主题更改工作正常,但是在 Android 上它没有触发重新渲染。

I fixed this by doing the following thing: Create an appstate listener which detects when an app is coming back from the background to the foreground .我通过执行以下操作解决了这个问题:创建一个appstate侦听器,用于检测应用何时从后台返回到前台

Then in the listener function I checked if the current theme is equal to the theme selected in the device.然后在监听器 function 我检查当前主题是否等于设备中选择的主题。 If the theme changed I updated my redux store which contains my colorScheme.如果主题改变了,我更新了包含我的 colorScheme 的 redux 商店。

And by doing that all my components subscribed to my reduxstate just re-renderd.通过这样做,我订阅了我的 reduxstate 的所有组件都重新渲染了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM