获取窗口内容规避的区域值全部都是0?

阅读 439
1 个回答

根据接口定义,没有办法确定在调用该方法时是否存在子窗口。如果需要保证一直能获取到窗口的避让区域,可以使用以下代码:

public static getStatusHeight(): Promise<number> {
return this.windowStage.getMainWindow()…then(currentWindow => {
let avoidArea = currentWindow.getWindowAvoidArea(window.AvoidAreaType.TYPE_SYSTEM)
let statusHeight = avoidArea.topRect.height;
console.log("statusHeight: ", JSON.stringify(avoidArea));
return statusHeight;
}).catch((e) => {
hilog.error(0x0000, TAG, ‘%{public}s’, 'Failed to obtain the default display object. Code: ’ + JSON.stringify(e));
return 0;
})
}

获取windowStage方式需要在entryAbility中获取:

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/Page1’, (err, data) => {
if (err.code) {
hilog.error(0x0000, ‘testTag’, ‘Failed to load the content. Cause: %{public}s’, JSON.stringify(err) ?? ‘’);
return;
}
// 将windowStage进行持久化以便子窗口调用
AppStorage.setOrCreate(“windowStage”,windowStage);
hilog.info(0x0000, ‘testTag’, ‘Succeeded in loading the content. Data: %{public}s’, JSON.stringify(data) ?? ‘’);
});
}
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进