当前位置: 首页 > news >正文

鸿蒙工具类---设置全屏效果(上下安全区背景可设置)

1,获得上下文对象(context)

在EntryAbility.ets文件中的onCreate(want: Want, launchParam: AbilityConstant.LaunchParam): void {}方法中,通过

AppStorage.setOrCreate('context', this.context)
将上下文对象存储起来 - this.context。

2,创建工具类,设置上下安全区(顶部状态栏,底部安全区域)

import { window } from '@kit.ArkUI'
import { logger } from './Logger' // 实时更新一下导入位置class FullScreen {// 开启全屏async enable () {// 1. 先获取上下文对象const context = AppStorage.get<Context>('context')if (context) {// 2. 获取应用窗口const win = await window.getLastWindow(context)// 3. 开启沉浸式模式/全屏效果await win.setWindowLayoutFullScreen(true)// 4. 获取顶部区域const topArea = win.getWindowAvoidArea(window.AvoidAreaType.TYPE_SYSTEM)// 5. 存储顶部状态栏的高度 默认获取的高度单位是px// px2vp() -> 将px单位的数值 -> vp单位 精准logger.info('顶部状态的高度', px2vp(topArea.topRect.height).toString())AppStorage.setOrCreate('topHeight', px2vp(topArea.topRect.height))// 6. 获取底部区域const bottomArea = win.getWindowAvoidArea(window.AvoidAreaType.TYPE_NAVIGATION_INDICATOR)// 7. 存储顶部状态栏的高度 默认获取的高度单位是px// px2vp() -> 将px单位的数值 -> vp单位 精准logger.info('底部安全区的高度', px2vp(bottomArea.bottomRect.height).toString())AppStorage.setOrCreate('bottomHeight', px2vp(bottomArea.bottomRect.height))}}// 关闭全屏async disable() {// 1. 先获取上下文对象const context = AppStorage.get<Context>('context')if (context) {// 2. 获取应用窗口const win = await window.getLastWindow(context)// 3. 关闭沉浸式模式/全屏效果await win.setWindowLayoutFullScreen(false)AppStorage.setOrCreate('topHeight', 0)AppStorage.setOrCreate('bottomHeight',0)}}
}
export const fullScreen = new FullScreen()

3,工具类使用

在EntryAbility.ets文件中的onWindowStageCreate(windowStage: window.WindowStage): void {}方法中进行设置,因为这个方法是页面展示之前会被调用的方法。

fullScreen.enable() //开启全屏
fullScreen.disable() //关闭全屏

4,获取安全区的大小

@StorageProp('topHeight')
topHeight: number = 0
@StorageProp('bottomHeight')
bottomHeight: number = 0

相关文章:

  • 用jsp做的二手交易网站搜索引擎的两个基本方法
  • 公司网站空间域名建设百度广告收费
  • 网站制作生成器百度搜索引擎排行榜
  • 做食品网站用什么颜色崇左seo
  • 武汉鑫灵锐网站建设关键词排名监控
  • 百度个人网站申请培训机构网站模板
  • 常用的串口通信工具MobaXterm、Bus Hound、SScom等
  • GNSS位移监测站的应用场景
  • 轻巧灵动,智启未来 ——Kinova Gen3 Lite 机器人轻松解锁各行业自动化新姿势
  • 云原生灰度方案对比:服务网格灰度(Istio ) 与 K8s Ingress 灰度(Nginx Ingress )
  • TCP/UDP协议深度解析(二):TCP连接管理全解,三次握手四次挥手的完整流程
  • SoC仿真环境中自定义printf函数的实现
  • 微算法科技融合Grover算法与统一哈希函数的混合经典-量子算法技术,可在多领域高效提升文本处理效率
  • nt!CcFlushCache函数分析之nt!CcFindBcb
  • 【自己动手写AI Agent】 对于企业构建AI应用的几点思考3
  • C++(智能指针)
  • Redis—持久化
  • 设计模式 | 抽象工厂模式
  • 鸿蒙分布式能力深度解析:构建跨设备无缝体验的技术基石
  • Fisco Bcos学习 - 控制台搭建和基本使用
  • NVads V710 v5 系列虚拟机已发布
  • 【iOS】iOS崩溃总结
  • 我手动从go官网下载了go1.16.15linux安装包,我该如何做,才能使得vscode仍能通过右下角来管理这个go版本
  • python学习笔记(深度学习)
  • 新能源汽车电池类型差异分析
  • 英飞凌高性能BMS解决方案助力汽车电动化