暗模式:useColorScheme() 在 Android 上总是返回光

And*_*dré 1 android react-native expo darkmode

我正在尝试让暗模式工作,但它在 Android 上不起作用。它总是返回“光”。在 iOS 上它工作正常。

import React from 'react';
import { useColorScheme } from "react-native";

export default function App() {
  const theme = useColorScheme();
  alert("your color scheme is: " + theme); // always returns "light" on Android
  return null;
}
Run Code Online (Sandbox Code Playgroud)

我正在使用 Expo SDK 42。

我把"userInterfaceStyle": "automatic"我的,app.json但它没有什么区别。

And*_*dré 7

我想到了。仅仅放入"userInterfaceStyle": "automatic"app.json 根目录是不够的,我还必须分别为 iOS 和 Android 定义它:

应用程序.json:

{
  "expo": {
    "userInterfaceStyle": "automatic",
    "ios": {
      "userInterfaceStyle": "automatic"
    },
    "android": {
      "userInterfaceStyle": "automatic"
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

  • 噢,孩子,这太搞笑了。它绝对应该添加到文档中或只是修复以回退到顶级值。谢谢! (3认同)