async onWindowStageCreate(windowStage: window.WindowStage): Promise<void> { // 3.为沉浸式窗口加载对应的目标页面。 windowStage.loadContent("pages/Index", (err) => { if (err.code) { console.error('Failed to load the content. Cause:' + JSON.stringify(err)); return; } console.info('Succeeded in loading the content.'); }); //1.设置全屏 //应用窗口对象 const windowClass = await windowStage.getMainWindow() //设置全屏 windowClass.setWindowLayoutFullScreen(true) //获取状态栏高度 const avoidArea = windowClass.getWindowAvoidArea(window.AvoidAreaType.TYPE_SYSTEM) //单位是px const topHeight = avoidArea.topRect.height; //将状态高度存储起来 AppStorage.setOrCreate('topHeight', px2vp(topHeight)) const prop = windowClass.getWindowProperties() hilog.info(0x0000, 'atguigu', JSON.stringify(prop)) }
修改EntryAbility.ets的
onWindowStageCreate方法修改为 async 返回参数Promise<void>
存储起来状态栏高度 在需要使用的地方取出
AppStorage.setOrCreate('topHeight', px2vp(topHeight))
取出方法:
@StorageProp('topHeight') topHeight: number = 0例如:height(211 - 36 + this.topHeight)
注:模拟器运行和预览完全不一样,需要使用模拟器去查看效果。