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

场景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中我没有获取到。如果有更好的方法麻烦告诉我一下

相关推荐
w1395485642239 分钟前
鸿蒙实战:PhoneLoginPage 6位验证码 Cell 设计与 60 秒倒计时
华为·harmonyos·鸿蒙系统
xd1855785551 小时前
敲门砖工坊-求职信定制的HarmonyOS开发实践
人工智能·华为·harmonyos·鸿蒙
心中有国也有家1 小时前
AtomGit Flutter 鸿蒙客户端:ModalBottomSheet 实战
android·javascript·学习·flutter·华为·harmonyos
tyqtyq221 小时前
求职信生成:AI 智能求职信撰写系统的鸿蒙实现
人工智能·学习·华为·生活·harmonyos
绝世番茄2 小时前
鸿蒙原生ArkTS布局方式之Brightness亮度调节布局详解
华为·harmonyos·鸿蒙
l134062082352 小时前
鸿蒙实战:RDB 数据库设计与 DatabaseService
数据库·华为·harmonyos·鸿蒙系统
ZZZMMM.zip3 小时前
数据侦探社-数据趋势分析的HarmonyOS开发实践
人工智能·华为·harmonyos·鸿蒙·鸿蒙系统
星释4 小时前
鸿蒙智能体开发实战:34.鸿蒙壁纸大师 - 提示词工程与优化
android·华为·harmonyos·鸿蒙
千逐684 小时前
React Native 性能优化深度解析:从原理到实战
flutter·华为·鸿蒙
ujainu小5 小时前
并发性能测试:心跳监测与主线程阻塞分析
华为·性能优化·harmonyos