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

鸿蒙:使用媒体查询监听屏幕方向、切换横竖屏

1. 前言

媒体查询(@ohos.mediaquery)作为响应式设计的核心,在移动设备上应用十分广泛。媒体查询可根据不同设备类型或同设备不同状态修改应用的样式。媒体查询常用于下面两种场景:

  1. 针对设备和应用的属性信息(比如显示区域、深浅色、分辨率),设计出相匹配的布局。
  2. 当屏幕发生动态改变时(比如分屏、横竖屏切换),同步更新应用的页面布局。

2. 参考文档

https://developer.huawei.com/consumer/cn/doc/harmonyos-guides/arkts-layout-development-media-query

3. 核心思路

  1. 使用媒体查询api组件初始化前监听当前的屏幕方向状态
  2. 通过媒体查询api点击ui组件会触发切换当前的屏幕方向状态

4. 核心代码

/// 获取当前方向状态
getCurrentOrientation() {this.listener.on('change', (mediaQueryResult: mediaquery.MediaQueryResult) => {console.log("监听结果" + mediaQueryResult.media)if (mediaQueryResult.matches) {this.currentState = "横屏"} else {this.currentState = "竖屏"}});
}
/// 切换横屏
switchToLandscape() {// 获取应用的最后一个窗口实例window.getLastWindow(this.context).then((lastWindow) => {// 根据参数设置窗口首选方向:横屏lastWindow.setPreferredOrientation(window.Orientation.LANDSCAPE)});}/// 切换竖屏
switchToPortrait() {// 获取应用的最后一个窗口实例window.getLastWindow(this.context).then((lastWindow) => {// 根据参数设置窗口首选方向:竖屏lastWindow.setPreferredOrientation(window.Orientation.PORTRAIT)});}

5. 运行效果

6. 完整代码

Index.ets

import { mediaquery, window } from '@kit.ArkUI';@Entry
@ComponentV2
struct Index {@Local currentState: string = ""listener: mediaquery.MediaQueryListener =this.getUIContext().getMediaQuery().matchMediaSync('(orientation: landscape)');// 获取当前应用的UIAbility上下文context = this.getUIContext().getHostContext()!;aboutToAppear(): void {// 监听屏幕方向改变this.getCurrentOrientation()}/// 获取当前方向状态getCurrentOrientation() {this.listener.on('change', (mediaQueryResult: mediaquery.MediaQueryResult) => {console.log("监听结果" + mediaQueryResult.media)if (mediaQueryResult.matches) {this.currentState = "横屏"} else {this.currentState = "竖屏"}});}/// 切换横屏switchToLandscape() {// 获取应用的最后一个窗口实例window.getLastWindow(this.context).then((lastWindow) => {// 根据参数设置窗口首选方向:横屏lastWindow.setPreferredOrientation(window.Orientation.LANDSCAPE)});}/// 切换竖屏switchToPortrait() {// 获取应用的最后一个窗口实例window.getLastWindow(this.context).then((lastWindow) => {// 根据参数设置窗口首选方向:竖屏lastWindow.setPreferredOrientation(window.Orientation.PORTRAIT)});}build() {Column({ space: 150 }) {Text("当前的状态:" + this.currentState).fontSize(30).fontWeight(FontWeight.Bold)Column({ space: 20 }) {Button("点击切换横屏").onClick(() => {this.switchToLandscape()}).backgroundColor(this.currentState == "横屏" ? Color.Black : Color.Blue)Button("点击切换竖屏").onClick(() => {this.switchToPortrait()}).backgroundColor(this.currentState == "横屏" ? Color.Black : Color.Blue)}}.justifyContent(FlexAlign.Center).width("100%").height("100%")}
}

觉得有帮助,可以点赞或收藏

http://www.dtcms.com/a/490050.html

相关文章:

  • 8.list的使用
  • 网页跳转github镜像
  • 安灯系统(Andon)如何为汽车工厂打造零延迟响应
  • C++(条件判断与循环)
  • 温州建设局网站首页中国企业名录黄页
  • linux/centos迁移conda文件夹
  • Quill 富文本编辑器 功能介绍,使用场景说明,使用示例演示
  • 网站生成器怎么做网站建设与管理实训主要内容
  • 网站信用认证可以自己做吗稀奇古怪好玩有用的网站
  • MySQL 基础语句
  • Linux中CPU初始化和调度器初始化函数的实现
  • MATLAB基于ST-CNN-SVM的轴承故障诊断,S变换和卷积神经网络结合支持向量机
  • 在优豆云免费云服务器上初探SSH与SCP的便捷操作
  • MySQL数据库:软件、相关知识和基本操作
  • Bahdanau注意力
  • 重生之我在大学自学鸿蒙开发第七天-《AI语音朗读》
  • Spring AI 1.0 GA 深度解析:Java生态的AI革命已来
  • Linux网络之----TCP网络编程
  • 【零基础学习CAPL语法】——writeLineEx() 函数
  • 计算机网络数据链路层
  • 做网站选什么专业门户网站开发步骤博客
  • 论文写作 24: 全文保持同样的节奏
  • 洛谷 P1438 无聊的数列 题解
  • iOS混淆与IPA加固实战手记,如何构建苹果应用防反编译体系
  • 想抓PostgreSQL里的慢SQL?pg_stat_statements基础黑匣子和pg_stat_monitor时间窗,谁能帮你更准揪出性能小偷?
  • 把 iOS 26 的「Liquid Glass」带进 React Native
  • 基于物理信息的神经网络求解偏微分方程反问题的综合优化策略
  • 工地佩戴安全帽检测-目标检测数据集
  • 广东网站备案查询系统企业网站带后台
  • 知名的集团门户网站建设费用我要自学网网站建设