HarmonyOS 关于屏幕高度

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

HarmonyOS
2024-12-20 15:28:35
浏览
收藏 0
回答 1
待解决
回答 1
按赞同
/
按时间
Excelsior_abit

可以在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') 获取。
分享
微博
QQ
微信
回复
2024-12-20 17:35:13
相关问题
HarmonyOS 关于屏幕高度
401浏览 • 0回复 待解决
HarmonyOS 屏幕的默认高度咨询
115浏览 • 1回复 待解决
获取设备屏幕的宽度和高度
982浏览 • 1回复 待解决
关于设置屏幕常亮问题
1208浏览 • 1回复 待解决
关于不同屏幕UI适配方案
664浏览 • 1回复 待解决
关于屏幕安全区域的问题咨询
714浏览 • 1回复 待解决
HarmonyOS 屏幕锁屏和解锁屏幕
501浏览 • 1回复 待解决
HarmonyOS web的高度自适应内容的高度
610浏览 • 1回复 待解决