1、在EntryAbility中对windowStage进行本地存储:
AppStorage.setOrCreate('windowStage', windowStage);
2、首页中新增如下代码:
windowStage: window.WindowStage = AppStorage.get("windowStage") as window.WindowStage;
mainWin: window.Window = this.windowStage.getMainWindowSync();
3、在首页的aboutToAppear初始化方法中写入如下代码:
aboutToAppear(): void {
// 配置Web开启调试模式
web_webview.WebviewController.setWebDebuggingAccess(true);
//深色模式_配置状态栏
let sysBarProps: window.SystemBarProperties = {
statusBarColor: '#ffffff', //背景颜色
statusBarContentColor: '#000000'//字体颜色
};
this.mainWin.setWindowSystemBarProperties(sysBarProps, (err: BusinessError) => {
let errCode: number = err.code;
if (errCode) {
console.error('[StaticUtils] Failed to set the system bar properties. Cause: ' + JSON.stringify(err));
return;
}
console.info('[StaticUtils] Succeeded in setting the system bar properties.');
});
}
注解:由于手机设置深色模式后,顶部状态栏会自动变为黑色背景色+白色字体颜色,如果这时候app内没有适配系统的深色模式,白色的字体会和app内顶部的白色背景重合,此时就会出现深色模式下,app内状态栏内的文字无法看到的bug,用上面的方法是强制给状态栏的背景设为白色,字体设为黑色,不受系统深色模式的影响,当然,如果有时间把app适配上系统的深色模式更佳!