【HarmonyOS 5】鸿蒙应用代码控制横竖屏切换,自动切换横竖屏,监听横竖屏以及注意事项

GeorgeGcs
• 阅读 3

##鸿蒙开发能力 ##HarmonyOS SDK应用服务##鸿蒙金融类应用 (金融理财#

一、鸿蒙应用如何进行页面横竖屏调用API手动切换 1.首先要在EntryAbility 中获取主窗口对象 EntryAbility.ets import { AbilityConstant, UIAbility, Want } from '@kit.AbilityKit'; import { window } from '@kit.ArkUI';

export default class EntryAbility extends UIAbility {

onWindowStageCreate(windowStage: window.WindowStage): void { // 挂载globalThis上,可以当全局对象使用。当然此处实现方式因人而异,你可以放在单例里,或者localstore中等等 globalThis.windowClass = windowStage.getMainWindowSync(); windowStage.loadContent('pages/RotationTestPage', (err) => { if (err.code) { return; } }); } }

之后在需要调用横竖屏切换的页面或者逻辑中调用,我这里用按钮触发举例: RotationTestPage.ets import { BusinessError } from '@kit.BasicServicesKit'; import { window } from '@kit.ArkUI';

@Entry @Component struct RotationTestPage { private TAG: string = "RotationTestPage";

onClickRotation = ()=>{ // 设置横竖屏状态 let orientation = window.Orientation.LANDSCAPE; try{ globalThis.windowClass.setPreferredOrientation(orientation, (err: BusinessError) => { if(err.code){ console.error(this.TAG, 'Failed to set window orientation. Cause: ' + JSON.stringify(err)); return; } console.info(this.TAG,'Succeeded in setting window orientation.'); }); }catch (exception) { console.error(this.TAG,'Failed to set window orientation. Cause: ' + JSON.stringify(exception)); } }

build() { RelativeContainer() { Text("点击切换为横屏") .id('RotationTestPageHelloWorld') .fontSize(50) .fontWeight(FontWeight.Bold) .alignRules({ center: { anchor: 'container', align: VerticalAlign.Center }, middle: { anchor: 'container', align: HorizontalAlign.Center } }) .onClick(this.onClickRotation) } .height('100%') .width('100%') } } window.Orientation具体数值,参见以下网址 注意: 设置主窗口的显示方向属性。仅在支持跟随sensor旋转的设备上生效,子窗口调用后不生效。 二、如何实现应用的屏幕自动旋转 在module.json5添加属性"orientation": "auto_rotation"。 "abilities": [ { "name": "EntryAbility", "srcEntry": "./ets/entryability/EntryAbility.ets", "description": "$string:EntryAbility_desc", "icon": "$media:icon", "label": "$string:EntryAbility_label", "startWindowIcon": "$media:startIcon", "startWindowBackground": "$color:start_window_background", "exported": true, "skills": [ { "entities": [ "entity.system.home" ], "actions": [ "action.system.home" ] } ], "orientation": "auto_rotation", // 随传感器旋转 } ] 注意: auto_rotation随传感器旋转 需要在系统下滑菜单中,放开自动锁定状态才可生效。 三、如何监听屏幕旋转 使用媒体查询接口监听屏幕旋转 import { mediaquery } from '@kit.ArkUI'; let listener = mediaquery.matchMediaSync('(orientation: landscape)'); // 监听横屏事件 function onPortrait(mediaQueryResult: mediaquery.MediaQueryResult) { if (mediaQueryResult.matches) { // do something here } else { // do something here } } listener.on('change', onPortrait) // 注册回调 listener.off('change', onPortrait) // 去注册回调

点赞
收藏
评论区
推荐文章
Stella981 Stella981
3年前
Android屏幕横竖屏切换和生命周期管理的详细总结
  一般的我们去切换屏幕方向都是不希望Activity被重新创建,这时就需要对一些属性进行设置,或者使用代码设置。        今天想学一下Android屏幕横竖屏切换,但是网上很多知识不准确或不正确,这里我还是自己总结一篇文章,供大家参考。一.屏幕横竖屏切换的代码       很多文章一上来就将什么生命周期或者
陈杨 陈杨
2天前
鸿蒙5开发宝藏案例分享---平板开发实践
以下是根据官方文档整理的鸿蒙平板开发实战指南,结合代码解析和避坑要点,帮你高效实现“一次开发,多端部署”👇一、开篇:为什么平板开发不同?平板三大特性决定开发策略:1.大屏优势:分辨率高→需展示更多内容(如分栏/网格布局)1.形态灵活:横竖屏旋转自由窗口
GeorgeGcs GeorgeGcs
16小时前
【 HarmonyOS 5 入门系列 】鸿蒙HarmonyOS示例项目讲解
【HarmonyOS5入门系列】鸿蒙HarmonyOS示例项目讲解\鸿蒙开发能力HarmonyOSSDK应用服务鸿蒙金融类应用(金融理财一、前言:移动开发声明式UI框架的技术变革在移动操作系统的发展历程中,UI开发模式经历了从命令式到声明式的重大变革。根据
GeorgeGcs GeorgeGcs
16小时前
【HarmonyOS 5】AttributeModifier和AttributeUpdater区别详解
【HarmonyOS5】AttributeModifier和AttributeUpdater区别详解\鸿蒙开发能力HarmonyOSSDK应用服务鸿蒙金融类应用(金融理财一、AttributeModifier和AttributeUpdater的定义和作用1
GeorgeGcs GeorgeGcs
9小时前
从“备胎”到领航者,鸿蒙操作系统的传奇进化
鸿蒙开发能力HarmonyOSSDK应用服务鸿蒙金融类应用(金融理财【HarmonyOS5】2019年,在全球科技产业的风云变幻中,华为正式推出了鸿蒙操作系统(HarmonyOS),这一消息如同一颗重磅炸弹,瞬间吸引了全世界的目光。彼时,外界对鸿蒙的诞生背
GeorgeGcs GeorgeGcs
7小时前
【HarmonyOS 5】鸿蒙中Stage模型与FA模型详解
鸿蒙开发能力HarmonyOSSDK应用服务鸿蒙金融类应用(金融理财一、前言在HarmonyOS5的应用开发模型中,featureAbility是旧版FA模型(FeatureAbility)的用法,Stage模型已采用全新的应用架构,推荐使用组件化的上下文
GeorgeGcs GeorgeGcs
7小时前
【HarmonyOS 5】金融应用开发鸿蒙组件实践
鸿蒙开发能力HarmonyOSSDK应用服务鸿蒙金融类应用(金融理财一、鸿蒙生态观察2024年1月18日:发布原生鸿蒙操作系统星河版,面向开发者开放申请,余承东宣布鸿蒙生态设备数达8亿台;建设银行、邮储银行等完成鸿蒙原生应用Beta版本开发。2024年10
GeorgeGcs GeorgeGcs
7小时前
【HarmonyOS NEXT】鸿蒙应用实现手机摇一摇功能
鸿蒙开发能力HarmonyOSSDK应用服务鸿蒙金融类应用(金融理财一、前言手机摇一摇功能,是通过获取手机设备,加速度传感器接口,获取其中的数值,进行逻辑判断实现的功能。在鸿蒙中手机设备传感器@ohos.sensor(传感器)的系统API监听有以下:@oh
GeorgeGcs GeorgeGcs
7小时前
【HarmonyOS 5】鸿蒙中Stage模型与FA模型详解
鸿蒙开发能力HarmonyOSSDK应用服务鸿蒙金融类应用(金融理财一、前言在HarmonyOS5的应用开发模型中,featureAbility是旧版FA模型(FeatureAbility)的用法,Stage模型已采用全新的应用架构,推荐使用组件化的上下文
GeorgeGcs GeorgeGcs
5小时前
【HarmonyOS 5】鸿蒙发展历程
鸿蒙开发能力HarmonyOSSDK应用服务鸿蒙金融类应用(金融理财一、鸿蒙HarmonyOS版本年代记鸿蒙1.0:2019年8月9日,华为在开发者大会上正式发布鸿蒙1.0系统,这一版本首次应用于华为荣耀智慧屏产品中,标志着华为正式进军操作系统领域。该版本
GeorgeGcs
GeorgeGcs
Lv1
男 · 金融头部企业 · 鸿蒙应用架构师
HarmonyOS认证创作先锋,华为HDE专家,鸿蒙讲师,作者。目前任职鸿蒙应用架构师。 历经腾讯,宝马,研究所,金融。 待过私企,外企,央企。 深耕大应用开发领域十年。 AAE,Harmony(OpenHarmony\HarmonyOS),MAE(Android\IOS),FE(H5\Vue\RN)。
文章
56
粉丝
1
获赞
2