当我将移动主题从浅色更改为深色时,它会影响我的反应本机应用程序的背景颜色。我只是希望它始终保持白色,但当我将移动主题从浅色更改为深色时,它会从白色变为黑色。我找到了 React Native without Expo: React Native android dark mode issues的解决方案,但我无法使用它,因为我没有Android文件夹。
直到今天我也遇到了同样的问题,在阅读了很多文档后我发现了这一点:
https://docs.expo.dev/guides/color-schemes/
在您的 app.json 文件上:
{
"expo": {
"userInterfaceStyle": "light",
"ios": {
"userInterfaceStyle": "light"
},
"android": {
"userInterfaceStyle": "light"
}
}
}
Run Code Online (Sandbox Code Playgroud)
之后,不要忘记安装expo-system-ui. 要检查一切是否正常,您可以运行SKD 45+ 的expo config --type introspect或。npx expo config --type introspect
但这并不能解决小米设备的问题。为了解决这个设备的问题,我们需要一个附加配置,如下所示:
在根目录(与 app.json 相同的目录)中创建一个名为 的文件夹plugin;
添加一个名为withDisableForcedDarkModeAndroid.js
在这个新文件中添加以下代码:
const {
createRunOncePlugin,
withAndroidStyles,
AndroidConfig
} = require('@expo/config-plugins');
function setForceDarkModeToFalse(styles) {
styles = AndroidConfig.Styles.assignStylesValue(styles, {
add: true,
parent: AndroidConfig.Styles.getAppThemeLightNoActionBarGroup(),
name: `android:forceDarkAllowed`,
value: "false",
});
return styles;
}
const withDisableForcedDarkModeAndroid = (config) => {
return withAndroidStyles(config, (config) => {
config.modResults = setForceDarkModeToFalse(config.modResults);
return config;
});
};
module.exports = createRunOncePlugin(withDisableForcedDarkModeAndroid, 'disable-forced-dark-mode', '1.0.0');
Run Code Online (Sandbox Code Playgroud)
之后,在app.json文件中添加新插件expo.config.plugin:
{
"expo": {
[...]
"userInterfaceStyle": "light",
"ios": {
[...]
"userInterfaceStyle": "light",
},
"android": {
[...]
"userInterfaceStyle": "light",
},
"plugins": [
[
"./plugins/withDisableForcedDarkModeAndroid.js",
{}
]
]
}
}
Run Code Online (Sandbox Code Playgroud)
就是这样!在这里工作:D 我在这里找到了这段代码: https ://gist.github.com/hirbod/d6bb5d0fc946a12ba9e3cf01120c604a
提前感谢作者!
PS.1:userInterfaceStyle- 可用选项有:自动(遵循系统外观设置并通知用户所做的任何更改)、浅色(限制应用程序仅支持浅色主题)和深色(限制应用程序仅支持深色主题)。
PS.2:由于小米设备的配置,此特定配置是必要的android:forceDarkAllowed=true。仅通过添加 Expo 团队提出的配置不会覆盖此配置。
| 归档时间: |
|
| 查看次数: |
6500 次 |
| 最近记录: |