HarmonyOS 鸿蒙Next中如何在某页面中获取导航栏高度

HarmonyOS 鸿蒙Next中如何在某页面中获取导航栏高度 如何在某页面中获取导航栏高度。 除去在EntryAbility中使用下面这种方式

AppStorage.setOrCreate("tabBarHeight",
px2vp(windowStage.getMainWindowSync().getWindowAvoidArea(window.AvoidAreaType.TYPE_NAVIGATION_INDICATOR)
.bottomRect.height))

项目中不推崇使用AppStorage方式。 是否还有其他的实现方式?

3 回复
您参考一下如下demo,看能否满足您的需求

```javascript
[@Entry](/user/Entry)
[@Component](/user/Component)
struct Index{
  private bottomRectHeight:number = 0
  private statusBarHeight:number = 0
  aboutToAppear(): void {
    window.getLastWindow(getContext(this), (error, topWindow) => {
      if (topWindow) {
        let area = topWindow.getWindowAvoidArea(window.AvoidAreaType.TYPE_SYSTEM);
        let areaBottom = topWindow.getWindowAvoidArea(window.AvoidAreaType.TYPE_NAVIGATION_INDICATOR);
        //注意:返回的是px值,如果要用vp值需要转换
        this.bottomRectHeight = px2vp(areaBottom.bottomRect.height);
        this.statusBarHeight = px2vp(area.topRect.height);
        console.log('this.statusBarHeight' + this.statusBarHeight)
        console.log('this.bottomRectHeight' + this.bottomRectHeight)
      }
    });
  }
  build() {
    Column(){
      Row(){
        Text('测试页面')
      }
    }
  }
}

更多关于HarmonyOS 鸿蒙Next中如何在某页面中获取导航栏高度的实战系列教程也可以访问 https://www.itying.com/category-93-b0.html


在HarmonyOS(鸿蒙Next)中,获取导航栏高度可以通过Window类提供的接口来实现。具体步骤如下:

  1. 使用Window类的getWindowManager方法获取WindowManager实例。
  2. 通过WindowManager实例调用getDefaultDisplay方法获取Display对象。
  3. 使用Display对象的getCutout方法获取DisplayCutout对象。
  4. 通过DisplayCutout对象的getSafeInsetTop方法获取导航栏的高度。

示例代码如下:

import window from '@ohos.window';

let windowManager = window.getWindowManager();
let display = windowManager.getDefaultDisplay();
let cutout = display.getCutout();
let navigationBarHeight = cutout.getSafeInsetTop();

这段代码会返回导航栏的高度,单位为像素。

在HarmonyOS(鸿蒙Next)中,可以通过Window类的getWindowSystemConfig方法获取导航栏高度。具体步骤如下:

  1. 使用Window类的getInstance方法获取当前窗口实例。
  2. 调用getWindowSystemConfig方法获取窗口系统配置。
  3. 从配置中获取navigationBarHeight属性,即为导航栏高度。

示例代码如下:

Window window = Window.getInstance();
WindowSystemConfig config = window.getWindowSystemConfig();
int navigationBarHeight = config.getNavigationBarHeight();
回到顶部