ui-ts-layout-mediaquery.md 9.2 KB
Newer Older
E
ester.zhou 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
# Media Query


Media queries are widely used on mobile devices. You can use them to modify the application style based on the device type or specific features and device parameters (such as the screen resolution). Specifically, media queries allow you to:


1. Design a layout style based on the device and app attributes.

2. Update the page layout to adapt to dynamic screen changes (for example, screen splitting or switching between landscape and portrait modes).


## Usage

Invoke the **mediaquery** API to set the media query condition and the callback, and change the page layout or implement service logic in the callback corresponding to the condition.

First, import the mediaquery module, as shown below:
E
ester.zhou 已提交
17
```ts
E
ester.zhou 已提交
18
import mediaquery from '@ohos.mediaquery'
E
ester.zhou 已提交
19 20
```
Then, use the **matchMediaSync** API to set the media query condition and save the returned listener, as shown below:
E
ester.zhou 已提交
21
```ts
E
ester.zhou 已提交
22
listener = mediaquery.matchMediaSync('(orientation: landscape)')
E
ester.zhou 已提交
23 24
```
Finally, register the callback using the saved listener, and change the page layout or implement service logic in the callback. When the media query condition is matched, the callback is triggered. The sample code is as follows:
E
ester.zhou 已提交
25
```ts
E
ester.zhou 已提交
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
onPortrait(mediaQueryResult) {
    if (mediaQueryResult.matches) {
        // do something here
    } else {
        // do something here
    }
}
listener.on('change', onPortrait)
```

## Syntax of Media Query Conditions
```
[media-type] [and|not|only] [(media-feature)]
```
Examples are as follows:
E
ester.zhou 已提交
41

E
ester.zhou 已提交
42
 `screen and (round-screen: true)`  // The query is valid when the device screen is round.
E
ester.zhou 已提交
43

E
ester.zhou 已提交
44
 `(max-height: 800)`  // The query is valid when the height does not exceed 800.
E
ester.zhou 已提交
45

E
ester.zhou 已提交
46
 `(height <= 800)`   // The query is valid when the height does not exceed 800.
E
ester.zhou 已提交
47

E
ester.zhou 已提交
48
 `screen and (device-type: tv) or (resolution < 2)`  // The query is valid only when all media features are true.
E
ester.zhou 已提交
49 50 51

###  media-type

E
ester.zhou 已提交
52 53
| Type    | Description            |
| ------ | -------------- |
E
ester.zhou 已提交
54
| screen | Media query based on screen-related parameters.|
E
ester.zhou 已提交
55 56 57 58 59 60


### Media Logic Operation (and|not|only)

You can use logical operators (**and**, **or**, **not**, and **only**) to compose complex media queries. You can also combine them using comma (,). The following table describes the operators.

E
ester.zhou 已提交
61
**Table 1** Media logical operators
E
ester.zhou 已提交
62

E
ester.zhou 已提交
63 64
| Type      | Description                                      |
| -------- | ---------------------------------------- |
E
ester.zhou 已提交
65
| and      | The **and** operator is used to combine multiple media features into one media query, in a logical AND operation. The query is valid only when all media features are true. It can also combine media types and media functions.<br>For example, **screen and (device-type: wearable) and (max-height: 600) ** indicates that the query is valid when the device type is wearable and the maximum height of the application is 600 pixel units.|
E
ester.zhou 已提交
66
| or       | The **or** operator is used to combine multiple media features into one media query, in a logical OR operation. The query is valid if a media feature is true.<br>For example, **screen and (max-height: 1000) or  (round-screen: true)** indicates that the query is valid when the maximum height of the application is 1000 pixel units or the device screen is round. |
E
ester.zhou 已提交
67 68 69
| not      | The **not** operator is used to perform a logical negation for a media query. **true** is returned if the query condition is not met. Otherwise, **false** is returned. In a media query list, logical negation is performed only for the media query using the **not** operator.<br>For example, **not screen and (min-height: 50) and (max-height: 600) ** indicates that the query is valid when the height of the application is less than 50 pixel units or greater than 600 pixel units.<br>**NOTE**<br>When the **not** operator is used, the media type must be specified. |
| only     | The **only** operator applies the selected style only when the entire expression is matched. It can be used to prevent ambiguity on browsers of earlier versions. The statements that contain both media types and media features produce ambiguity when they are received by some browsers of earlier versions. For example:<br>screen and (min-height: 50)<br>The browsers of earlier versions would mislead this sentence into screen, causing the fact that the specified style is applied when only the media type is matched. In this case, the **only** operator can be used to avoid this problem.<br>**NOTE**<br>When the **only** operator is used, the media type must be specified. |
| ,(comma) | The **or** operator is used to combine multiple media features into one media query, in a logical OR operation. The query is valid if a media feature is true. The effect of a comma operator is equivalent to that of the **or** operator.<br>For example, **screen and (min-height: 1000),  (round-screen: true) ** indicates that the query is valid when the minimum height of the application is 1000 pixel units or the device screen is round.|
E
ester.zhou 已提交
70

E
ester.zhou 已提交
71
At MediaQuery Level 4, range query is imported so that you can use the operators including &lt;=, &gt;=, &lt;, and &gt; besides the max- and min-operators.
E
ester.zhou 已提交
72

E
ester.zhou 已提交
73
**Table 2** Logical operators for range query
E
ester.zhou 已提交
74

E
ester.zhou 已提交
75 76
| Type   | Description                                      |
| ----- | ---------------------------------------- |
E
ester.zhou 已提交
77 78 79 80
| &lt;= | Less than or equal to, for example, **screen and (50 &lt;= height)**.|
| &gt;= | Greater than or equal to, for example, **screen and (600 &gt;= height)**.|
| &lt;  | Less than, for example, **screen and (50 &lt; height)**.|
| &gt;  | Greater than, for example, **screen and (600 &gt; height)**.|
E
ester.zhou 已提交
81 82 83 84


### media-feature

E
ester.zhou 已提交
85 86 87 88 89 90 91 92
| Type               | Description                                      |
| ----------------- | ---------------------------------------- |
| height            | Height of the display area on the application page.                            |
| min-height        | Minimum height of the display area on the application page.                          |
| max-height        | Maximum height of the display area on the application page.                          |
| width             | Width of the display area on the app page.                            |
| min-width         | Minimum width of the display area on the application page.                          |
| max-width         | Maximum width of the display area on the application page.                          |
E
ester.zhou 已提交
93
| resolution        | Resolution of the device. The unit can be dpi, dppx, or dpcm.<br>- **dpi** indicates the number of physical pixels per inch. 1 dpi ≈ 0.39 dpcm.<br>- **dpcm** indicates the number of physical pixels per centimeter. 1 dpcm ≈ 2.54 dpi.<br>- **dppx** indicates the number of physical pixels in each pixel. (This unit is calculated based on this formula: 96 px = 1 inch, which is different from the calculation method of the px unit on the page.) 1 dppx = 96 dpi.|
E
ester.zhou 已提交
94 95
| min-resolution    | Minimum device resolution.                               |
| max-resolution    | Maximum device resolution.                               |
E
ester.zhou 已提交
96
| orientation       | Screen orientation.<br>Options are as follows:<br>- orientation: portrait<br>- orientation: landscape|
E
ester.zhou 已提交
97 98 99 100 101 102 103 104
| device-height     | Height of the device.                                  |
| min-device-height | Minimum height of the device.                                |
| max-device-height | Maximum height of the device.                                |
| device-width      | Width of the device.                                  |
| min-device-width  | Minimum width of the device.                                |
| max-device-width  | Maximum width of the device.                                |
| round-screen      | Screen type. The value **true** means that the screen is round, and **false** means the opposite. |
| dark-mode         | Whether the device is in dark mode. The value **true** means that the device is in dark mode, and **false** means the opposite.                 |
E
ester.zhou 已提交
105 106 107 108 109

## Example Scenario

Use media queries to apply different content and styles to the page text when the screen is switched between landscape and portrait modes.

E
ester.zhou 已提交
110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128
```ts
import mediaquery from '@ohos.mediaquery'

let portraitFunc = null

@Entry
@Component
struct MediaQueryExample {
  @State color: string = '#DB7093'
  @State text: string = 'Portrait'
  listener = mediaquery.matchMediaSync('(orientation: landscape)') // The query is valid when the device is in landscape mode.

  onPortrait(mediaQueryResult) {
    if (mediaQueryResult.matches) {
      this.color = '#FFD700'
      this.text = 'Landscape'
    } else {
      this.color = '#DB7093'
      this.text = 'Portrait'
E
ester.zhou 已提交
129
    }
E
ester.zhou 已提交
130 131 132 133 134 135 136 137 138 139
  }

  aboutToAppear() {
    portraitFunc = this.onPortrait.bind(this) // Bind to the current application instance.
    this.listener.on('change', portraitFunc)
  }

  build() {
    Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Center, justifyContent: FlexAlign.Center }) {
      Text(this.text).fontSize(50).fontColor(this.color)
E
ester.zhou 已提交
140
    }
E
ester.zhou 已提交
141
    .width('100%').height('100%')
E
ester.zhou 已提交
142
  }
E
ester.zhou 已提交
143 144
}
```
E
ester.zhou 已提交
145
When the device is in landscape orientation, the text content is displayed in landscape mode in the color of #FFD700.<br>
E
ester.zhou 已提交
146
![en-us_image_0000001262954829](figures/en-us_image_0000001262954829.png)
E
ester.zhou 已提交
147

E
ester.zhou 已提交
148
When the device is not in landscape orientation, the text content is displayed in portrait mode in the color of #DB7093.<br>
E
ester.zhou 已提交
149
![en-us_image_0000001263074739](figures/en-us_image_0000001263074739.png)