W3Cschool
恭喜您成為首批注冊用戶
獲得88經(jīng)驗值獎勵
媒體查詢作為響應式設計的核心,在移動設備上應用十分廣泛。媒體查詢可根據(jù)不同設備類型或同設備不同狀態(tài)修改應用的樣式。媒體查詢常用于下面兩種場景:
媒體查詢通過mediaquery模塊接口,設置查詢條件并綁定回調函數(shù),在對應的條件的回調函數(shù)里更改頁面布局或者實現(xiàn)業(yè)務邏輯,實現(xiàn)頁面的響應式設計。具體步驟如下:
首先導入媒體查詢模塊。
- import mediaquery from '@ohos.mediaquery';
通過matchMediaSync接口設置媒體查詢條件,保存返回的條件監(jiān)聽句柄listener。例如監(jiān)聽橫屏事件:
- let listener = mediaquery.matchMediaSync('(orientation: landscape)');
給條件監(jiān)聽句柄listener綁定回調函數(shù)onPortrait,當listener檢測設備狀態(tài)變化時執(zhí)行回調函數(shù)。在回調函數(shù)內,根據(jù)不同設備狀態(tài)更改頁面布局或者實現(xiàn)業(yè)務邏輯。
- onPortrait(mediaQueryResult) {
- if (mediaQueryResult.matches) {
- // do something here
- } else {
- // do something here
- }
- }
- listener.on('change', onPortrait);
語法規(guī)則包括媒體類型(media-type)、媒體邏輯操作(media-logic-operations)和媒體特征(media-feature)。
- [media-type] [media-logic-operations] [(media-feature)]
例如:
媒體邏輯操作符:and、or、not、only用于構成復雜媒體查詢,也可以通過comma(, )將其組合起來,詳細解釋說明如下表。
類型 | 說明 |
---|---|
and | 將多個媒體特征(Media Feature)以“與”的方式連接成一個媒體查詢,只有當所有媒體特征都為true,查詢條件成立。另外,它還可以將媒體類型和媒體功能結合起來。例如:screen and (device-type: wearable) and (max-height: 600) 表示當設備類型是智能穿戴且應用的最大高度小于等于600個像素單位時成立。 |
or | 將多個媒體特征以“或”的方式連接成一個媒體查詢,如果存在結果為true的媒體特征,則查詢條件成立。例如:screen and (max-height: 1000) or (round-screen: true) 表示當應用高度小于等于1000個像素單位或者設備屏幕是圓形時,條件成立。 |
not | 取反媒體查詢結果,媒體查詢結果不成立時返回true,否則返回false。例如:not screen and (min-height: 50) and (max-height: 600) 表示當應用高度小于50個像素單位或者大于600個像素單位時成立。 使用not運算符時必須指定媒體類型。 |
only | 當整個表達式都匹配時,才會應用選擇的樣式,可以應用在防止某些較早的版本的瀏覽器上產(chǎn)生歧義的場景。一些較早版本的瀏覽器對于同時包含了媒體類型和媒體特征的語句會產(chǎn)生歧義,比如:screen and (min-height: 50)。老版本瀏覽器會將這句話理解成screen,從而導致僅僅匹配到媒體類型(screen),就應用了指定樣式,使用only可以很好地規(guī)避這種情況。 使用only時必須指定媒體類型。 |
comma(, ) | 將多個媒體特征以“或”的方式連接成一個媒體查詢,如果存在結果為true的媒體特征,則查詢條件成立。其效果等同于or運算符。例如:screen and (min-height: 1000), (round-screen: true) 表示當應用高度大于等于1000個像素單位或者設備屏幕是圓形時,條件成立。 |
媒體范圍操作符包括<=,>=,<,>,詳細解釋說明如下表。
類型 | 說明 |
---|---|
<= | 小于等于,例如:screen and (height <= 50)。 |
>= | 大于等于,例如:screen and (height >= 600)。 |
< | 小于,例如:screen and (height < 50)。 |
> | 大于,例如:screen and (height > 600)。 |
媒體特征包括應用顯示區(qū)域的寬高、設備分辨率以及設備的寬高等屬性,詳細說明如下表。
類型 | 說明 |
---|---|
height | 應用頁面可繪制區(qū)域的高度。 |
min-height | 應用頁面可繪制區(qū)域的最小高度。 |
max-height | 應用頁面可繪制區(qū)域的最大高度。 |
width | 應用頁面可繪制區(qū)域的寬度。 |
min-width | 應用頁面可繪制區(qū)域的最小寬度。 |
max-width | 應用頁面可繪制區(qū)域的最大寬度。 |
resolution | 設備的分辨率,支持dpi,dppx和dpcm單位。其中: - dpi表示每英寸中物理像素個數(shù),1dpi ≈ 0.39dpcm; - dpcm表示每厘米上的物理像素個數(shù),1dpcm ≈ 2.54dpi; - dppx表示每個px中的物理像素數(shù)(此單位按96px = 1英寸為基準,與頁面中的px單位計算方式不同),1dppx = 96dpi。 |
min-resolution | 設備的最小分辨率。 |
max-resolution | 設備的最大分辨率。 |
orientation | 屏幕的方向。 可選值: - orientation: portrait(設備豎屏); - orientation: landscape(設備橫屏)。 |
device-height | 設備的高度。 |
min-device-height | 設備的最小高度。 |
max-device-height | 設備的最大高度。 |
device-width | 設備的寬度。 |
device-type | 設備的類型。 可選值:default、tablet。 |
min-device-width | 設備的最小寬度。 |
max-device-width | 設備的最大寬度。 |
round-screen | 屏幕類型,圓形屏幕為true,非圓形屏幕為false。 |
dark-mode | 系統(tǒng)為深色模式時為true,否則為false。 |
下例中使用媒體查詢,實現(xiàn)屏幕橫豎屏切換時,給頁面文本應用添加不同的內容和樣式。
Stage模型下的示例:
- import mediaquery from '@ohos.mediaquery';
- import window from '@ohos.window';
- import common from '@ohos.app.ability.common';
- let portraitFunc = null;
- @Entry
- @Component
- struct MediaQueryExample {
- @State color: string = '#DB7093';
- @State text: string = 'Portrait';
- // 當設備橫屏時條件成立
- listener = mediaquery.matchMediaSync('(orientation: landscape)');
- // 當滿足媒體查詢條件時,觸發(fā)回調
- onPortrait(mediaQueryResult) {
- if (mediaQueryResult.matches) { // 若設備為橫屏狀態(tài),更改相應的頁面布局
- this.color = '#FFD700';
- this.text = 'Landscape';
- } else {
- this.color = '#DB7093';
- this.text = 'Portrait';
- }
- }
- aboutToAppear() {
- // 綁定當前應用實例
- portraitFunc = this.onPortrait.bind(this);
- // 綁定回調函數(shù)
- this.listener.on('change', portraitFunc);
- }
- // 改變設備橫豎屏狀態(tài)函數(shù)
- private changeOrientation(isLandscape: boolean) {
- // 獲取UIAbility實例的上下文信息
- let context = getContext(this) as common.UIAbilityContext;
- // 調用該接口手動改變設備橫豎屏狀態(tài)
- window.getLastWindow(context).then((lastWindow) => {
- lastWindow.setPreferredOrientation(isLandscape ? window.Orientation.LANDSCAPE : window.Orientation.PORTRAIT)
- });
- }
- build() {
- Column({ space: 50 }) {
- Text(this.text).fontSize(50).fontColor(this.color)
- Text('Landscape').fontSize(50).fontColor(this.color).backgroundColor(Color.Orange)
- .onClick(() => {
- this.changeOrientation(true);
- })
- Text('Portrait').fontSize(50).fontColor(this.color).backgroundColor(Color.Orange)
- .onClick(() => {
- this.changeOrientation(false);
- })
- }
- .width('100%').height('100%')
- }
- }
FA模型下的示例:
- import mediaquery from '@ohos.mediaquery';
- import featureAbility from '@ohos.ability.featureAbility';
- let portraitFunc = null;
- @Entry
- @Component
- struct MediaQueryExample {
- @State color: string = '#DB7093';
- @State text: string = 'Portrait';
- listener = mediaquery.matchMediaSync('(orientation: landscape)'); // 當設備橫屏時條件成立
- onPortrait(mediaQueryResult) { // 當滿足媒體查詢條件時,觸發(fā)回調
- if (mediaQueryResult.matches) { // 若設備為橫屏狀態(tài),更改相應的頁面布局
- this.color = '#FFD700';
- this.text = 'Landscape';
- } else {
- this.color = '#DB7093';
- this.text = 'Portrait';
- }
- }
- aboutToAppear() {
- portraitFunc = this.onPortrait.bind(this); // 綁定當前應用實例
- this.listener.on('change', portraitFunc); //綁定回調函數(shù)
- }
- build() {
- Column({ space: 50 }) {
- Text(this.text).fontSize(50).fontColor(this.color)
- Text('Landscape').fontSize(50).fontColor(this.color).backgroundColor(Color.Orange)
- .onClick(() => {
- let context = featureAbility.getContext();
- context.setDisplayOrientation(0); //調用該接口手動改變設備橫豎屏狀態(tài)
- })
- Text('Portrait').fontSize(50).fontColor(this.color).backgroundColor(Color.Orange)
- .onClick(() => {
- let context = featureAbility.getContext();
- context.setDisplayOrientation(1); //調用該接口手動改變設備橫豎屏狀態(tài)
- })
- }
- .width('100%').height('100%')
- }
- }
Copyright©2021 w3cschool編程獅|閩ICP備15016281號-3|閩公網(wǎng)安備35020302033924號
違法和不良信息舉報電話:173-0602-2364|舉報郵箱:jubao@eeedong.com
掃描二維碼
下載編程獅App
編程獅公眾號
聯(lián)系方式:
更多建議: