鸿蒙设置沉浸式状态栏、全屏、获取导航栏高度

场景1.在EntryAbility.ts中设置

TypeScript 复制代码
  onWindowStageCreate(windowStage: window.WindowStage) {


    let windowClass: window.Window = null;
    windowStage.getMainWindow((err, data) => {
      // 1.获取应用主窗口
      windowClass = data;
      // 2.设置导航栏、状态栏不显示。
      windowClass.setWindowSystemBarEnable([], (err) => {

      });
      // 3.设置全屏
      windowClass.setWindowLayoutFullScreen(true).then(() => {

      })
      // 4.获取底部导航栏高度,此时的高度单位是px
      let navigationHeight = windowClass.getWindowAvoidArea(window.AvoidAreaType.TYPE_SYSTEM).bottomRect.height
      PersistentStorage.PersistProps([{
        key: "navigationHeight", defaultValue: navigationHeight
      }])
      windowStage.loadContent('pages/Index', (err, data) => {

      });
    })

  }

此时APP顶部状态栏和底部导航栏都将被隐藏,并且页面全面屏显示

场景2. 如果想要在其他页面继续显示状态栏和底部导航栏又该怎么做呢?

第一步:先获取context属性

导入依赖

TypeScript 复制代码
import common from '@ohos.app.ability.common'
import window from '@ohos.window'

声明context属性

TypeScript 复制代码
private context = getContext(this) as common.UIAbilityContext
复制代码
aboutToAppear()方法中
TypeScript 复制代码
aboutToAppear() {
  window.getLastWindow(this.context).then((windowClass) => {
    windowClass.setWindowSystemBarEnable(["status", "navigation"]).then(() => {
    })
  })
}

"status"和"navigation"就对象状态栏和导航栏。想显示哪个就写哪个

如果想获取导航栏的高度为vp,那么通过下面方法获取

TypeScript 复制代码
px2vp(this.navigationHeight)

注意:目前我只在页面中能获取到px2vp这个方法,在ability中我没有获取到。如果有更好的方法麻烦告诉我一下

相关推荐
爱吃大芒果2 天前
从零搭建完整 HarmonyOS 应用实战教程
华为·typescript·harmonyos
richard_yuu2 天前
鸿蒙首页实战开发|ArkTS 从零搭建治愈系首页、动态问候与功能模块
华为·harmonyos
音视频牛哥2 天前
SmartMediaKit 鸿蒙NEXT GB28181设备接入SDK
华为·harmonyos·鸿蒙gb28181·鸿蒙next gb28181·鸿蒙gb28181接入·鸿蒙接入gb28181平台·鸿蒙执法记录仪gb28181
key_3_feng2 天前
鸿蒙车规级MCU开发方案
单片机·华为·harmonyos
大雷神2 天前
HarmonyOS APP<<古今职鉴定>>开源教程第14篇:碰一碰分享:NFC 近场通信
华为·华为云·harmonyos
想你依然心痛2 天前
HarmonyOS 6(API 23)实战:基于悬浮导航、沉浸光感与HMAF的“智流工坊“——低代码可视化智能体编排平台
低代码·华为·harmonyos
richard_yuu2 天前
鸿蒙ArkUI组件化实战|公共组件封装、复用解耦与上架级UI规范落地
ui·华为·harmonyos
KKei16382 天前
Flutter for OpenHarmony 学习专注模式APP技术文章
学习·flutter·华为·harmonyos
想你依然心痛2 天前
HarmonyOS 6(API 23)实战:基于悬浮导航、沉浸光感与HMAF的“数字孪生工坊“——工业制造AI智能体协同平台
人工智能·制造·harmonyos
UnicornDev2 天前
【Flutter x HarmonyOS 6】挑战功能的业务逻辑实现
flutter·华为·harmonyos·鸿蒙·鸿蒙系统