HarmonyOS 关于屏幕高度?

display.getDefaultDisplaySync 可以获取到整个屏幕的高度, 那顶部的状态栏和底部的安全区高度 怎么获取?

阅读 601
1 个回答

可以在EntryAbility里获取并存储,获取到的高度是px,所以用px2vp()转换为vp使用:

onWindowStageCreate(windowStage: window.WindowStage) {
  // Main window is created, set main page for this ability
  hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onWindowStageCreate');

  windowStage.loadContent('pages/Index', (err, data) => {
    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. Data: %{public}s', JSON.stringify(data) ?? '');

    let windowClass = windowStage.getMainWindowSync();
    let statusHeight = windowClass.getWindowAvoidArea(window.AvoidAreaType.TYPE_SYSTEM).topRect.height;
    let bottomHeight = windowClass.getWindowAvoidArea(window.AvoidAreaType.TYPE_NAVIGATION_INDICATOR).bottomRect.height;
    AppStorage.setOrCreate('bottomHeight',px2vp(bottomHeight));
    AppStorage.setOrCreate('statusHeight',px2vp(statusHeight));
  });
}

在需要的界面使用 AppStorage.get('bottomHeight'),AppStorage.get('statusHeight') 获取。
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进