HarmonyOS 页面设置成了沉浸式,如何获取状态栏的高度?

拿到结果一直是0,代码如下:

window.getLastWindow(getContext(this), (err, windowClass) => {
  if (!err.code) {
    //保存窗口管理器
    this.windowClass = windowClass
    //获取状态栏高度
    console.log('获取状态栏高度', windowClass.getWindowAvoidArea(window.AvoidAreaType.TYPE_SYSTEM).topRect.height)
    this.statusHeight = px2vp(windowClass.getWindowAvoidArea(window.AvoidAreaType.TYPE_SYSTEM).topRect.height)
    //获取手机底部规避区域高度
    this.bottomAvoidAreaHeight = px2vp(windowClass.getWindowAvoidArea(window.AvoidAreaType.TYPE_NAVIGATION_INDICATOR)
      .bottomRect
      .height)
    windowClass.setWindowLayoutFullScreen(true)
  }
})
阅读 604
1 个回答

可以在entryAbility的creat方法中先获取状态栏高度,然后存到全局变量中保存起来以便后面使用。

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进