From 7ecf19a48778d4028de118a86f1dc2c8e2d58bc9 Mon Sep 17 00:00:00 2001 From: lon9 <815882449@qq.com> Date: Mon, 29 Sep 2025 10:31:32 +0800 Subject: [PATCH 1/3] =?UTF-8?q?Revert=20"=E5=90=8C=E6=BA=90=E5=B7=A5?= =?UTF-8?q?=E7=A8=8B=E6=95=B4=E6=94=B9"?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This reverts commit babd1df4169115e8e6a3fef789a9dff42c055c4c. --- ArkUI/orientationDevelopment/README.md | 44 --------- .../ets/entryability/EntryAbility.ets | 44 +++++++++ .../entrybackupability/EntryBackupAbility.ets | 16 ++++ .../configautorotation/ets/pages/Index.ets | 23 +++++ .../src/main/configautorotation/module.json5 | 64 +++++++++++++ .../ets/entryability/EntryAbility.ets | 44 +++++++++ .../entrybackupability/EntryBackupAbility.ets | 16 ++++ .../configlandscapeauto/ets/pages/Index.ets | 23 +++++ .../src/main/configlandscapeauto/module.json5 | 64 +++++++++++++ .../ets/entryability/EntryAbility.ets | 44 +++++++++ .../entrybackupability/EntryBackupAbility.ets | 16 ++++ .../main/configportrait/ets/pages/Index.ets | 23 +++++ .../src/main/configportrait/module.json5 | 64 +++++++++++++ .../entry/src/main/ets/pages/Index.ets | 1 - .../src/main/ets/pages/MediaQueryPage.ets | 71 +++++++++++++++ .../src/main/ets/pages/OrientationPage.ets | 91 +++++++++++++++++++ .../src/main/ets/pages/ScreenRotationA.ets | 25 +++++ .../src/main/ets/pages/ScreenRotationB.ets | 20 ++++ 18 files changed, 648 insertions(+), 45 deletions(-) delete mode 100644 ArkUI/orientationDevelopment/README.md create mode 100644 ArkUI/orientationDevelopment/entry/src/main/configautorotation/ets/entryability/EntryAbility.ets create mode 100644 ArkUI/orientationDevelopment/entry/src/main/configautorotation/ets/entrybackupability/EntryBackupAbility.ets create mode 100644 ArkUI/orientationDevelopment/entry/src/main/configautorotation/ets/pages/Index.ets create mode 100644 ArkUI/orientationDevelopment/entry/src/main/configautorotation/module.json5 create mode 100644 ArkUI/orientationDevelopment/entry/src/main/configlandscapeauto/ets/entryability/EntryAbility.ets create mode 100644 ArkUI/orientationDevelopment/entry/src/main/configlandscapeauto/ets/entrybackupability/EntryBackupAbility.ets create mode 100644 ArkUI/orientationDevelopment/entry/src/main/configlandscapeauto/ets/pages/Index.ets create mode 100644 ArkUI/orientationDevelopment/entry/src/main/configlandscapeauto/module.json5 create mode 100644 ArkUI/orientationDevelopment/entry/src/main/configportrait/ets/entryability/EntryAbility.ets create mode 100644 ArkUI/orientationDevelopment/entry/src/main/configportrait/ets/entrybackupability/EntryBackupAbility.ets create mode 100644 ArkUI/orientationDevelopment/entry/src/main/configportrait/ets/pages/Index.ets create mode 100644 ArkUI/orientationDevelopment/entry/src/main/configportrait/module.json5 create mode 100644 ArkUI/orientationDevelopment/entry/src/main/ets/pages/MediaQueryPage.ets create mode 100644 ArkUI/orientationDevelopment/entry/src/main/ets/pages/OrientationPage.ets create mode 100644 ArkUI/orientationDevelopment/entry/src/main/ets/pages/ScreenRotationA.ets create mode 100644 ArkUI/orientationDevelopment/entry/src/main/ets/pages/ScreenRotationB.ets diff --git a/ArkUI/orientationDevelopment/README.md b/ArkUI/orientationDevelopment/README.md deleted file mode 100644 index f58c9803..00000000 --- a/ArkUI/orientationDevelopment/README.md +++ /dev/null @@ -1,44 +0,0 @@ -# 多设备功能开发同源代码工程 - -## 介绍 - -本示例为多设备功能开发同源代码工程,包含最佳实践文档中包含的推荐使用方法的样例代码。工程本身不具备实际功能,开发者请直接阅读文档结合源码来理解多设备功能开发。 - -## 效果预览 - -不涉及。 - -## 工程目录 - -``` -entry/src/main/ets -│ ├──entryability -│ │ └──entryability // 应用入口类 -│ ├──entrybackupability -│ │ └──EntryBackupAbility.ets // 应用备份能力类 -│ └──pages -│ └──Index.ets // 首页 -└────entry/src/main/resources -``` - -## 具体实现 - -不涉及。 - -## 相关权限 - -不涉及。 - -## 依赖 - -不涉及。 - -## 约束与限制 - -1.本示例仅支持标准系统上运行,支持设备:华为手机。 - -2.HarmonyOS系统:HarmonyOS 5.0.5 Release及以上。 - -3.DevEco Studio版本:DevEco Studio 5.0.5 Release及以上。 - -4.HarmonyOS SDK版本:HarmonyOS 5.0.5 Release SDK及以上。 \ No newline at end of file diff --git a/ArkUI/orientationDevelopment/entry/src/main/configautorotation/ets/entryability/EntryAbility.ets b/ArkUI/orientationDevelopment/entry/src/main/configautorotation/ets/entryability/EntryAbility.ets new file mode 100644 index 00000000..508880af --- /dev/null +++ b/ArkUI/orientationDevelopment/entry/src/main/configautorotation/ets/entryability/EntryAbility.ets @@ -0,0 +1,44 @@ +import { AbilityConstant, ConfigurationConstant, UIAbility, Want } from '@kit.AbilityKit'; +import { hilog } from '@kit.PerformanceAnalysisKit'; +import { window } from '@kit.ArkUI'; + +const DOMAIN = 0x0000; + +export default class EntryAbility extends UIAbility { + onCreate(want: Want, launchParam: AbilityConstant.LaunchParam): void { + this.context.getApplicationContext().setColorMode(ConfigurationConstant.ColorMode.COLOR_MODE_NOT_SET); + hilog.info(DOMAIN, 'testTag', '%{public}s', 'Ability onCreate'); + } + + onDestroy(): void { + hilog.info(DOMAIN, 'testTag', '%{public}s', 'Ability onDestroy'); + } + + onWindowStageCreate(windowStage: window.WindowStage): void { + // Main window is created, set main page for this ability + hilog.info(DOMAIN, 'testTag', '%{public}s', 'Ability onWindowStageCreate'); + + windowStage.loadContent('pages/Index', (err) => { + if (err.code) { + hilog.error(DOMAIN, 'testTag', 'Failed to load the content. Cause: %{public}s', JSON.stringify(err)); + return; + } + hilog.info(DOMAIN, 'testTag', 'Succeeded in loading the content.'); + }); + } + + onWindowStageDestroy(): void { + // Main window is destroyed, release UI related resources + hilog.info(DOMAIN, 'testTag', '%{public}s', 'Ability onWindowStageDestroy'); + } + + onForeground(): void { + // Ability has brought to foreground + hilog.info(DOMAIN, 'testTag', '%{public}s', 'Ability onForeground'); + } + + onBackground(): void { + // Ability has back to background + hilog.info(DOMAIN, 'testTag', '%{public}s', 'Ability onBackground'); + } +} \ No newline at end of file diff --git a/ArkUI/orientationDevelopment/entry/src/main/configautorotation/ets/entrybackupability/EntryBackupAbility.ets b/ArkUI/orientationDevelopment/entry/src/main/configautorotation/ets/entrybackupability/EntryBackupAbility.ets new file mode 100644 index 00000000..8e4de992 --- /dev/null +++ b/ArkUI/orientationDevelopment/entry/src/main/configautorotation/ets/entrybackupability/EntryBackupAbility.ets @@ -0,0 +1,16 @@ +import { hilog } from '@kit.PerformanceAnalysisKit'; +import { BackupExtensionAbility, BundleVersion } from '@kit.CoreFileKit'; + +const DOMAIN = 0x0000; + +export default class EntryBackupAbility extends BackupExtensionAbility { + async onBackup() { + hilog.info(DOMAIN, 'testTag', 'onBackup ok'); + await Promise.resolve(); + } + + async onRestore(bundleVersion: BundleVersion) { + hilog.info(DOMAIN, 'testTag', 'onRestore ok %{public}s', JSON.stringify(bundleVersion)); + await Promise.resolve(); + } +} \ No newline at end of file diff --git a/ArkUI/orientationDevelopment/entry/src/main/configautorotation/ets/pages/Index.ets b/ArkUI/orientationDevelopment/entry/src/main/configautorotation/ets/pages/Index.ets new file mode 100644 index 00000000..8e2d24ad --- /dev/null +++ b/ArkUI/orientationDevelopment/entry/src/main/configautorotation/ets/pages/Index.ets @@ -0,0 +1,23 @@ +@Entry +@Component +struct Index { + @State message: string = 'Hello World'; + + build() { + RelativeContainer() { + Text(this.message) + .id('HelloWorld') + .fontSize($r('app.float.page_text_font_size')) + .fontWeight(FontWeight.Bold) + .alignRules({ + center: { anchor: '__container__', align: VerticalAlign.Center }, + middle: { anchor: '__container__', align: HorizontalAlign.Center } + }) + .onClick(() => { + this.message = 'Welcome'; + }) + } + .height('100%') + .width('100%') + } +} \ No newline at end of file diff --git a/ArkUI/orientationDevelopment/entry/src/main/configautorotation/module.json5 b/ArkUI/orientationDevelopment/entry/src/main/configautorotation/module.json5 new file mode 100644 index 00000000..01b6011d --- /dev/null +++ b/ArkUI/orientationDevelopment/entry/src/main/configautorotation/module.json5 @@ -0,0 +1,64 @@ +// [Start module] +{ + "module": { + // [StartExclude module] + "name": "entry", + "type": "entry", + "description": "$string:module_desc", + "mainElement": "EntryAbility", + "deviceTypes": [ + "phone", + "tablet", + "2in1", + "wearable" + ], + "deliveryWithInstall": true, + "installationFree": false, + "pages": "$profile:main_pages", + // [EndExclude module] + "abilities": [ + { + "name": "EntryAbility", + // [StartExclude module] + "srcEntry": "./ets/entryability/EntryAbility.ets", + "description": "$string:EntryAbility_desc", + "icon": "$media:layered_image", + "label": "$string:EntryAbility_label", + "startWindowIcon": "$media:startIcon", + // [EndExclude module] + "orientation": "auto_rotation", + // [StartExclude module] + "startWindowBackground": "$color:start_window_background", + "exported": true, + "skills": [ + { + "entities": [ + "entity.system.home" + ], + "actions": [ + "action.system.home" + ] + } + ], + // [EndExclude module] + } + ], + // [StartExclude module] + "extensionAbilities": [ + { + "name": "EntryBackupAbility", + "srcEntry": "./ets/entrybackupability/EntryBackupAbility.ets", + "type": "backup", + "exported": false, + "metadata": [ + { + "name": "ohos.extension.backup", + "resource": "$profile:backup_config" + } + ], + } + ], + // [EndExclude module] + } +} +// [End module] \ No newline at end of file diff --git a/ArkUI/orientationDevelopment/entry/src/main/configlandscapeauto/ets/entryability/EntryAbility.ets b/ArkUI/orientationDevelopment/entry/src/main/configlandscapeauto/ets/entryability/EntryAbility.ets new file mode 100644 index 00000000..508880af --- /dev/null +++ b/ArkUI/orientationDevelopment/entry/src/main/configlandscapeauto/ets/entryability/EntryAbility.ets @@ -0,0 +1,44 @@ +import { AbilityConstant, ConfigurationConstant, UIAbility, Want } from '@kit.AbilityKit'; +import { hilog } from '@kit.PerformanceAnalysisKit'; +import { window } from '@kit.ArkUI'; + +const DOMAIN = 0x0000; + +export default class EntryAbility extends UIAbility { + onCreate(want: Want, launchParam: AbilityConstant.LaunchParam): void { + this.context.getApplicationContext().setColorMode(ConfigurationConstant.ColorMode.COLOR_MODE_NOT_SET); + hilog.info(DOMAIN, 'testTag', '%{public}s', 'Ability onCreate'); + } + + onDestroy(): void { + hilog.info(DOMAIN, 'testTag', '%{public}s', 'Ability onDestroy'); + } + + onWindowStageCreate(windowStage: window.WindowStage): void { + // Main window is created, set main page for this ability + hilog.info(DOMAIN, 'testTag', '%{public}s', 'Ability onWindowStageCreate'); + + windowStage.loadContent('pages/Index', (err) => { + if (err.code) { + hilog.error(DOMAIN, 'testTag', 'Failed to load the content. Cause: %{public}s', JSON.stringify(err)); + return; + } + hilog.info(DOMAIN, 'testTag', 'Succeeded in loading the content.'); + }); + } + + onWindowStageDestroy(): void { + // Main window is destroyed, release UI related resources + hilog.info(DOMAIN, 'testTag', '%{public}s', 'Ability onWindowStageDestroy'); + } + + onForeground(): void { + // Ability has brought to foreground + hilog.info(DOMAIN, 'testTag', '%{public}s', 'Ability onForeground'); + } + + onBackground(): void { + // Ability has back to background + hilog.info(DOMAIN, 'testTag', '%{public}s', 'Ability onBackground'); + } +} \ No newline at end of file diff --git a/ArkUI/orientationDevelopment/entry/src/main/configlandscapeauto/ets/entrybackupability/EntryBackupAbility.ets b/ArkUI/orientationDevelopment/entry/src/main/configlandscapeauto/ets/entrybackupability/EntryBackupAbility.ets new file mode 100644 index 00000000..8e4de992 --- /dev/null +++ b/ArkUI/orientationDevelopment/entry/src/main/configlandscapeauto/ets/entrybackupability/EntryBackupAbility.ets @@ -0,0 +1,16 @@ +import { hilog } from '@kit.PerformanceAnalysisKit'; +import { BackupExtensionAbility, BundleVersion } from '@kit.CoreFileKit'; + +const DOMAIN = 0x0000; + +export default class EntryBackupAbility extends BackupExtensionAbility { + async onBackup() { + hilog.info(DOMAIN, 'testTag', 'onBackup ok'); + await Promise.resolve(); + } + + async onRestore(bundleVersion: BundleVersion) { + hilog.info(DOMAIN, 'testTag', 'onRestore ok %{public}s', JSON.stringify(bundleVersion)); + await Promise.resolve(); + } +} \ No newline at end of file diff --git a/ArkUI/orientationDevelopment/entry/src/main/configlandscapeauto/ets/pages/Index.ets b/ArkUI/orientationDevelopment/entry/src/main/configlandscapeauto/ets/pages/Index.ets new file mode 100644 index 00000000..8e2d24ad --- /dev/null +++ b/ArkUI/orientationDevelopment/entry/src/main/configlandscapeauto/ets/pages/Index.ets @@ -0,0 +1,23 @@ +@Entry +@Component +struct Index { + @State message: string = 'Hello World'; + + build() { + RelativeContainer() { + Text(this.message) + .id('HelloWorld') + .fontSize($r('app.float.page_text_font_size')) + .fontWeight(FontWeight.Bold) + .alignRules({ + center: { anchor: '__container__', align: VerticalAlign.Center }, + middle: { anchor: '__container__', align: HorizontalAlign.Center } + }) + .onClick(() => { + this.message = 'Welcome'; + }) + } + .height('100%') + .width('100%') + } +} \ No newline at end of file diff --git a/ArkUI/orientationDevelopment/entry/src/main/configlandscapeauto/module.json5 b/ArkUI/orientationDevelopment/entry/src/main/configlandscapeauto/module.json5 new file mode 100644 index 00000000..f45f9d02 --- /dev/null +++ b/ArkUI/orientationDevelopment/entry/src/main/configlandscapeauto/module.json5 @@ -0,0 +1,64 @@ +// [Start module] +{ + "module": { + // [StartExclude module] + "name": "entry", + "type": "entry", + "description": "$string:module_desc", + "mainElement": "EntryAbility", + "deviceTypes": [ + "phone", + "tablet", + "2in1", + "wearable" + ], + "deliveryWithInstall": true, + "installationFree": false, + "pages": "$profile:main_pages", + // [EndExclude module] + "abilities": [ + { + "name": "EntryAbility", + // [StartExclude module] + "srcEntry": "./ets/entryability/EntryAbility.ets", + "description": "$string:EntryAbility_desc", + "icon": "$media:layered_image", + "label": "$string:EntryAbility_label", + "startWindowIcon": "$media:startIcon", + // [EndExclude module] + "preferMultiWindowOrientation": "landscape_auto", + // [StartExclude module] + "startWindowBackground": "$color:start_window_background", + "exported": true, + "skills": [ + { + "entities": [ + "entity.system.home" + ], + "actions": [ + "action.system.home" + ] + } + ], + // [EndExclude module] + } + ], + // [StartExclude module] + "extensionAbilities": [ + { + "name": "EntryBackupAbility", + "srcEntry": "./ets/entrybackupability/EntryBackupAbility.ets", + "type": "backup", + "exported": false, + "metadata": [ + { + "name": "ohos.extension.backup", + "resource": "$profile:backup_config" + } + ], + } + ], + // [EndExclude module] + } +} +// [End module] \ No newline at end of file diff --git a/ArkUI/orientationDevelopment/entry/src/main/configportrait/ets/entryability/EntryAbility.ets b/ArkUI/orientationDevelopment/entry/src/main/configportrait/ets/entryability/EntryAbility.ets new file mode 100644 index 00000000..508880af --- /dev/null +++ b/ArkUI/orientationDevelopment/entry/src/main/configportrait/ets/entryability/EntryAbility.ets @@ -0,0 +1,44 @@ +import { AbilityConstant, ConfigurationConstant, UIAbility, Want } from '@kit.AbilityKit'; +import { hilog } from '@kit.PerformanceAnalysisKit'; +import { window } from '@kit.ArkUI'; + +const DOMAIN = 0x0000; + +export default class EntryAbility extends UIAbility { + onCreate(want: Want, launchParam: AbilityConstant.LaunchParam): void { + this.context.getApplicationContext().setColorMode(ConfigurationConstant.ColorMode.COLOR_MODE_NOT_SET); + hilog.info(DOMAIN, 'testTag', '%{public}s', 'Ability onCreate'); + } + + onDestroy(): void { + hilog.info(DOMAIN, 'testTag', '%{public}s', 'Ability onDestroy'); + } + + onWindowStageCreate(windowStage: window.WindowStage): void { + // Main window is created, set main page for this ability + hilog.info(DOMAIN, 'testTag', '%{public}s', 'Ability onWindowStageCreate'); + + windowStage.loadContent('pages/Index', (err) => { + if (err.code) { + hilog.error(DOMAIN, 'testTag', 'Failed to load the content. Cause: %{public}s', JSON.stringify(err)); + return; + } + hilog.info(DOMAIN, 'testTag', 'Succeeded in loading the content.'); + }); + } + + onWindowStageDestroy(): void { + // Main window is destroyed, release UI related resources + hilog.info(DOMAIN, 'testTag', '%{public}s', 'Ability onWindowStageDestroy'); + } + + onForeground(): void { + // Ability has brought to foreground + hilog.info(DOMAIN, 'testTag', '%{public}s', 'Ability onForeground'); + } + + onBackground(): void { + // Ability has back to background + hilog.info(DOMAIN, 'testTag', '%{public}s', 'Ability onBackground'); + } +} \ No newline at end of file diff --git a/ArkUI/orientationDevelopment/entry/src/main/configportrait/ets/entrybackupability/EntryBackupAbility.ets b/ArkUI/orientationDevelopment/entry/src/main/configportrait/ets/entrybackupability/EntryBackupAbility.ets new file mode 100644 index 00000000..8e4de992 --- /dev/null +++ b/ArkUI/orientationDevelopment/entry/src/main/configportrait/ets/entrybackupability/EntryBackupAbility.ets @@ -0,0 +1,16 @@ +import { hilog } from '@kit.PerformanceAnalysisKit'; +import { BackupExtensionAbility, BundleVersion } from '@kit.CoreFileKit'; + +const DOMAIN = 0x0000; + +export default class EntryBackupAbility extends BackupExtensionAbility { + async onBackup() { + hilog.info(DOMAIN, 'testTag', 'onBackup ok'); + await Promise.resolve(); + } + + async onRestore(bundleVersion: BundleVersion) { + hilog.info(DOMAIN, 'testTag', 'onRestore ok %{public}s', JSON.stringify(bundleVersion)); + await Promise.resolve(); + } +} \ No newline at end of file diff --git a/ArkUI/orientationDevelopment/entry/src/main/configportrait/ets/pages/Index.ets b/ArkUI/orientationDevelopment/entry/src/main/configportrait/ets/pages/Index.ets new file mode 100644 index 00000000..8e2d24ad --- /dev/null +++ b/ArkUI/orientationDevelopment/entry/src/main/configportrait/ets/pages/Index.ets @@ -0,0 +1,23 @@ +@Entry +@Component +struct Index { + @State message: string = 'Hello World'; + + build() { + RelativeContainer() { + Text(this.message) + .id('HelloWorld') + .fontSize($r('app.float.page_text_font_size')) + .fontWeight(FontWeight.Bold) + .alignRules({ + center: { anchor: '__container__', align: VerticalAlign.Center }, + middle: { anchor: '__container__', align: HorizontalAlign.Center } + }) + .onClick(() => { + this.message = 'Welcome'; + }) + } + .height('100%') + .width('100%') + } +} \ No newline at end of file diff --git a/ArkUI/orientationDevelopment/entry/src/main/configportrait/module.json5 b/ArkUI/orientationDevelopment/entry/src/main/configportrait/module.json5 new file mode 100644 index 00000000..e53c3302 --- /dev/null +++ b/ArkUI/orientationDevelopment/entry/src/main/configportrait/module.json5 @@ -0,0 +1,64 @@ +// [Start module] +{ + "module": { + // [StartExclude module] + "name": "entry", + "type": "entry", + "description": "$string:module_desc", + "mainElement": "EntryAbility", + "deviceTypes": [ + "phone", + "tablet", + "2in1", + "wearable" + ], + "deliveryWithInstall": true, + "installationFree": false, + "pages": "$profile:main_pages", + // [EndExclude module] + "abilities": [ + { + "name": "EntryAbility", + // [StartExclude module] + "srcEntry": "./ets/entryability/EntryAbility.ets", + "description": "$string:EntryAbility_desc", + "icon": "$media:layered_image", + "label": "$string:EntryAbility_label", + "startWindowIcon": "$media:startIcon", + // [EndExclude module] + "orientation": "portrait", + // [StartExclude module] + "startWindowBackground": "$color:start_window_background", + "exported": true, + "skills": [ + { + "entities": [ + "entity.system.home" + ], + "actions": [ + "action.system.home" + ] + } + ], + // [EndExclude module] + } + ], + // [StartExclude module] + "extensionAbilities": [ + { + "name": "EntryBackupAbility", + "srcEntry": "./ets/entrybackupability/EntryBackupAbility.ets", + "type": "backup", + "exported": false, + "metadata": [ + { + "name": "ohos.extension.backup", + "resource": "$profile:backup_config" + } + ], + } + ], + // [EndExclude module] + } +} +// [End module] \ No newline at end of file diff --git a/ArkUI/orientationDevelopment/entry/src/main/ets/pages/Index.ets b/ArkUI/orientationDevelopment/entry/src/main/ets/pages/Index.ets index 5c9b16d9..1c7a6e8a 100644 --- a/ArkUI/orientationDevelopment/entry/src/main/ets/pages/Index.ets +++ b/ArkUI/orientationDevelopment/entry/src/main/ets/pages/Index.ets @@ -1,6 +1,5 @@ // [Start enable_nfc] import { nfcController } from '@kit.ConnectivityKit'; -import { hilog } from '@kit.PerformanceAnalysisKit'; @Entry @Component diff --git a/ArkUI/orientationDevelopment/entry/src/main/ets/pages/MediaQueryPage.ets b/ArkUI/orientationDevelopment/entry/src/main/ets/pages/MediaQueryPage.ets new file mode 100644 index 00000000..c71a990e --- /dev/null +++ b/ArkUI/orientationDevelopment/entry/src/main/ets/pages/MediaQueryPage.ets @@ -0,0 +1,71 @@ +import { mediaquery, window } from '@kit.ArkUI'; +import { common } from '@kit.AbilityKit'; + +// [Start Media] +@Entry +@Component +struct MediaQueryPage { + @State color: string = '#DB7093'; + @State text: string = 'Portrait'; + // The condition is true when the device is in landscape orientation + listener: mediaquery.MediaQueryListener = + this.getUIContext().getMediaQuery().matchMediaSync('(orientation: landscape)'); + // When the media query conditions are met, a callback is triggered + onPortrait(mediaQueryResult: mediaquery.MediaQueryResult) { + if (mediaQueryResult.matches as boolean) { + // If the device is in landscape orientation, change the corresponding page layout + this.color = '#FFD700'; + this.text = 'Landscape'; + } else { + this.color = '#DB7093'; + this.text = 'Portrait'; + } + } + aboutToAppear() { + // Bind the current application instance + // Bind a callback function + this.listener.on('change', (mediaQueryResult: mediaquery.MediaQueryResult) => { + this.onPortrait(mediaQueryResult) + }); + } + aboutToDisappear() { + // Unbind the callback function registered in the listener + this.listener.off('change'); + } + + // Change the horizontal and vertical screen status functions of the device + private changeOrientation(isLandscape: boolean) { + // Obtain the context of the UIAbility instance + let context: common.UIAbilityContext = this.getUIContext().getHostContext() as common.UIAbilityContext; + // Call this API to manually change the horizontal and vertical screen status of the device + 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%') + } +} + +// [End Media] diff --git a/ArkUI/orientationDevelopment/entry/src/main/ets/pages/OrientationPage.ets b/ArkUI/orientationDevelopment/entry/src/main/ets/pages/OrientationPage.ets new file mode 100644 index 00000000..72e0ed10 --- /dev/null +++ b/ArkUI/orientationDevelopment/entry/src/main/ets/pages/OrientationPage.ets @@ -0,0 +1,91 @@ +// [Start orientation] +import { display, window } from '@kit.ArkUI'; + +const TAG = 'foo'; +const ORIENTATION: Array = ['垂直', '平', '反向垂直', '反向水平']; + +@Entry +@Component +struct OrientationPage { + @State rotation: number = 0; + @State message: string = ORIENTATION[this.rotation]; + @Watch('setWindowLayOut') @State isLandscape: boolean = false; + + aboutToAppear() { + this.setOrientation(1); + let callback = async () => { + let getDefaultDisplay = display.getDefaultDisplaySync(); + this.rotation = getDefaultDisplay.rotation; + this.message = ORIENTATION[this.rotation]; + }; + try { + display.on('change', callback); + } catch (exception) { + hilog.error(0x0000, TAG, 'Failed to register callback. Code: ' + JSON.stringify(exception)); + } + } + + setOrientation(type: number) { + try { + window.getLastWindow(getContext(this), (err, data) => { + //获取window实例 + if (err.code) { + hilog.error(0x0000, TAG, 'Failed to obtain the top window. Cause: ' + JSON.stringify(err)); + return; + } + let windowClass = data; + hilog.info(0x0000, TAG, 'Succeeded in obtaining the top window. Data: ' + JSON.stringify(data)); + let orientation: number; + if (type === 1) { + orientation = window.Orientation.AUTO_ROTATION; + } else { + orientation = window.Orientation.UNSPECIFIED; + } + try { + windowClass.setPreferredOrientation(orientation, (err) => { + if (err.code) { + hilog.error(0x0000, TAG, 'Failed to set window orientation. Cause: ' + JSON.stringify(err)); + return; + } + hilog.info(0x0000, 'Succeeded in setting window orientation.'); + }); + } catch (exception) { + hilog.error(0x0000, TAG, 'Failed to set window orientation. Cause: ' + JSON.stringify(exception)); + } + }); + } catch (exception) { + hilog.error(0x0000, TAG, 'Failed to obtain the top window. Cause: ' + JSON.stringify(exception)); + } + } + + setWindowLayOut() { + window.getLastWindow(getContext(this)).then((windowClass) => { + if (this.isLandscape) { + hilog.info(0x0000, 'OrientationPage', '设置屏幕横屏'); + windowClass.setPreferredOrientation(window.Orientation.AUTO_ROTATION_LANDSCAPE); + } else { + hilog.info(0x0000, 'OrientationPage', '设置屏幕竖屏'); + windowClass.setPreferredOrientation(window.Orientation.AUTO_ROTATION_PORTRAIT); + } + }); + } + + build() { + Row() { + Column() { + Text(`${this.rotation}`) + .fontSize(25) + Text(`${this.message}`) + .fontSize(25) + Button('全屏') + .width(140) + .onClick(() => { + this.isLandscape = !this.isLandscape; + }) + } + .width('100%') + } + .height('100%') + } +} +// [End orientation] diff --git a/ArkUI/orientationDevelopment/entry/src/main/ets/pages/ScreenRotationA.ets b/ArkUI/orientationDevelopment/entry/src/main/ets/pages/ScreenRotationA.ets new file mode 100644 index 00000000..6d7948a9 --- /dev/null +++ b/ArkUI/orientationDevelopment/entry/src/main/ets/pages/ScreenRotationA.ets @@ -0,0 +1,25 @@ +import { window } from '@kit.ArkUI'; + +@Component +struct ScreenRotationA { + // [Start orientation] + @StorageLink('mainWindow') mainWindow?: window.Window = undefined; + public lastOrientation?: window.Orientation; + + aboutToAppear(): void { + if (this.mainWindow === undefined) { + return; + } + this.lastOrientation = this.mainWindow!.getPreferredOrientation(); + this.mainWindow!.setPreferredOrientation(window.Orientation.LANDSCAPE); + } + + aboutToDisappear(): void { + this.mainWindow!.setPreferredOrientation(this.lastOrientation) + } + // [End orientation] + + build() { + Text('你好') + } +} diff --git a/ArkUI/orientationDevelopment/entry/src/main/ets/pages/ScreenRotationB.ets b/ArkUI/orientationDevelopment/entry/src/main/ets/pages/ScreenRotationB.ets new file mode 100644 index 00000000..80b24fa4 --- /dev/null +++ b/ArkUI/orientationDevelopment/entry/src/main/ets/pages/ScreenRotationB.ets @@ -0,0 +1,20 @@ +import { common } from '@kit.AbilityKit'; + +@Component +struct ScreenRotationB { + // [Start window] + private windowClass = (this.getUIContext().getHostContext() as common.UIAbilityContext).windowStage.getMainWindowSync() + + aboutToAppear(): void { + this.windowClass.enableLandscapeMultiWindow(); + } + + aboutToDisappear(): void { + this.windowClass.disableLandscapeMultiWindow(); + } + // [End window] + + build() { + Text('你好') + } +} -- Gitee From 513bb111a0ad9e1bda70c3fb9f54a6b605d6ff28 Mon Sep 17 00:00:00 2001 From: lon9 <815882449@qq.com> Date: Mon, 29 Sep 2025 10:38:57 +0800 Subject: [PATCH 2/3] =?UTF-8?q?=E5=90=8C=E6=BA=90=E5=B7=A5=E7=A8=8B?= =?UTF-8?q?=E6=95=B4=E6=94=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ArkUI/orientationDevelopment/README.md | 42 ++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) create mode 100644 ArkUI/orientationDevelopment/README.md diff --git a/ArkUI/orientationDevelopment/README.md b/ArkUI/orientationDevelopment/README.md new file mode 100644 index 00000000..8d815134 --- /dev/null +++ b/ArkUI/orientationDevelopment/README.md @@ -0,0 +1,42 @@ +# 多设备同源代码工程 + +## 介绍 + +本示例为窗口方向与多设备功能开发同源代码工程,包含最佳实践文档中包含的推荐使用方法的样例代码。工程本身不具备实际功能,开发者请直接阅读文档结合源码来理解多设备功能开发。 + +## 效果预览 + +不涉及。 + +## 工程目录 + +``` +entry/src/main/ +├──configautorotation // 窗口自动旋转 +├──configlandscapeauto // 窗口横屏 +├──configportrait // 窗口竖屏 +├──ets // 主目录 +└──resources // 资源目录 +``` + +## 具体实现 + +不涉及。 + +## 相关权限 + +不涉及。 + +## 依赖 + +不涉及。 + +## 约束与限制 + +1.本示例仅支持标准系统上运行,支持设备:华为手机。 + +2.HarmonyOS系统:HarmonyOS 5.0.5 Release及以上。 + +3.DevEco Studio版本:DevEco Studio 5.0.5 Release及以上。 + +4.HarmonyOS SDK版本:HarmonyOS 5.0.5 Release SDK及以上。 \ No newline at end of file -- Gitee From e6218bc948da8e8b8fb553d7bc6e2b528be05c6a Mon Sep 17 00:00:00 2001 From: lon9 <815882449@qq.com> Date: Mon, 29 Sep 2025 10:39:15 +0800 Subject: [PATCH 3/3] =?UTF-8?q?=E5=90=8C=E6=BA=90=E5=B7=A5=E7=A8=8B?= =?UTF-8?q?=E6=95=B4=E6=94=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ArkUI/orientationDevelopment/entry/src/main/ets/pages/Index.ets | 1 + 1 file changed, 1 insertion(+) diff --git a/ArkUI/orientationDevelopment/entry/src/main/ets/pages/Index.ets b/ArkUI/orientationDevelopment/entry/src/main/ets/pages/Index.ets index 1c7a6e8a..5c9b16d9 100644 --- a/ArkUI/orientationDevelopment/entry/src/main/ets/pages/Index.ets +++ b/ArkUI/orientationDevelopment/entry/src/main/ets/pages/Index.ets @@ -1,5 +1,6 @@ // [Start enable_nfc] import { nfcController } from '@kit.ConnectivityKit'; +import { hilog } from '@kit.PerformanceAnalysisKit'; @Entry @Component -- Gitee