当前位置:网站首页>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. |
参考文档
边栏推荐
- Windows 事件转发到 SQL 数据库
- 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
- 【系统学习编程-编程入门-全民编程 视频教程】
- MySQL窗口函数 PARTITION BY()函数介绍
- 甲方不让用开源【监控软件】?大不了我自己写一个
- devops-2:Jenkins的使用及Pipeline语法讲解
- Windows 事件查看器记录到 MYSQL
- MPLS的wpn实验
- [Unity Getting Started Plan] Basic Concepts (8) - Tile Map TileMap 01
- leetcode SVM
猜你喜欢
随机推荐
WordPress 5.2.3 更新,升级出现请求超时的解决方法
【AppCube】零代码小课堂开课啦
怎么在opengauss中进行测试自己添加的新函数的性能(循环n次的运行时间)?
83. Remove Duplicates from Sorted List
Selective Search学习笔记
如何使用MATLAB绘制极坐标堆叠柱状图
MATLAB | 七夕节快到了,还不给朋友安排上这个咕呱小青蛙?
When mobile applications go overseas, is your "network optimization" holding back?
视频人脸识别和图片人脸识别的关系
世界顶级级架构师编写2580页DDD领域驱动设计笔记,属实有牌面
C专家编程 第3章 分析C语言的声明 3.2 声明是如何形成的
uniapp的webview滑动缩放
Common distributed theories (CAP, BASE) and consensus protocols (Gosssip, Raft)
TCP 可靠吗?为什么?
Hannah荣获第六季完美童模全球总决赛全球人气总冠军
Difference and performance comparison between HAL and LL library of STM32
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
甲方不让用开源【监控软件】?大不了我自己写一个
[redis] cache penetration and cache avalanche and cache breakdown solutions
To participate in sweepstakes, incoming new programmers magazine welfare!