onWindowStageCreate(windowStage: window.WindowStage): void {
// Main window is created, set main page for this ability
hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onWindowStageCreate');
// this.setFullSize(windowStage);
windowStage.loadContent('pages/PianoPage', this.localStorage, (err) => {
if (err.code) {
hilog.error(0x0000, 'testTag', 'Failed to load the content. Cause: %{public}s', JSON.stringify(err) ?? '');
return;
}
let windowClass: window.Window = windowStage.getMainWindowSync(); // 获取应用主窗口
CommonConstants.WINDOW_WIDTH = windowClass.getWindowProperties().windowRect.width / display.getDefaultDisplaySync().densityPixels;
let area = windowClass.getWindowAvoidArea(window.AvoidAreaType.TYPE_NAVIGATION_INDICATOR);
CommonConstants.NAV_HEIGHT = area.bottomRect.height / display.getDefaultDisplaySync().densityPixels;
// 1. 设置窗口全屏
let isLayoutFullScreen = true;
windowClass.setWindowLayoutFullScreen(isLayoutFullScreen)
.then(() => {
console.info('Succeeded in setting the window layout to full-screen mode.');
})
.catch((err: BusinessError) => {
console.error('Failed to set the window layout to full-screen mode. Cause:' + JSON.stringify(err));
});
// 2. 设置状态栏和导航条隐藏
windowClass.setSpecificSystemBarEnabled('status', false)
.then(() => {
console.info('Succeeded in setting the status bar to be invisible.');
})
.catch((err: BusinessError) => {
console.error(`Failed to set the status bar to be invisible. Code is ${err.code}, message is ${err.message}`);
});
});
}
如上函数,跳转到一个UIAbility,在他的onWindowStageCreate函数下执行上述操作,得到的 CommonConstants.WINDOW\_WIDTH 和CommonConstants.NAV\_HEIGHT有概率为0,特别是折叠屏全部展开的状态下。
如何才能准确获取屏幕高宽值,和导航栏高度
宽高:
导航栏: