HarmonyOS 鸿蒙Next中底部导航栏获取问题

HarmonyOS 鸿蒙Next中底部导航栏获取问题 在真机上获取到的底部导航栏高度为0,顶部状态栏的高度可以获取到

3 回复

在UIAbility里尝试是否可以获取

onWindowStageCreate(windowStage: window.Window): void {

  // Main window is created, set main page for this ability

  hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onWindowStageCreate');

  windowStage.loadContent('pages/Index',async(err) => {

  if (err.code) {

  hilog.error(0x0000, 'testTag', 'Failed to load the content. Cause: %{public}s', JSON.stringify(err) ?? '');

  return;

}
let windowClass: window.Window = windowStage.getMainWindowSync(); // 获取应用主窗口

await windowClass.setWindowSystemBarEnable(['status','navigation'])

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)

hilog.info(0x0000, 'testTag', 'Succeeded in loading the content.');

});

}

参考拓展安全区:

https://developer.huawei.com/consumer/cn/doc/harmonyos-guides-V5/arkts-develop-apply-immersive-effects-V5#section202081847174413

// xxx.ets

@Entry
@Component
struct Example {

  build() {

    Column () {

      Row () {

        Text('ROW2').fontSize(40)

      }.backgroundColor(Color.Orange).padding(20)

      Row () {

        Text('ROW3').fontSize(40)

      }.backgroundColor(Color.Orange).padding(20)

      Row () {

        Text('ROW4').fontSize(40)

      }.backgroundColor(Color.Orange).padding(20)

      Row () {

        Text('ROW5').fontSize(40)

      }.backgroundColor(Color.Orange).padding(20)

      Row () {

        Text('Bottom Row').fontSize(40).textAlign(TextAlign.Center).width('100%')

      }

      .backgroundColor(Color.Orange)

      // 设置底部绘制延伸到导航条

    }

    .width('100%').height('100%').alignItems(HorizontalAlign.Center)

    .backgroundColor('#008000')

    .justifyContent(FlexAlign.SpaceBetween)

    .expandSafeArea([SafeAreaType.SYSTEM], [SafeAreaEdge.TOP])

  }

}

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


在HarmonyOS(鸿蒙Next)中,底部导航栏的获取通常通过TabBar组件实现。TabBar是鸿蒙提供的一个标准组件,用于在应用底部显示导航栏。开发者可以通过TabBar的API来设置和获取导航栏的状态、内容等信息。

具体来说,可以通过TabBargetTabBarController方法获取导航栏的控制器,进而操作导航栏的各项属性。例如,可以使用TabBarControllergetSelectedIndex方法获取当前选中的导航项索引,或者使用setSelectedIndex方法设置选中的导航项。

此外,TabBar还支持自定义导航项的内容和样式,开发者可以通过TabBarItem组件来定义每个导航项的文字、图标等。通过TabBaraddTab方法可以动态添加新的导航项。

在鸿蒙Next中,底部导航栏的获取和操作主要通过TabBarTabBarController这两个核心类来完成,开发者可以参考鸿蒙的官方文档来详细了解这些API的使用方法和参数说明。

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

  1. 获取Window实例:通过getWindow()方法获取当前窗口的实例。
  2. 获取系统配置:调用getWindowSystemConfig()方法获取窗口的系统配置。
  3. 获取导航栏高度:通过getNavigationBarHeight()方法获取底部导航栏的高度。

示例代码如下:

Window window = getWindow();
WindowManager.LayoutParams layoutParams = window.getAttributes();
int navigationBarHeight = window.getWindowSystemConfig().getNavigationBarHeight();

此方法适用于需要动态调整布局以适配不同设备屏幕的场景。

回到顶部