鸿蒙沉浸式显示模式总结

在鸿蒙(HarmonyOS)中,沉浸式模式通常指的是让应用的界面更加专注于内容,隐藏系统状态栏和导航栏,以提供更加沉浸的用户体验。这种模式特别适用于游戏、视频播放、阅读等场景,其中用户可能不希望被界面元素干扰。

没有沉浸式模式的效果:

以下是如何在鸿蒙应用中实现沉浸式模式的一些基本步骤:

  • 某个页面要突破手机安全区域来全屏显示(沉浸式模式显示),其他页面不需要
  • 在组件的aboutToAppear中使用window模块的 getLastWindowsetWindowLayoutFullScreen两个方法来处理全屏显示

比如在"我的"页面:

ts 复制代码
import { window } from '@kit.ArkUI'

@Component
export struct Mine {

  aboutToAppear(): void {
    window.getLastWindow(getContext()).then(win=>{
      win.setWindowLayoutFullScreen(true)  // 开启了当前页面的沉浸式模式(开启全屏模式)
    })
  }

  build() {
    Column(){
      Text('我的' + Math.random().toFixed(2))
    }
    .height('100%')
    .width('100%')
    .backgroundColor(Color.Pink)
  }
}

设置沉浸式模式的特点:

在任何一个页面中设置过一次之后,其他页面也会跟着全屏显示。

以上这么处理会出现问题: 从其他tab标签进入我的tab标签时,会出现底部tab栏闪动。

解决这种问题: 在进入应用时(index.tes页面)就开启所有页面的全屏模式。

但是又会带来新的问题:不需要全屏显示的其他页面的内容会突破安全区域显示,导致内容显示不正常

比如:

"首页"页面出现问题:内容突破了整个安全区域,靠手机顶部显示了内容

解决办法:

需要通过获取手机的安全高度来结合 padding来适配顶部内容的正常显示

步骤:

  • 通过window模块的getLastWindowgetWindowAvoidArea 来获取安全高度
  • 安全高度获取到的是px单位,需要使用px2vp函数转换成vp单位
  • 将安全区域高度保存到AppStorage中共享给其他页面使用

以下是Index页面的ArkTS代码

ts 复制代码
import { Home } from '../views/home/home'
import { Interview } from '../views/interview/interview'
import { Mine } from '../views/mine/mine'
import { Project } from '../views/project/project'
import { window } from '@kit.ArkUI'
import { Logger } from '../common/utils/Logger'

@Entry
@Component
struct Index {
  @State
  currentIndex: number = 0

  aboutToAppear(): void {
    window.getLastWindow(getContext()).then(win => {
      win.setWindowLayoutFullScreen(true)
      let area = win.getWindowAvoidArea(window.AvoidAreaType.TYPE_SYSTEM)
      let height = area.topRect.height
      let vpheight = px2vp(height)
      AppStorage.setOrCreate('topHeight', vpheight)
      Logger.info(height.toString(), vpheight.toString())
    })
  }

  @Builder
  tabBuilder(text: string, icon: ResourceStr, selectedIcon: ResourceStr, index: number) {
    Column() {
      Image(this.currentIndex === index ? selectedIcon : icon)
        .height(28)
        .aspectRatio(1)
      Text(text)
    }
  }

  build() {
    Tabs({ index: $$this.currentIndex }) {
      TabContent() {
        if (this.currentIndex === 0) {
          Home()
        }

      }
      .tabBar(this.tabBuilder('首页', $r('app.media.home'), $r('app.media.home_select'), 0))

      TabContent() {
        if (this.currentIndex === 1) {
          Project()
        }

      }
      .tabBar(this.tabBuilder('项目', $r('app.media.tabbar_project'), $r('app.media.project_select'), 1))

      TabContent() {
        if (this.currentIndex === 2) {
          Interview()
        }
      }
      .tabBar(this.tabBuilder('面经', $r('app.media.tabbar_interview'), $r('app.media.interview_select'), 2))

      TabContent() {
        if (this.currentIndex === 3) {
          Mine()
        }
      }
      .tabBar(this.tabBuilder('我的', $r('app.media.tabbar_mine'), $r('app.media.mine_select'), 3))
    }
    .padding({ bottom: 15 })
    .barPosition(BarPosition.End)
    .animationDuration(0)
  }
}

其他页面使用:

比如首页

ts 复制代码
@Component
export struct Home {
  @StorageProp("topHeight") topHeight: number = 0

  build() {
    Column() {
      Text('首页' + Math.random().toFixed(2))
    }
    .padding({ top: this.topHeight })
    .width("100%")
    .height("100%")
    .backgroundColor(Color.Gray)
  }
}

实现效果:

可以看见,首页的内容区域是没有显示在安全区域了

设置安全区域文字颜色

  • 通过window模块的getLastWindowsetWindowSystemBarProperties({statusBarContentColor:颜色}) 来设置安全区域文字颜色
ts 复制代码
import { window } from '@kit.ArkUI'

@Component
export struct Mine {
  @StorageProp("topHeight") topHeight: number = 0

  aboutToAppear(): void {
    window.getLastWindow(getContext()).then((win) => {
      win.setWindowSystemBarProperties({ statusBarContentColor: '#FFFFFF' })
    })
  }

  aboutToDisappear(): void {
    window.getLastWindow(getContext()).then((win) => {
      win.setWindowSystemBarProperties({ statusBarContentColor: '#000000' })
    })
  }

  build() {
    Column() {
      Text('我的' + Math.random().toFixed(2))
    }
    .padding({ top: this.topHeight })
    .width("100%")
    .height("100%")
    .backgroundColor(Color.Pink)
  }
}
相关推荐
子兮曰1 天前
jev-ultrafast 深度解析:7 秒订机票的浏览器 Agent 是如何炼成的
前端·后端·agent
子兮曰1 天前
Jev 爆发一周:7 秒 Agent 背后的 System One 生态与三场争议
前端·后端·ai编程
前端小万1 天前
写公众号赚了 3000 块后,我做了一款叫 "一键成稿" 的软件
前端·微信小程序
爱勇宝1 天前
ZCode 开源 24 小时:一份没有历史的账本,回答不了"有没有偷代码"
前端·后端·chatglm (智谱)
三十而立洋1 天前
Cookie 详解:从产生到安全,一次讲透
前端·javascript
晨米酱1 天前
AGENTS.md:Agent 的上下文策略层
面试·架构·agent
卡布鲁1 天前
把一个 Vite + Vue3 应用塞进 qiankun (React + Umi3) 主站:十个坑的复盘
前端·javascript·react.js
彧azz1 天前
Linux 环境下 Redis 学习总结:数据类型、持久化、锁、事务、主从与缓存问题
linux·redis·笔记·学习·面试
汉堡大王95271 天前
Jev:不是聊天机器人, 而是一个智能 if 语句
前端·人工智能·后端
梦想很大很大1 天前
从运行事实到回归证据:Workrun 的 Telemetry 与 Evaluation 实践
前端·人工智能·后端