可以通过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%') } }
可以通过window窗口接口的getWindowAvoidArea获取系统规避区域,返回值中的topRect.height即为系统状态栏高度,注意接口返回值单位为px,参考链接:
https://developer.huawei.com/consumer/cn/doc/harmonyos-references-V5/js-apis-window-V5
请参考demo测试一下: