HarmonyOS鸿蒙Next中已设置应用窗口全屏的前提下,怎么获取状态栏高度

HarmonyOS鸿蒙Next中已设置应用窗口全屏的前提下,怎么获取状态栏高度 已设置窗口全屏;
怎么获取状态栏高度?
获取状态栏高度的目的:自定义导航栏,需要加上状态栏的高度。

5 回复

试下这个

let avoidArea = windowClass.getWindowAvoidArea(window.AvoidAreaType.TYPE_SYSTEM);

AvoidAreaType需要设置为TYPE_SYSTEM,表示系统默认区域。一般包括状态栏、导航栏,各设备系统定义可能不同。TYPE_NAVIGATION_INDICATOR只包含导航栏,

参考文档:[@ohos.window (窗口)-图形图像-ArkTS API-ArkUI(方舟UI框架)-应用框架 - 华为HarmonyOS开发者](https://developer.huawei.com/consumer/cn/doc/harmonyos-references-V5/js-apis-window-V5#getwindowavoidarea9)

更多关于HarmonyOS鸿蒙Next中已设置应用窗口全屏的前提下,怎么获取状态栏高度的实战系列教程也可以访问 https://www.itying.com/category-93-b0.html


之前写组件库的时候用到了,做了一个安全区,具体可以看官网文档

import { window } from '@kit.ArkUI';

export const getWindowStage:
() => Promise<window.Window> = () => {
  return window.getLastWindow(getContext())
}
export const getWindowAvoidArea:
(type?: window.AvoidAreaType) => Promise<window.AvoidArea> =
  (type = window.AvoidAreaType.TYPE_SYSTEM) => {
    return new Promise((resolve, reject) => {
      getWindowStage().then(res => {
        resolve(res.getWindowAvoidArea(type))
      }).catch(_ => reject(_))
    })
  }

getWindowAvoidArea().then((res: window.AvoidArea) => { this.navHeight = this.space?.top ?? px2vp(res.topRect.height); this.botHeight = this.space?.bottom ?? px2vp(res.bottomRect.height); this.leftWidth = this.space?.left ?? px2vp(res.leftRect.width); this.rightWidth = this.space?.right ?? px2vp(res.rightRect.width); })

在HarmonyOS鸿蒙Next中,如果应用窗口已设置为全屏,可以通过Window类的getWindowInsetsController方法获取窗口的WindowInsets对象,再通过WindowInsets对象的getSystemWindowInsets方法获取状态栏高度。具体代码如下:

import window from '@ohos.window';

let windowClass = window.getTopWindow();
let windowInsetsController = windowClass.getWindowInsetsController();
let windowInsets = windowInsetsController.getSystemWindowInsets();
let statusBarHeight = windowInsets.top;

statusBarHeight即为状态栏的高度。

在HarmonyOS鸿蒙Next中,即使应用已设置为全屏,仍可以通过Window类获取状态栏高度。使用WindowInsetsgetSystemWindowInsets方法,具体如下:

WindowInsets windowInsets = getWindow().getDecorView().getRootWindowInsets();
int statusBarHeight = windowInsets.getSystemWindowInsets().top;

该方法返回状态栏的高度,单位为像素。即使在全屏模式下,系统仍会提供状态栏的高度信息。

回到顶部