HarmonyOS项目开发经历——开启沉浸式模式

沉浸式模式

首先我们得先知道什么是沉浸式模式。

在手机系统中,由于有安全区域的限制,因此我们在开发应用时并不能全屏显示,例如下图,注意顶部状态栏:

开启沉浸式模式后,再注意顶部状态栏:

那么如何实现这种效果呢,我们就需要突破手机自带的顶部的安全区域,达到沉浸式全屏显示的效果。

一、直接在index主页开启沉浸式模式(一进应用页面就开启):

ts 复制代码
aboutToAppear() {
   this.enableFullScreen()
}
​
async enableFullScreen() {
    const win = await window.getLastWindow(getContext()) //使用window这个API的getLastWindow方法获取页面
    win.setWindowLayoutFullScreen(true) //使用setWindowLayoutFullScreen设置true开启沉浸式模式
    const area = await win.getWindowAvoidArea(window.AvoidAreaType.TYPE_SYSTEM) //使用getWindowAvoidArea方法获取到安全区域的高度
    let vpHeight = px2vp(area.topRect.height)
    AppStorage.setOrCreate('topHeight', vpHeight) //将高度转成vp,存到AppStorage中方便其它页面获取高度
  }

1、解释一下为什么要一进应用就开启? 因为在最新鸿蒙当中,使用API在开启沉浸式模式后会有小bug,就是一旦开启沉浸式模式,当我们切换到别的页面也会开启。所以我们统一在index主页开启,也就是一进入应用就开启。

2、为什么要获取安全区域高度? 因为我们在index主页中开启了沉浸式模式,我们所有页面都会进入沉浸式,在我们不需要全屏显示的页面就可以通过拿到这个高度来调整顶部高度,间接等于关闭了沉浸式模式。

二、设置顶部安全区域状态栏的字体颜色(看项目界面的需求,按UI设计稿来)

ts 复制代码
   async settingStatusBarLight() {
    const win = await window.getLastWindow(getContext())
    win.setWindowSystemBarProperties({ statusBarContentColor: '#FFFFFF' }) //设置安全区字体颜色为白色
  }
​
  async settingStatusBarDark() {
    const win = await window.getLastWindow(getContext())
    win.setWindowSystemBarProperties({ statusBarContentColor: '#000000' }) //设置安全区字体颜色为白色
  }

效果图(注意顶部系统状态栏的颜色由黑色变成了白色):

三、封装沉浸式模式的代码(便于复用)

完整代码

ts 复制代码
import { window } from '@kit.ArkUI'
​
export class windowManager {
  //开启沉浸式全屏模式
  static async enableFullScreen() {
    const win = await window.getLastWindow(getContext()) //使用window这个API的getLastWindow方法获取页面
    win.setWindowLayoutFullScreen(true) //使用setWindowLayoutFullScreen设置true开启沉浸式模式
    const area = await win.getWindowAvoidArea(window.AvoidAreaType.TYPE_SYSTEM) //使用getWindowAvoidArea方法获取到安全区域的高度
    let vpHeight = px2vp(area.topRect.height)
    AppStorage.setOrCreate('topHeight', vpHeight) //将高度转成vp,存到AppStorage中方便其它页面获取高度
  }
​
  //关闭沉浸式模式
  static async disableFullScreen() {
    const win = await window.getLastWindow(getContext()) //使用window这个API的getLastWindow方法获取页面
    win.setWindowLayoutFullScreen(false) //使用setWindowLayoutFullScreen设置false关闭沉浸式模式
    AppStorage.setOrCreate('topHeight', 0) //将高度重置为零
  }
​
  static async settingStatusBarLight() {
    const win = await window.getLastWindow(getContext())
    win.setWindowSystemBarProperties({ statusBarContentColor: '#FFFFFF' }) //设置安全区字体颜色为白色
  }
​
  static async settingStatusBarDark() {
    const win = await window.getLastWindow(getContext())
    win.setWindowSystemBarProperties({ statusBarContentColor: '#000000' }) //设置安全区字体颜色为白色
  }
}

四、页面中调用封装好的代码

1、其他的页面需要调整顶部距离(通过设置顶部padding关闭全屏显示)

ts 复制代码
  @StorageProp('topHeight') topHeight: number = 0 //获取安全区域高度,通过padding设置顶部距离
  
  @Entry
@Component
struct SettingPage {
  @StorageProp('topHeight') topHeight: number = 0
    
  build() {
    Navigation() {
​
    }.title('返回')
    .titleMode(NavigationTitleMode.Mini)
    .padding(this.topHeight)   // 通过调整顶部高度实现关闭沉浸式显示
​
  }
}

2、需要换状态栏字体颜色的页面

ts 复制代码
  aboutToAppear() {
    windowManager.settingStatusBarLight()
  }
​
  aboutToDisappear() {
    windowManager.settingStatusBarDark()
  }
相关推荐
●VON30 分钟前
双非大学生自学鸿蒙5.0零基础入门到项目实战 -ArkTs核心
华为·harmonyos·arkts·arkui
爱笑的眼睛1111 小时前
HarmonyOS Span文本片段富文本编辑深度解析
华为·harmonyos
爱笑的眼睛1111 小时前
HarmonyOS相机开发:深入解析预览与拍照参数配置
华为·harmonyos
爱笑的眼睛1115 小时前
深入理解ArkTS装饰器:提升HarmonyOS应用开发效率
华为·harmonyos
Damon小智19 小时前
HarmonyOS 5 开发实践:分布式任务调度与设备协同架构
分布式·架构·harmonyos
●VON1 天前
双非大学生自学鸿蒙5.0零基础入门到项目实战 -《基础篇》
android·华为·harmonyos·鸿蒙
Damon小智1 天前
鸿蒙分布式数据服务(DDS)原理与企业同步实战
分布式·华为·harmonyos
猫林老师2 天前
HarmonyOS自动化测试与持续集成实战指南
ci/cd·华为·harmonyos
寂然如故2 天前
拥抱未来:HarmonyOS NEXT 开发新范式深度解析
华为·harmonyos
国霄2 天前
(2)Kotlin/Js For Harmony——如何复用ViewModel
harmonyos