当前位置:网站首页>How ArkUI adapter somehow the screen
How ArkUI adapter somehow the screen
2022-08-03 16:38:00 【HUAWEI CLOUD】
媒体查询(Media Query)在移动设备上应用十分广泛,开发者经常需要根据设备的大致类型或者特定的特征和设备参数(例如屏幕分辨率)来修改应用的样式.为此媒体查询提供了如下功能:
针对设备和应用的属性信息,可以设计出相匹配的布局样式.
当屏幕发生动态改变时(比如分屏、横竖屏切换),应用页面布局同步更新.
如何使用
By calling the media query interface,Set the media query condition and the callback function of the query result,Change the page layout or implement business logic in the callback function of the corresponding condition.
First import the media query module,例如:
import mediaquery from '@ohos.mediaquery'然后通过matchMediaSyncThe interface sets media query conditions,And save the returned conditional listener handle,例如:
listener = mediaquery.matchMediaSync('(orientation: landscape)')Finally, listen to the handle through the condition saved abovelistenerto register callbacks函数,Change the page layout or implement business logic in the callback function,This callback function is fired when the media query condition is matched,例如:
import mediaquery from '@ohos.mediaquery' let listener = mediaquery.matchMediaSync('(orientation: landscape)'); //Listen for horizontal screen events function onPortrait(mediaQueryResult) { if (mediaQueryResult.matches) { //你的逻辑 this.islandscape = true } else { //你的逻辑 this.islandscape = false } } this.listener.on('change', this.onPortrait) // 注册回调 this.listener.off('change', this.onPortrait) // to register callbacks例子:
import mediaquery from '@ohos.mediaquery'let portraitFunc = null@[email protected] MediaQueryExample { @State islandscape: boolean = false listener = mediaquery.matchMediaSync('(orientation: landscape)') onPortrait(mediaQueryResult) { if (mediaQueryResult.matches) { this.islandscape = true } else { this.islandscape = false } } aboutToAppear() { portraitFunc = this.onPortrait.bind(this) //bind current js instance this.listener.on('change', portraitFunc) } build() { Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Center, justifyContent: FlexAlign.Center }) { Text( this.islandscape==true ? "横屏显示": "竖屏显示") .backgroundColor(Color.Blue) .textAlign(TextAlign.Center) .fontSize('50fp') .fontColor(Color.White) .width('100%') .height('100vp') } .width('100%').height('100%') }}
Media query condition syntax rules
[media-type] [and|not|only] [(media-feature)]例如:
screen and (round-screen: true) :The condition is true when the device screen is circular
(max-height: 800) :when the height is less than 800时条件成立
(height <= 800) :when the height is less than 800时条件成立
screen and (device-type: tv) or (resolution < 2) :Multi-condition complex statement query containing multiple media features
媒体特征(media-feature)
| 类型 | 说明 |
|---|---|
| height | The height of the display area of the application page. |
| min-height | The minimum height of the display area of the application page. |
| max-height | The maximum height of the display area of the application page. |
| width | The width of the display area of the application page. |
| min-width | Applies the minimum width of the page display area. |
| max-width | The maximum width of the display area of the application page. |
| resolution | 设备的分辨率,支持dpi,dppx和dpcm单位.其中:- dpiIndicates the number of physical pixels per inch,1dpi≈0.39dpcm;- dpcmIndicates the number of physical pixels per centimeter,1dpcm ≈ 2.54dpi;- dppx表示每个pxThe number of physical pixels in (Press this unit96px=1inches as a reference,与页面中的pxUnits are calculated differently),1dppx = 96dpi. |
| min-resolution | 设备的最小分辨率. |
| max-resolution | 设备的最大分辨率. |
| orientation | 屏幕的方向.可选值:- orientation: portrait(Device vertical screen)- orientation: landscape(The device is in landscape orientation) |
| device-height | 设备的高度. |
| min-device-height | The minimum height of the device. |
| max-device-height | The maximum height of the device. |
| device-width | 设备的宽度. |
| min-device-width | The minimum width of the device. |
| max-device-width | The maximum width of the device. |
| round-screen | 屏幕类型,The circular screen istrue, Non-circular screens are false. |
| dark-mode | When the system is in dark modetrue,否则为false. |
参考文档
边栏推荐
- 详谈RDMA技术原理和三种实现方式
- 高薪程序员&面试题精讲系列132之微服务之间如何进行通信?服务熔断是怎么回事?你熟悉Hystrix吗?
- Some optional strategies and usage scenarios for PWA application Service Worker caching
- 新版本 MaxCompute 的SQL 中支持的 EXTRACT 函数有什么作用?
- 如何选择合适的导电滑环型号
- Tolstoy: There are only two misfortunes in life
- 超分重建数据集
- C专家编程 第3章 分析C语言的声明 3.3 优先级规则
- 高效的组织信息共享知识库是一种宝贵的资源
- C专家编程 第3章 分析C语言的声明 3.6 typedef int x[10]和#define x int[10]的区别
猜你喜欢

Analysis of ffplay video playback principle

C专家编程 第3章 分析C语言的声明 3.7 typedef struct foo{... foo;}的含义

正向代理与反向代理

#夏日挑战赛# HarmonyOS 实现一个绘画板

最强分布式锁工具:Redisson

How to analyze the weekly activity rate?

从零开始搭建MySQL主从复制架构

TCP 可靠吗?为什么?

Not to be ignored!Features and advantages of outdoor LED display

Understand the recommendation system in one article: Outline 02: The link of the recommendation system, from recalling rough sorting, to fine sorting, to rearranging, and finally showing the recommend
随机推荐
【翻译】关于扩容一个百万级别用户系统的六个课程
如何在 DataWorks 中 写SQL语句监控数据的变化到达一定的值 进行提示
C专家编程 第3章 分析C语言的声明 3.5 typedef可以成为你的朋友
关于oracle表空间在线碎片整理
When mobile applications go overseas, is your "network optimization" holding back?
Web3 安全风险令人生畏?应该如何应对?
Understand the recommendation system in one article: Outline 02: The link of the recommendation system, from recalling rough sorting, to fine sorting, to rearranging, and finally showing the recommend
如何设计大电流九线导电滑环
滑环安装注意事项
protobuf 中数据编码规则
WordPress建站技术笔记
uniapp的webview滑动缩放
protobuf 反射使用总结
甲方不让用开源【监控软件】?大不了我自己写一个
C专家编程 第1章 C:穿越时空的迷雾 1.11 轻松一下---由编译器定义的Pragmas效果
Small Tools (4) integrated Seata1.5.2 distributed transactions
Async的线程池使用的哪个?
To add digital wings to education, NetEase Yunxin released the overall solution of "Internet + Education"
大佬们。使用flink-cdc-sqlserver 2.2.0 版本读取sqlserver2008R
[Deep Learning] Today's bug (August 2)