HarmonyOS 状态栏高度无法获取?

目前无法通过接口获取屏幕上面和下面的状态栏高度,导致无法计算屏幕可用区域。

阅读 506
1 个回答

可以通过window窗口接口的getWindowAvoidArea获取系统规避区域,返回值中的topRect.height即为系统状态栏高度,注意接口返回值单位为px,参考链接:

https://developer.huawei.com/consumer/cn/doc/harmonyos-references-V5/js-apis-window-V5

请参考demo测试一下:

import { emitter } from '@kit.BasicServicesKit';
import { window } from '@kit.ArkUI';
import { hilog } from '@kit.PerformanceAnalysisKit';

@Entry
@Component
struct Index {
  @State message: string = 'Hello World'
  @State isLandscape: boolean = false
  @State notchMargin: number = 0
  context = getContext()

  build() {
    RelativeContainer() {
      Text(this.message)
      Button()
        .onClick(async () => {

          let type = window.AvoidAreaType.TYPE_SYSTEM;
          try {
            let windowClass: window.Window = await window.getLastWindow(this.context)
            let avoidArea = windowClass.getWindowAvoidArea(type);
            console.log('tag: avoidArea = ' + JSON.stringify(avoidArea))
          } catch (exception) {
            console.error('Failed to obtain the area. Cause:' + JSON.stringify(exception));
          }
        })
        .id('HelloWorld')
        .fontSize(50)
        .fontWeight(FontWeight.Bold)
        .alignRules({
          center: { anchor: '__container__', align: VerticalAlign.Center },
          middle: { anchor: '__container__', align: HorizontalAlign.Center }
        })
    }
    .height('100%')
    .width('100%')
  }
}
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进