说明
从 API Version 7 开始支持。后续版本如有新增内容,则采用上角标单独标记该内容的起始版本。
mediaquery.matchMediaSync
matchMediaSync(condition: string): MediaQueryListener
设置媒体查询的查询条件,并返回对应的监听句柄。
示例代码
- 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)')
- onPortrait(mediaQueryResult) {
- if (mediaQueryResult.matches) {
- this.color = '#FFD700'
- this.text = 'Landscape'
- } else {
- this.color = '#DB7093'
- this.text = 'Portrait'
- }
- }
- 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.text).fontSize(24).fontColor(this.color)
- }
- .width('100%').height('100%')
- }
- }
复制代码示例效果:
附件: