HarmonyOS:获取屏幕顶部状态栏、底部导航栏和导航条的高度

获取屏幕顶部状态栏、底部导航栏和导航条的高度
可以使用window的getWindowAvoidArea方法获取

测试结果图

示例TestGetWindowAvoidArea.ets文件代码

typescript 复制代码
import { window } from '@kit.ArkUI'
import { BusinessError } from '@kit.BasicServicesKit';

@Entry
@Component
struct TestGetWindowAvoidArea {
  @State message: string = '获取屏幕顶部状态栏、底部导航栏和导航条的高度';

  getAvoidAreaHeight() {
    let type1 = window.AvoidAreaType.TYPE_SYSTEM;
    let type2 = window.AvoidAreaType.TYPE_NAVIGATION_INDICATOR;
    window.getLastWindow(getContext(this)).then((data) => {
      // 获取系统默认区域,一般包括状态栏、导航栏
      let avoidArea1 = data.getWindowAvoidArea(type1);
      // 顶部状态栏高度
      let statusBarHeight = avoidArea1.topRect.height;
      // 底部导航栏高度
      let bottomNavHeight = avoidArea1.bottomRect.height;
      // 获取导航条区域
      let avoidArea2 = data.getWindowAvoidArea(type2);
      // 获取到导航条区域的高度
      let indicatorHeight = avoidArea2.bottomRect.height;
      console.info(`屏幕顶部状态栏 statusBarHeight = ${statusBarHeight}`);
      console.info(`底部导航栏 bottomNavHeight = ${bottomNavHeight}`);
      console.info(`导航条的高度 indicatorHeight = ${indicatorHeight}`);
    }).catch((err: BusinessError) => {
      console.error(`获取屏幕顶部状态栏、底部导航栏和导航条的高度 出现异常 Failed to obtain the window. Cause: ${JSON.stringify(err)}`);
    });
  }

  build() {
    Column({ space: 10 }) {
      Text(this.message)
        .id('TestGetWindowAvoidAreaHelloWorld')
        .fontSize(20)
        .fontWeight(FontWeight.Bold)
        .onClick(() => {
          this.getAvoidAreaHeight()
        })

    }
    .height('100%')
    .width('100%')
  }
}
相关推荐
程序猿追20 分钟前
HarmonyOS 7 WebSocket 实时消息,断开重连后怎样只显示一条回信
websocket·华为·harmonyos
OH_TPC3 小时前
@ohos/grpc 源码解读(三):连接池化与并发——三级锁、写工作线程与 JS 排干释放
harmonyos
时光慢煮3 小时前
鸿蒙 PC 上用 Electron 跨平台框架做窗口焦点:景深台 DepthDeck 的景深光学模型与 HAP 上屏实战
华为·electron·harmonyos
时光慢煮3 小时前
鸿蒙 PC 上用 Electron 跨平台框架俯瞰窗口:窗寰 Panoria 的三维投影、编队回放与 HAP 上屏实战
华为·electron·harmonyos
程序猿追5 小时前
用 HarmonyOS 7 AppStorage 处理交接台的试排与共享阶段
harmonyos
NJPJI_Yang6 小时前
鸿蒙操作系统使用 ArkTS 状态管理 V2 实现跨页面共享与持久化实战(本文已在AtomGit仓库开源)
华为·开源·harmonyos
渣哥搞AI7 小时前
在鸿蒙 PC 上用 Electron 写鸿蒙 PC 应用:星枢 AstroHub 的跨平台框架落地与 16 路能力调用拓扑
华为·harmonyos
向哆哆7 小时前
在鸿蒙PC上用ArkUI开发跨平台缓存可视化应用:CacheLoom实战
缓存·华为·harmonyos
向哆哆8 小时前
在鸿蒙PC上用Electron跨平台框架开发桌面应用:动量台 Momentum 的窗口动力学引擎与 HAP 交付实践
华为·electron·harmonyos