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%')
  }
}
相关推荐
平安的平安13 分钟前
【OpenHarmony】React Native鸿蒙实战:Camera 相机组件详解
数码相机·react native·harmonyos
松叶似针16 分钟前
Flutter三方库适配OpenHarmony【secure_application】— 调试与问题排查实战
flutter·harmonyos
阿林来了38 分钟前
Flutter三方库适配OpenHarmony【flutter_speech】— pubspec.yaml 多平台配置
flutter·harmonyos
ITUnicorn20 小时前
【HarmonyOS 6】进度组件实战:打造精美的数据可视化
华为·harmonyos·arkts·鸿蒙·harmonyos6
松叶似针1 天前
Flutter三方库适配OpenHarmony【secure_application】— 五平台隐私保护机制横向对比
flutter·harmonyos
平安的平安1 天前
【OpenHarmony】React Native鸿蒙实战:SegmentControl 分段控件详解
react native·react.js·harmonyos
平安的平安1 天前
【OpenHarmony】React Native鸿蒙实战:ProgressRing 环形进度详解
react native·react.js·harmonyos
平安的平安1 天前
【OpenHarmony】React Native鸿蒙实战:ProgressBar 进度条详解
react native·react.js·harmonyos
前端不太难1 天前
未来的鸿蒙 App,还需要“首页”吗?
华为·状态模式·harmonyos
平安的平安1 天前
【OpenHarmony】React Native鸿蒙实战:SearchBar 搜索栏详解
react native·react.js·harmonyos