HarmonyOS鸿蒙Next中怎么获取顶部栏和导航栏高度

HarmonyOS鸿蒙Next中怎么获取顶部栏和导航栏高度 需要实现沉浸式效果,要内容在状态栏和导航栏背面,现在参考HmosWord效果实现了,但是存在一个问题,就是底部导航栏盖住了我的Tabs View,怎么获取底部导航栏高度?同时请问怎么获取状态栏高度,同样需要规避

4 回复

参考如下:

let windowClass: window.Window = windowStage.getMainWindowSync(); // 获取应用主窗口
let type = window.AvoidAreaType.TYPE_NAVIGATION_INDICATOR; // 以导航条避让为例
let avoidArea = windowClass.getWindowAvoidArea(type);
let bottomRectHeight = avoidArea.bottomRect.height; // 获取到导航条区域的高度
console.log("bottomRectHeight: " + bottomRectHeight)
let area = windowClass.getWindowAvoidArea(window.AvoidAreaType.TYPE_SYSTEM);
let statusBarHeight = px2vp(area.topRect.height) //状态栏高度
console.log("statusBarHeight: " + statusBarHeight)

更多关于HarmonyOS鸿蒙Next中怎么获取顶部栏和导航栏高度的实战系列教程也可以访问 https://www.itying.com/category-93-b0.html


aboutToAppear(): void {
  let w = window.findWindow(this.getUIContext().getWindowName())
  w.getWindowAvoidArea(window.AvoidAreaType.TYPE_SYSTEM)
}

还有个expandSafeArea可以试试,适合静态内容的沉浸式状态栏

在HarmonyOS(鸿蒙Next)中,获取顶部栏和导航栏高度可以通过WindowWindowManager相关API实现。具体步骤如下:

  1. 获取Window对象:通过WindowManager获取当前窗口的Window对象。
  2. 获取WindowInsets:通过Window对象的getDecorView()方法获取根视图,然后调用getRootWindowInsets()方法获取WindowInsets对象。
  3. 获取顶部栏高度:通过WindowInsets对象的getSystemWindowInsetTop()方法获取顶部栏高度。
  4. 获取导航栏高度:通过WindowInsets对象的getSystemWindowInsetBottom()方法获取导航栏高度。

示例代码如下:

Window window = getWindow();
View decorView = window.getDecorView();
WindowInsets windowInsets = decorView.getRootWindowInsets();
int statusBarHeight = windowInsets.getSystemWindowInsetTop();
int navigationBarHeight = windowInsets.getSystemWindowInsetBottom();

statusBarHeight即为顶部栏高度,navigationBarHeight即为导航栏高度。

在HarmonyOS鸿蒙Next中,可以通过Window对象获取顶部栏和导航栏的高度。使用WindowgetWindowSystemConfig()方法获取系统的窗口配置,然后通过getStatusBarHeight()getNavigationBarHeight()方法分别获取顶部栏和导航栏的高度。示例代码如下:

Window window = getWindow();
WindowSystemConfig config = window.getWindowSystemConfig();
int statusBarHeight = config.getStatusBarHeight();
int navigationBarHeight = config.getNavigationBarHeight();

这样即可获取到顶部栏和导航栏的高度。

回到顶部