鸿蒙OS ArkUI 沉浸式导航栏动态获取高度 -- 小白篇

本文详细介绍了如何在鸿蒙OS中使用ArkUI实现沉浸式导航栏的动态高度获取和样式设置,包括使用ImmersiveUtils工具类和PersistentStorage进行存储管理。开发者可以在EntryAbility的`onWindowStageCreate`和`.etsUI`页面中应用这些技术。
摘要由CSDN通过智能技术生成

鸿蒙OS ArkUI 沉浸式导航栏动态获取高度 -- 小白篇

下面展示一些 内联代码片
这是一个 沉浸式的样式工具类,这个没什么可说的,copy过去用就可以了

import window from '@ohos.window';

export class ImmersiveUtils {
  public static immersive(windowStage: window.WindowStage, config: {
    barColor: string,
    contentColor: string,
    navigationColor: string,
    navigatorColor: string
  }) {
    windowStage.getMainWindow()
      .then(win => {
        win.setWindowSystemBarProperties({
          statusBarColor: config.barColor,
          statusBarContentColor: config.contentColor,
          navigationBarColor: config.navigationColor,
          navigationBarContentColor: config.navigatorColor
        });
      })
  }
}

下面展示一些 主体代码
首先在你的EntryAbility或者是你的主入口UIAbility都可以
在onWindowStageCreate里去实现你的沉浸式导航高度获取和存储

  onWindowStageCreate(windowStage: window.WindowStage) {
    // 设置沉浸式代码
    globalThis.windowStage = windowStage;
    // 获取主窗口实例 参考API 9
    windowStage.getMainWindow()
      .then(win => {
        // 设置窗口的布局是否为全屏显示状态
        win.setWindowLayoutFullScreen(true);
        // 获取导航条高度 参考API 9
        // 这个方法为返回的是PX像素,需要使用px2vp方法转换一下 参考API 9
        let navHeight:number = win.getWindowAvoidArea(window.AvoidAreaType.TYPE_CUTOUT).topRect.height
        // 存储高度信息 参考API 9
        PersistentStorage.PersistProp('systemBarHeight', navHeight)
      })
  }

这里是在你的.ets UI页面里去实现的

// 引入工具类方法
import {ImmersiveUtils} from '../Utils/ImmersiveUtils'
@Entry
@Component
struct Login {
// 获取存储的数据信息
// px2vp 这个方法是.ets后缀文件才有的方法 .ts后缀是没有这个方法的
// 为什么使用这个方法上边的代码块有说明,感兴趣的也可以去查查官方的API文档
  @StorageLink('systemBarHeight') systemBarHeight: number = 0;
  build() {
    Column(){
      Text('顶部title')
        .fontSize(18)
        .fontWeight(FontWeight.Bold)
    }.padding({top:px2vp(this.systemBarHeight)}).height('100%').width('100%')
  }
  //设置你的顶部导航栏样式
  onPageShow(){
    ImmersiveUtils.immersive(globalThis.windowStage, {
      barColor: "#00286098",
      contentColor: "#ffffff",
      navigationColor: "#005d99d6",
      navigatorColor: "#ffffff"
    });
  }
}
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值