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%')
  }
}
相关推荐
爱笑的眼睛112 小时前
HarmonyOS 应用开发深度解析:ArkTS 声明式 UI 与状态管理最佳实践
华为·harmonyos
安卓开发者2 小时前
鸿蒙Next ArkWeb进程解析:多进程架构如何提升Web体验
前端·架构·harmonyos
damo王3 小时前
鸿蒙(HarmonyOS) 历史
华为·harmonyos
爱笑的眼睛114 小时前
HarmonyOS声明式UI开发:深入ArkUI与状态管理实践
华为·harmonyos
爱笑的眼睛114 小时前
HarmonyOS 应用开发进阶:深入 Stage 模型与 ArkUI 声明式开发实践
华为·harmonyos
2501_919749034 小时前
鸿蒙:更改状态栏、导航栏颜色
华为·harmonyos
2501_919749035 小时前
鸿蒙:@Builder 和 @BuilderParam正确使用方法
华为·harmonyos
爱笑的眼睛115 小时前
HarmonyOS应用开发:深入解析Stage模型与UIAbility
华为·harmonyos
HMSCore8 小时前
Cloud Foundation Kit启动预加载,赋能喜马拉雅秒启秒开流畅体验
harmonyos
我在看你呵呵笑21 小时前
GD32VW553-IOT开发板移植适配openharmony
物联网·华为·harmonyos