1# 媒体查询 (@ohos.mediaquery) 2 3 4## 概述 5 6[媒体查询](../reference/apis-arkui/js-apis-mediaquery.md)作为响应式设计的核心,在移动设备上应用十分广泛。媒体查询可根据不同设备类型或同设备不同状态修改应用的样式。媒体查询常用于下面两种场景: 7 81. 针对设备和应用的属性信息(比如显示区域、深浅色、分辨率),设计出相匹配的布局。 9 102. 当屏幕发生动态改变时(比如分屏、横竖屏切换),同步更新应用的页面布局。 11 12 13## 引入与使用流程 14 15媒体查询通过mediaquery模块接口,设置查询条件并绑定回调函数,任一[媒体特征](#媒体特征media-feature)改变时,均会触发回调函数,返回匹配结果,根据返回值更改页面布局或者实现业务逻辑,实现页面的响应式设计。具体步骤如下: 16 17首先导入媒体查询模块。 18 19 20```ts 21import { mediaquery } from '@kit.ArkUI'; 22``` 23 24通过matchMediaSync接口设置媒体查询条件,保存返回的条件监听句柄listener。例如监听横屏事件: 25 26 27```ts 28let listener: mediaquery.MediaQueryListener = this.getUIContext().getMediaQuery().matchMediaSync('(orientation: landscape)'); 29``` 30 31给条件监听句柄listener绑定回调函数onPortrait,当listener检测设备状态变化时执行回调函数。在回调函数内,根据不同设备状态更改页面布局或者实现业务逻辑。 32 33 34```ts 35onPortrait(mediaQueryResult: mediaquery.MediaQueryResult) { 36 if (mediaQueryResult.matches as boolean) { 37 // do something here 38 } else { 39 // do something here 40 } 41} 42 43listener.on('change', onPortrait); 44``` 45 46 47## 媒体查询条件 48 49媒体查询条件由媒体类型、逻辑操作符、媒体特征组成,其中媒体类型可省略,逻辑操作符用于连接不同媒体类型与媒体特征,其中,媒体特征要使用“()”包裹且可以有多个。 50 51 52### 语法规则 53 54语法规则包括[媒体类型(media-type)](#媒体类型media-type)、[媒体逻辑操作(media-logic-operations)](#媒体逻辑操作media-logic-operations)和[媒体特征(media-feature)](#媒体特征media-feature)。 55 56 57```ts 58[media-type] [media-logic-operations] [(media-feature)] 59``` 60 61例如: 62 63- screen and (round-screen: true) :表示当设备屏幕是圆形时条件成立。 64 65- (max-height: 800px) :表示当高度小于等于800px时条件成立。 66 67- (height <= 800px) :表示当高度小于等于800px时条件成立。 68 69- screen and (device-type: tv) or (resolution < 2) :表示包含多个媒体特征的多条件复杂语句查询,当设备类型为tv或设备分辨率小于2时条件成立。 70 71- (dark-mode: true) :表示当系统为深色模式时成立。 72 73 74### 媒体类型(media-type) 75查询条件未写媒体类型时,默认为screen。媒体类型必须写在查询条件开头。 76 77| **类型** | **说明** | 78| ------ | -------------- | 79| screen | 按屏幕相关参数进行媒体查询。 | 80 81 82### 媒体逻辑操作(media-logic-operations) 83 84媒体逻辑操作符:and、or、not、only用于构成复杂媒体查询,也可以通过comma(, )将其组合起来,详细解释说明如下表。 85 86 **表1** 媒体逻辑操作符 87 88| 类型 | 说明 | 89| ---------------- | ------------------------------------------------------------ | 90| and | 将多个媒体特征(Media Feature)以“与”的方式连接成一个媒体查询,只有当所有媒体特征都为true,查询条件成立。另外,它还可以将媒体类型和媒体功能结合起来。例如:screen and (device-type: wearable) and (max-height: 600px) 表示当设备类型是智能穿戴且应用的最大高度小于等于600个像素单位时成立。 | 91| or | 将多个媒体特征以“或”的方式连接成一个媒体查询,如果存在结果为true的媒体特征,则查询条件成立。例如:screen and (max-height: 1000px) or (round-screen: true) 表示当应用高度小于等于1000个像素单位或者设备屏幕是圆形时,条件成立。 | 92| not | not操作符必须搭配screen使用,取反媒体查询结果,媒体查询结果不成立时返回true,否则返回false。例如:not screen and (min-height: 50px) and (max-height: 600px) 表示当应用高度小于50个像素单位或者大于600个像素单位时成立。 | 93| only | only操作符必须搭配screen使用, 当前效果与单独使用screen相同。例如:only screen and (height <= 50) 。| 94| comma(, ) | 将多个媒体特征以“或”的方式连接成一个媒体查询,如果存在结果为true的媒体特征,则查询条件成立。其效果等同于or运算符。例如:screen and (min-height: 1000px), (round-screen: true) 表示当应用高度大于等于1000个像素单位或者设备屏幕是圆形时,条件成立。 | 95 96媒体范围操作符包括<=,>=,<,>,详细解释说明如下表。 97 98 **表2** 媒体逻辑范围操作符 99 100| 类型 | 说明 | 101| ----- | ---------------------------------------- | 102| <= | 小于等于,例如:screen and (height <= 50)。 | 103| >= | 大于等于,例如:screen and (height >= 600)。 | 104| < | 小于,例如:screen and (height < 50)。 | 105| > | 大于,例如:screen and (height > 600)。 | 106 107 108### 媒体特征(media-feature) 109 110媒体特征包括应用显示区域的宽高、设备分辨率以及设备的宽高等属性,详细说明如下表。 111 112 **表3** 媒体特征说明表 113 114比较height、width等宽高尺寸时,支持vp和px单位,无单位默认为px。 115 116| 类型 | 说明 | 117| ----------------- | ---------------------------------------- | 118| height | 应用页面可绘制区域的高度。 | 119| min-height | 应用页面可绘制区域的最小高度。 | 120| max-height | 应用页面可绘制区域的最大高度。 | 121| width | 应用页面可绘制区域的宽度。 | 122| min-width | 应用页面可绘制区域的最小宽度。 | 123| max-width | 应用页面可绘制区域的最大宽度。 | 124| resolution | 设备的分辨率,支持dpi,dppx和dpcm单位。其中:<br/>- dpi表示每英寸中物理像素个数,1dpi ≈ 0.39dpcm;<br/>- dpcm表示每厘米上的物理像素个数,1dpcm ≈ 2.54dpi;<br/>- dppx表示每个px中的物理像素数(此单位按96px = 1英寸为基准,与页面中的px单位计算方式不同),1dppx = 96dpi。 | 125| min-resolution | 设备的最小分辨率。 | 126| max-resolution | 设备的最大分辨率。 | 127| orientation | 屏幕的方向。<br/>可选值:<br/>- orientation: portrait(设备竖屏);<br/>- orientation: landscape(设备横屏)。 | 128| device-height | 设备的高度。 | 129| min-device-height | 设备的最小高度。 | 130| max-device-height | 设备的最大高度。 | 131| device-width | 设备的宽度。当前仅在应用初始化时保存一次,不会随设备宽度变化实时更新,例如折叠屏的折叠展开场景。 | 132| device-type | 设备的类型。<br/>可选值:default、phone、tablet、tv、car、wearable、2in1。 | 133| min-device-width | 设备的最小宽度。 | 134| max-device-width | 设备的最大宽度。 | 135| round-screen | 屏幕类型,圆形屏幕为true,非圆形屏幕为false。 | 136| dark-mode | 系统当前的深浅模式。可选值:true、false。 <br/> 深色模式为true,浅色模式为false。 | 137 138>**说明:** 139> 140>目前在卡片中使用媒体查询,只支持height、width。 141 142## 场景示例 143 144下例中使用媒体查询,实现屏幕横竖屏切换时,给页面文本应用添加不同的内容和样式。 145 146Stage模型下的示例: 147 148 149```ts 150import { mediaquery, window } from '@kit.ArkUI'; 151import { common } from '@kit.AbilityKit'; 152 153@Entry 154@Component 155struct MediaQueryExample { 156 @State color: string = '#DB7093'; 157 @State text: string = 'Portrait'; 158 // 当设备横屏时条件成立 159 listener:mediaquery.MediaQueryListener = this.getUIContext().getMediaQuery().matchMediaSync('(orientation: landscape)'); 160 161 // 当满足媒体查询条件时,触发回调 162 onPortrait(mediaQueryResult:mediaquery.MediaQueryResult) { 163 if (mediaQueryResult.matches as boolean) { // 若设备为横屏状态,更改相应的页面布局 164 this.color = '#FFD700'; 165 this.text = 'Landscape'; 166 } else { 167 this.color = '#DB7093'; 168 this.text = 'Portrait'; 169 } 170 } 171 172 aboutToAppear() { 173 // 绑定当前应用实例 174 // 绑定回调函数 175 this.listener.on('change', (mediaQueryResult: mediaquery.MediaQueryResult) => { 176 this.onPortrait(mediaQueryResult) 177 }); 178 } 179 180 aboutToDisappear() { 181 // 解绑listener中注册的回调函数 182 this.listener.off('change'); 183 } 184 185 // 改变设备横竖屏状态函数 186 private changeOrientation(isLandscape: boolean) { 187 // 获取UIAbility实例的上下文信息 188 let context:common.UIAbilityContext = this.getUIContext().getHostContext() as common.UIAbilityContext; 189 // 调用该接口手动改变设备横竖屏状态 190 window.getLastWindow(context).then((lastWindow) => { 191 lastWindow.setPreferredOrientation(isLandscape ? window.Orientation.LANDSCAPE : window.Orientation.PORTRAIT) 192 }); 193 } 194 195 build() { 196 Column({ space: 50 }) { 197 Text(this.text).fontSize(50).fontColor(this.color) 198 Text('Landscape').fontSize(50).fontColor(this.color).backgroundColor(Color.Orange) 199 .onClick(() => { 200 this.changeOrientation(true); 201 }) 202 Text('Portrait').fontSize(50).fontColor(this.color).backgroundColor(Color.Orange) 203 .onClick(() => { 204 this.changeOrientation(false); 205 }) 206 } 207 .width('100%').height('100%') 208 } 209} 210``` 211 212FA模型下的示例: 213 214 215```ts 216import { mediaquery } from '@kit.ArkUI'; 217import { featureAbility } from '@kit.AbilityKit'; 218 219@Entry 220@Component 221struct MediaQueryExample { 222 @State color: string = '#DB7093'; 223 @State text: string = 'Portrait'; 224 listener:mediaquery.MediaQueryListener = mediaquery.matchMediaSync('(orientation: landscape)'); // 当设备横屏时条件成立 225 226 onPortrait(mediaQueryResult:mediaquery.MediaQueryResult) { // 当满足媒体查询条件时,触发回调 227 if (mediaQueryResult.matches as boolean) { // 若设备为横屏状态,更改相应的页面布局 228 this.color = '#FFD700'; 229 this.text = 'Landscape'; 230 } else { 231 this.color = '#DB7093'; 232 this.text = 'Portrait'; 233 } 234 } 235 236 aboutToAppear() { 237 // 绑定当前应用实例 238 this.listener.on('change', (mediaQueryResult:mediaquery.MediaQueryResult) => { this.onPortrait(mediaQueryResult) }); //绑定回调函数 239 } 240 241 aboutToDisappear() { 242 // 解绑listener中注册的回调函数 243 this.listener.off('change'); 244 } 245 246 build() { 247 Column({ space: 50 }) { 248 Text(this.text).fontSize(50).fontColor(this.color) 249 Text('Landscape').fontSize(50).fontColor(this.color).backgroundColor(Color.Orange) 250 .onClick(() => { 251 let context = featureAbility.getContext(); 252 context.setDisplayOrientation(0); //调用该接口手动改变设备横竖屏状态 253 }) 254 Text('Portrait').fontSize(50).fontColor(this.color).backgroundColor(Color.Orange) 255 .onClick(() => { 256 let context = featureAbility.getContext(); 257 context.setDisplayOrientation(1); //调用该接口手动改变设备横竖屏状态 258 }) 259 } 260 .width('100%').height('100%') 261 } 262} 263``` 264 265 **图1** 竖屏 266 267 268 269 **图2** 横屏 270 271 272 273## 相关实例 274 275针对媒体查询开发,有以下相关实例可供参考: 276 277- [横竖屏切换(ArkTS)(API9)](https://gitee.com/openharmony/applications_app_samples/tree/master/code/UI/ArkTsComponentCollection/MediaQuery) 278