有没有HarmonyOS鸿蒙Next办法判断系统底部是小白条还是三键导航?

有没有HarmonyOS鸿蒙Next办法判断系统底部是小白条还是三键导航? 这两个的避让高度不一样,想做个区分。

3 回复

判断逻辑

使用 window.AvoidAreaType 枚举类型判断导航模式:

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

// 监听规避区域变化

windowClass.on('avoidAreaChange', (data) => {

  if (data.type === window.AvoidAreaType.TYPE_NAVIGATION_INDICATOR) {

    // 小白条触发的规避区域

    const indicatorHeight = px2vp(data.area.bottomRect.height);

  } else if (data.type === window.AvoidAreaType.TYPE_SYSTEM) {

    // 三键导航触发的规避区域

    const systemNavHeight = px2vp(data.area.bottomRect.height);

  }

});

动态适配:初始化时获取当前值

const area = windowClass.getWindowAvoidArea(window, window.AvoidAreaType.TYPE_NAVIGATION_INDICATOR);

if (area.bottomRect.height > 0) {

  // 存在小白条导航

} else {

  // 回退检测传统三键导航

  const systemArea = windowClass.getWindowAvoidArea(window, window.AvoidAreaType.TYPE_SYSTEM);

}

小白条避让 32vp;三键导航避让 30vp 热区高度

代码示例

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

@Entry

@Component

struct Index {

  @State currentNavType: string = 'unknown';

  aboutToAppear() {

    const windowClass = window.getLastWindow(this.context);

    

    // 初始检测

    let indicatorArea = windowClass.getWindowAvoidArea(window, window.AvoidAreaType.TYPE_NAVIGATION_INDICATOR);

    if (indicatorArea.bottomRect.height > 0) {

      this.currentNavType = 'gesture';

    } else {

      const systemArea = windowClass.getWindowAvoidArea(window, window.AvoidAreaType.TYPE_SYSTEM);

      this.currentNavType = systemArea.bottomRect.height >= 30 ? '3-button' : 'unknown';

    }

    // 动态监听变化

    windowClass.on('avoidAreaChange', (data) => {

      if (data.type === window.AvoidAreaType.TYPE_NAVIGATION_INDICATOR) {

        this.currentNavType = 'gesture';

      } else if (data.type === window.AvoidAreaType.TYPE_SYSTEM) {

        this.currentNavType = '3-button';

      }

    });

  }

  build() {

    Column() {

      Text(`当前导航类型: ${this.currentNavType}`)

    }

  }

}

更多关于有没有HarmonyOS鸿蒙Next办法判断系统底部是小白条还是三键导航?的实战系列教程也可以访问 https://www.itying.com/category-93-b0.html


在HarmonyOS中,可通过getDisplayBottomSafeArea()方法获取底部安全区域高度,结合getNavigationBarHeight()判断导航模式。若安全区域高度大于0且导航栏高度为0,通常为手势导航(小白条);若导航栏高度大于0,则为三键导航。具体实现需调用WindowDisplay相关API进行检测。

在HarmonyOS Next中,可以通过WindowManagergetWindowInsets()方法获取系统导航栏类型。使用WindowInsets.getSystemGestureInsets()判断是否为手势导航(小白条),或通过WindowInsets.getSystemBars()检查传统三键导航的高度差异。具体实现可参考开发文档中UI适配部分。

回到顶部