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%')
  }
}
相关推荐
●VON4 小时前
双非大学生自学鸿蒙5.0零基础入门到项目实战 - 歌曲列表
android·华为·harmonyos
HarmonyOS_SDK6 小时前
【FAQ】HarmonyOS SDK 闭源开放能力 — Background Tasks Kit
harmonyos
小白学鸿蒙6 小时前
新手记录使用uniapp-x开发鸿蒙应用
华为·uni-app·harmonyos
●VON8 小时前
双非大学生自学鸿蒙5.0零基础入门到项目实战 -ArkTs核心
华为·harmonyos·arkts·arkui
爱笑的眼睛1118 小时前
HarmonyOS Span文本片段富文本编辑深度解析
华为·harmonyos
爱笑的眼睛1118 小时前
HarmonyOS相机开发:深入解析预览与拍照参数配置
华为·harmonyos
爱笑的眼睛111 天前
深入理解ArkTS装饰器:提升HarmonyOS应用开发效率
华为·harmonyos
Damon小智1 天前
HarmonyOS 5 开发实践:分布式任务调度与设备协同架构
分布式·架构·harmonyos
●VON1 天前
双非大学生自学鸿蒙5.0零基础入门到项目实战 -《基础篇》
android·华为·harmonyos·鸿蒙
Damon小智2 天前
鸿蒙分布式数据服务(DDS)原理与企业同步实战
分布式·华为·harmonyos