效果展示
首先我们在EntryAbility.ets文件中的onWindowStageCreate方法中,
1. 首先获取应用主窗口,并设置为全屏,
2. 然后缓存窗口对象。
onWindowStageCreate(windowStage: window.WindowStage): void {
// Main window is created, set main page for this ability
hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onWindowStageCreate');
windowStage.loadContent('pages/Index', (err) => {
if (err.code) {
hilog.error(0x0000, 'testTag', 'Failed to load the content. Cause: %{public}s', JSON.stringify(err) ?? '');
return;
}
hilog.info(0x0000, 'testTag', 'Succeeded in loading the content.');
// 获取应用主窗口
let windowClass: window.Window = windowStage.getMainWindowSync();
// 1. 设置窗口全屏
let isLayoutFullScreen = true;
windowClass.setWindowLayoutFullScreen(isLayoutFullScreen);
//隐藏底部导航栏
windowClass.setSpecificSystemBarEnabled('navigationIndicator', false);
// 2. 缓存窗口对象
AppStorage.setOrCreate('windowClass', windowClass);
});
}
在我们需要控制显示或隐藏,状态栏与导航栏的页面
1. 导入Window对象
import { window } from '@kit.ArkUI';
2. 在页面组件内从本地存储取出我们在EntryAbility.ets文件中缓存的窗口对象
windowClass = AppStorage.get<window.Window>('windowClass') as window.Window;
3. 在页面的aboutToAppear生命周期中设置
//控制状态栏的显示或隐藏
this.windowClass.setSpecificSystemBarEnabled('status', false);
//控制导航栏的显示或隐藏
this.windowClass.setSpecificSystemBarEnabled('navigationIndicator', true);
完整
//导入window对象
import { window } from '@kit.ArkUI';
@Entry
@Component
struct Index {
//从缓存中取出window对象
windowClass = AppStorage.get<window.Window>('windowClass') as window.Window;
//生命周期中控制显示或隐藏
aboutToAppear(){
this.windowClass.setSpecificSystemBarEnabled('status', false);
this.windowClass.setSpecificSystemBarEnabled('navigationIndicator', true);
}
build() {}
}
更多沉浸式方案参考一下文档
如何设置沉浸式状态栏https://developer.huawei.com/consumer/cn/doc/harmonyos-faqs-V5/faqs-arkui-214-V5
如何实现状态栏背景颜色沉浸?http://xn--vqqw1wlubmxhftjkpci5lw1b73volc321dzeeqt1gk61r