当Android主题设置为暗模式时,如何在React Native应用中激活暗模式?

时间:2019-10-13 12:37:27

标签: android react-native android-dark-theme

我正在开发一个支持深色主题的React Native应用。

我使用了React导航,并使用React上下文创建了自定义主题。现在,我可以使用按钮在暗模式和亮模式之间切换。但是我想在打开应用程序时使用android主题(例如:暗模式)。这意味着,如果在Android中启用了暗模式,则我的应用要在打开应用时启用暗模式。

2 个答案:

答案 0 :(得分:2)

Facebook仍在努力增加外观支持: https://github.com/facebook/react-native/commit/63fa3f21c5ab308def450bffb22054241a8842ef

在此之前,我建议您使用类似https://github.com/expo/react-native-appearance

然后,您可以检索设备的外观设置并加载适当的样式表,对于非黑暗模式或不支持它的设备,可能会有一个后备选项。

答案 1 :(得分:0)

您可以使用react-native-appearance

import { AppearanceProvider, Appearance } from 'react-native-appearance';

export default () => (
  <AppearanceProvider>
    <App />
  </AppearanceProvider>
);

let colorScheme = Appearance.getColorScheme();

let subscription = Appearance.addChangeListener(({ colorScheme }) => {
  // do something with color scheme
});