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

HarmonyOS 开发应用沉浸式效果

开发应用沉浸式效果

概述

典型应用全屏窗口UI元素包括状态栏、应用界面和底部导航条,其中状态栏和导航条,通常在沉浸式布局下称为避让区;避让区之外的区域称为安全区。开发应用沉浸式效果主要指通过调整状态栏、应用界面和导航条的显示效果来减少状态栏导航条等系统界面的突兀感,从而使用户获得最佳的UI体验。

图1 界面元素示意图

沉浸式页面开发通常分为以下两步:

  1. 实现沉浸式效果。

    • 方案一:使用Window.setWindowLayoutFullScreen()方法设置窗口为全屏模式。

    隐藏状态栏

    避让状态栏

    • 方案二:设置组件的expandSafeArea属性,扩展组件的安全区域到状态栏和导航栏,从而实现沉浸式。
  2. 处理避让区域和页面内容的适配问题。

    由于避让区本身是有内容展示,如状态栏中的电量、时间等系统信息,或是手势交互,如导航条点击或上滑,在实现应用页面沉浸式效果后,往往会和避让区域产生UI元素的遮挡、视觉上的违和或交互上的冲突等问题,开发者可以针对不同场景选择以下方式对避让区和应用页面进行适配。

    • 使用Window.setWindowSystemBarEnable()方法或Window.setSpecificSystemBarEnabled()方法设置状态栏和导航栏的显隐。
    • 使用Window.setWindowSystemBarProperties()方法设置状态栏和导航栏的样式。
    • 使用Window.getWindowAvoidArea()方法获取避让区域的高度,据此设置应用页面内容的上下padding实现避让状态栏和导航栏。
    • 使用Display.getCutoutInfo()方法获取挖孔区域宽高和位置信息,设置对应避让元素的margin实现挖孔区避让。
@Entry
@Component
struct Index {build() {Column() {Row() {Text('ROW1').fontSize(40)}.backgroundColor(Color.Orange).padding(20)Row() {Text('ROW2').fontSize(40)}.backgroundColor(Color.Orange).padding(20)Row() {Text('ROW3').fontSize(40)}.backgroundColor(Color.Orange).padding(20)Row() {Text('ROW4').fontSize(40)}.backgroundColor(Color.Orange).padding(20)Row() {Text('ROW5').fontSize(40)}.backgroundColor(Color.Orange).padding(20)Row() {Text('ROW6').fontSize(40)}.backgroundColor(Color.Orange).padding(20)}.width('100%').height('100%').alignItems(HorizontalAlign.Center).justifyContent(FlexAlign.SpaceBetween).backgroundColor('#008000')}
}

窗口全屏布局方案

1.调用setWindowLayoutFullScreen()接口设置窗口全屏

2.使用getWindowAvoidArea()接口获取当前布局遮挡区域(例如状态栏、导航条)。

3.注册监听函数,动态获取避让区域的实时数据。常见的触发避让区回调的场景如下:应用窗口在全屏模式、悬浮模式、分屏模式之间的切换;应用窗口旋转;多折叠设备在屏幕折叠态和展开态之间的切换;应用窗口在多设备之间的流转。

export default class EntryAbility extends UIAbility {onCreate(want: Want, launchParam: AbilityConstant.LaunchParam): void {this.context.getApplicationContext().setColorMode(ConfigurationConstant.ColorMode.COLOR_MODE_NOT_SET);hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onCreate');}onDestroy(): void {hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onDestroy');}onWindowStageCreate(windowStage: window.WindowStage): void {// Main window is created, set main page for this abilityhilog.info(0x0000, 'testTag', '%{public}s', 'Ability onWindowStageCreate');// ....// ....// ....// ✅✅✅✅✅// 获取应用主窗口const windowClass: window.Window = windowStage.getMainWindowSync();// 窗口全屏布局方案windowClass.setWindowLayoutFullScreen(true)// 方案1:// windowClass.setSpecificSystemBarEnabled('status', false)// windowClass.setSpecificSystemBarEnabled('navigationIndicator', false)// 方案2// 获取布局避让遮挡的区域const topRectHeight = windowClass.getWindowAvoidArea(window.AvoidAreaType.TYPE_SYSTEM).topRect.height;  // 状态栏区域高度AppStorage.setOrCreate('topRectHeight', topRectHeight);const bottomRectHeight = windowClass.getWindowAvoidArea(window.AvoidAreaType.TYPE_NAVIGATION_INDICATOR).bottomRect.height;  // 导航条区域的高度AppStorage.setOrCreate('bottomRectHeight', bottomRectHeight);// 注册监听函数,动态获取避让区域数据windowClass.on('avoidAreaChange', (data) => {if (data.type === window.AvoidAreaType.TYPE_SYSTEM) {AppStorage.setOrCreate('topRectHeight', data.area.topRect.height);} else if (data.type == window.AvoidAreaType.TYPE_NAVIGATION_INDICATOR) {AppStorage.setOrCreate('bottomRectHeight', data.area.bottomRect.height);}});// ....// ....

4.布局中的UI元素需要避让状态栏和导航条,对控件顶部设置padding(具体数值与状态栏高度一致),实现对状态栏的避让;对底部设置padding(具体数值与底部导航条区域高度一致),实现对底部导航条的避让。

@Entry
@Component
struct Test2 {@StorageProp('bottomRectHeight')  bottomRectHeight: number = 0;@StorageProp('topRectHeight')  topRectHeight: number = 0;build() {Column() {Row() {Text('ROW1').fontSize(40)}.backgroundColor(Color.Orange).padding(20)Row() {Text('ROW2').fontSize(40)}.backgroundColor(Color.Orange).padding(20)Row() {Text('ROW3').fontSize(40)}.backgroundColor(Color.Orange).padding(20)Row() {Text('ROW4').fontSize(40)}.backgroundColor(Color.Orange).padding(20)Row() {Text('ROW5').fontSize(40)}.backgroundColor(Color.Orange).padding(20)Row() {Text('ROW6').fontSize(40)}.backgroundColor(Color.Orange).padding(20)}.width('100%').height('100%').alignItems(HorizontalAlign.Center).justifyContent(FlexAlign.SpaceBetween).backgroundColor('#008000')// top数值与状态栏区域高度保持一致;bottom数值与导航条区域高度保持一致.padding({ top: px2vp(this.topRectHeight), bottom: px2vp(this.bottomRectHeight) })}
}
  • 也可以单独组建设置全屏
import { window } from '@kit.ArkUI'@Entry
@Component
struct Test2 {async aboutToAppear() {const win = await window.getLastWindow(getContext())win.setWindowLayoutFullScreen(true)}build() {Column() {Row() {Text('ROW1').fontSize(40)}.backgroundColor(Color.Orange).padding(20)Row() {Text('ROW2').fontSize(40)}.backgroundColor(Color.Orange).padding(20)Row() {Text('ROW3').fontSize(40)}.backgroundColor(Color.Orange).padding(20)Row() {Text('ROW4').fontSize(40)}.backgroundColor(Color.Orange).padding(20)Row() {Text('ROW5').fontSize(40)}.backgroundColor(Color.Orange).padding(20)Row() {Text('ROW6').fontSize(40)}.backgroundColor(Color.Orange).padding(20)}.width('100%').height('100%').alignItems(HorizontalAlign.Center).justifyContent(FlexAlign.SpaceBetween).backgroundColor('#008000')}
}

组件安全区方案

@Entry
@Component
struct Test2 {build() {Column() {Row() {Text('ROW1').fontSize(40)}.backgroundColor(Color.Orange).padding(20)Row() {Text('ROW2').fontSize(40)}.backgroundColor(Color.Orange).padding(20)Row() {Text('ROW3').fontSize(40)}.backgroundColor(Color.Orange).padding(20)Row() {Text('ROW4').fontSize(40)}.backgroundColor(Color.Orange).padding(20)Row() {Text('ROW5').fontSize(40)}.backgroundColor(Color.Orange).padding(20)Row() {Text('ROW6').fontSize(40)}.backgroundColor(Color.Orange).padding(20)}.width('100%').height('100%').alignItems(HorizontalAlign.Center).justifyContent(FlexAlign.SpaceBetween).backgroundColor('#008000').expandSafeArea([SafeAreaType.SYSTEM], [SafeAreaEdge.TOP, SafeAreaEdge.BOTTOM])}
}

键盘避让

压缩、上抬

https://developer.huawei.com/consumer/cn/doc/harmonyos-references/ts-universal-attributes-expand-safe-area#示例6切换避让模式

import { hilog } from '@kit.PerformanceAnalysisKit';
import { KeyboardAvoidMode } from '@kit.ArkUI';
@Entry
@Componentstruct KeyboardAvoidExample3 {build() {Column() {Row({space:15}) {Button('OFFSET').onClick(() => {this.getUIContext().setKeyboardAvoidMode(KeyboardAvoidMode.OFFSET);hilog.info(0x0000, 'keyboardAvoidMode: %{public}s', JSON.stringify(this.getUIContext().getKeyboardAvoidMode()));}).layoutWeight(1)Button('RESIZE').onClick(() => {this.getUIContext().setKeyboardAvoidMode(KeyboardAvoidMode.RESIZE);hilog.info(0x0000, 'keyboardAvoidMode: %{public}s', JSON.stringify(this.getUIContext().getKeyboardAvoidMode()));}).layoutWeight(1)Button('NONE').onClick(() => {this.getUIContext().setKeyboardAvoidMode(KeyboardAvoidMode.NONE);hilog.info(0x0000, 'keyboardAvoidMode: %{public}s', JSON.stringify(this.getUIContext().getKeyboardAvoidMode()));}).layoutWeight(1)}.height("30%").width("100%").backgroundColor(Color.Gray)TextArea().width("100%").borderWidth(1)Text("I can see the bottom of the page").width("100%").textAlign(TextAlign.Center).backgroundColor('rgb(179,217,235)').layoutWeight(1)TextArea().width("100%").borderWidth(1)}.width('100%').height("100%")}
}

鸿蒙开发者班级

http://www.gsyq.cn/news/62629.html

相关文章:

  • HarmonyOS游戏开发入门:用ArkTS打造经典五子棋
  • 详细介绍:【论文阅读】AAAI 2025 | 面向精确分割式联邦学习的多模型聚合与知识重放
  • HarmonyOS ArkTS 状态管理 V2 全解析:新装饰器 + 实战示例 + 面试题(简述方便新手快速了解使用)
  • 2025最新机械密封件制造厂家实力榜!国内顶尖机械密封品牌权威发布,技术与服务双重保障,机械密封/机械密封件/釜用机械密封/双端面机械密封/机械密封维修品质与专业深度剖析,选择指南
  • 2025年度空调减震器制造企业排名:空调减震器制造企业选择哪
  • 2025年11月休闲食品品牌推荐榜:权威评测与消费者口碑全面解析
  • 2025年如何购买台式环形绕线机/全自动环形绕线机厂家选购指南与推荐
  • 2025年口碑好的铝方通隔断最新TOP厂家排名
  • 目录树
  • 2025年自动化立体库厂家推荐榜单最新发布:青岛茂源智能装备有限公司成为优选制造商
  • 2025年耐老化门窗尼龙隔热条厂家最新热销排行
  • 2025年覆盖面广泛全屋定制书柜/全屋定制衣帽间高评价厂家推荐榜
  • 2025年口碑好的低钠铝溶胶/铝溶胶厂家最新权威实力榜
  • 2025年耐用的三维弯管加工品牌厂家排行榜
  • 2025年中国十大益生菌靠谱厂家推荐:国内大型的益生菌工厂有
  • 2025年质量好的空气悬浮离心鼓风机/磁悬浮鼓风机厂家最新权威推荐排行榜
  • 2025公职面试培训品牌TOP5权威推荐:聚焦白雪面试对成长
  • 2025年11月审计事务所推荐榜:五家专业机构综合对比与选择指南
  • 2025年11月审计事务所市场报告:从资质到服务的全方位选择建议
  • 2025年评价高的冷拉型钢杭州品质装修
  • Diagzone Subscription Renewal for Thinkdiag2 DBSCAR7: Passenger, Electric Heavy Cars
  • 02-DBTrans事务管理核心教程
  • 2025年苹果售后维修网点查询指南:第三方连锁维修服务网络全解析
  • 2025年苹果售后维修网点查询指南:全国连锁维修服务与便捷预约方式
  • 中国酒店套房酒柜定制供应商推荐TOP5:现代风格酒柜定制优质
  • 2025年11月会计师事务所排行榜:主流机构对比与避坑指南
  • 2025年11月全屋定制哪家比较靠谱:十强全屋定制品牌哪家实
  • 如何选对20万家用SUV?2025年11月十大品牌深度解析,助力家庭精准决策不踩雷
  • 2025年耐用的小口径不锈钢焊管/大口径不锈钢焊管厂家推荐及选购指南
  • odoorpc下载附件