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()
  }
相关推荐
Android技术栈3 小时前
鸿蒙数据防泄漏(DLP)【Data Loss Prevention Kit开发指导】
程序员·移动开发·数据安全·harmonyos·鸿蒙·openharmony·防泄漏
pixle03 小时前
HarmonyOS Next系列之Echarts图表组件(折线图、柱状图、饼图等)实现(八)
华为·echarts·harmonyos
爱桥代码的程序媛4 小时前
鸿蒙开发管理:【@ohos.account.distributedAccount (分布式帐号管理)】
程序员·移动开发·harmonyos·鸿蒙·鸿蒙系统·openharmony·鸿蒙开发
XuZhenhao060918 小时前
HarmonyOS - 通过.p7b文件获取fingerprint
华为·harmonyos
全栈探索者20 小时前
玩转HarmonyOS NEXT之配置文件篇
harmonyos
不知名靓仔1 天前
鸿蒙应用APP开发实战:详细操作指南
华为·harmonyos
碎像1 天前
我使用HarmonyOs Next开发了b站的首页
华为·harmonyos
Android技术栈2 天前
鸿蒙开发Ability Kit(程序访问控制):【安全控件概述】
程序员·移动开发·harmonyos·鸿蒙·openharmony·访问控制·安全控件
硅纪元2 天前
硅纪元视角 | 国内首款鸿蒙人形机器人“夸父”开启应用新篇章
华为·机器人·harmonyos
李洋-蛟龙腾飞公司2 天前
HarmonyOS Next 原生应用开发-从TS到ArkTS的适配规则(三)
harmonyos