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%')
  }
}
相关推荐
hqk19 分钟前
鸿蒙零基础语法入门:开启你的开发之旅
android·前端·harmonyos
HarmonyOS_SDK21 分钟前
手写套件五大能力,让创作办公高效流畅
harmonyos
爱笑的眼睛1122 分钟前
深入探索HarmonyOS ArkTS异步编程模式
华为·harmonyos
lqj_本人2 小时前
鸿蒙Qt音频实战:解决QMediaPlayer的高延迟与杂音问题
qt·音视频·harmonyos
流影ng3 小时前
【HarmonyOS】自定义节点能力
typescript·harmonyos
SuperHeroWu73 小时前
【HarmonyOS 6】为什么getContext 废弃,使用getHostContext说明
华为·harmonyos·context·上下文·getcontext·gethostcontext
lqj_本人4 小时前
鸿蒙Qt权限避坑:动态申请与Crash修复
qt·华为·harmonyos
在下历飞雨4 小时前
Kuikly基础之音频播放与资源管理:青蛙叫声实现
android·ios·harmonyos
不爱吃糖的程序媛7 小时前
彻底解决 Flutter 开发 HarmonyOS 应用:No Hmos SDK found 报错
flutter·华为·harmonyos
liuxf12347 小时前
fvm管理鸿蒙flutter
flutter·华为·harmonyos