HarmonyOS鸿蒙Next中如何切换横竖屏?

HarmonyOS鸿蒙Next中如何切换横竖屏? OpenHarmony/HarmonyOS如何切换横竖屏?

本文中,FA模型我用的HarmonyOS3的手机,API版本为8。

Stage模型我用的OpenHarmony3.2,API版本为9。

FA模型

FA模型下,setDisplayOrientation是切换横竖屏的接口。

文档:https://gitee.com/openharmony/docs/blob/master/zh-cn/application-dev/reference/apis/js-apis-inner-app-context.md#contextsetdisplayorientation7

context.setDisplayOrientation7+

setDisplayOrientation(orientation: bundle.DisplayOrientation, callback: AsyncCallback): void

设置当前能力的显示方向(callback形式)。

系统能力:SystemCapability.Ability.AbilityRuntime.Core

参数:

参数名 类型 必填 说明
orientation bundle.DisplayOrientation 指示当前能力的新方向。
callback AsyncCallback 表示屏幕显示方向。

示例:

import featureAbility from '@ohos.ability.featureAbility';
import bundle from '@ohos.bundle';

var context = featureAbility.getContext();
var orientation = bundle.DisplayOrientation.UNSPECIFIED;
context.setDisplayOrientation(orientation, (err) => {
    console.info("setDisplayOrientation err: " + JSON.stringify(err));
});

Stage模型

从API 9开始,可以使用setPreferredOrientation来切换横竖屏。

文档:https://gitee.com/openharmony/docs/blob/master/zh-cn/application-dev/reference/apis/js-apis-window.md#setpreferredorientation9

在Stage模型中,使用到的主要是window,在设置横竖屏切换的时候,需要先使用getLastWindow()createWindow()findWindow()中的任一方法获取到Window实例,再通过此实例调用对应方法。通过获取对应的方法。我在这里使用getLastWindow

window.getLastWindow

getLastWindow(ctx: BaseContext): Promise

获取当前应用内最后显示的窗口,使用Promise异步回调。

系统能力:SystemCapability.WindowManager.WindowManager.Core

参数:

参数名 类型 必填 说明
ctx BaseContext 当前应用上下文信息。FA模型的Context定义见Context。 Stage模型的Context定义见Context

返回值:

类型 说明
Promise<Window> Promise对象。返回当前应用内最后显示的窗口对象。

错误码:

错误码ID 错误信息
1300002 This window state is abnormal.
let windowClass = null;
try {
    let promise = window.getLastWindow(this.context);
    promise.then((data) => {
        windowClass = data;
        console.info('Succeeded in obtaining the top window. Data: ' + JSON.stringify(data));
    }).catch((err) => {
        console.error('Failed to obtain the top window. Cause: ' + JSON.stringify(err));
    });
} catch (exception) {
    console.error('Failed to obtain the top window. Cause: ' + JSON.stringify(exception));
}

setPreferredOrientation

setPreferredOrientation(orientation: Orientation): Promise

设置窗口的显示方向属性,使用Promise异步回调。

系统能力:SystemCapability.WindowManager.WindowManager.Core

参数:

参数名 类型 必填 说明
Orientation Orientation 窗口显示方向的属性。

返回值:

类型 说明
Promise 无返回结果的Promise对象。

错误码:

错误码ID 错误信息
1300002 This window state is abnormal.

示例:

let orientation = window.Orientation.AUTO_ROTATION;
try {
    let promise = windowClass.setPreferredOrientation(orientation);
    promise.then(() => {
        console.info('Succeeded in setting the window orientation.');
    }).catch((err) => {
        console.error('Failed to set the window orientation. Cause: ' + JSON.stringify(err));
    });
} catch (exception) {
    console.error('Failed to set window orientation. Cause: ' + JSON.stringify(exception));
}
``

更多关于HarmonyOS鸿蒙Next中如何切换横竖屏?的实战教程也可以访问 https://www.itying.com/category-93-b0.html

1 回复

更多关于HarmonyOS鸿蒙Next中如何切换横竖屏?的实战系列教程也可以访问 https://www.itying.com/category-93-b0.html


在HarmonyOS鸿蒙Next中,切换横竖屏可以通过以下步骤实现:

  1. 自动旋转:确保设备的自动旋转功能已开启。在设置中找到“显示”或“屏幕”选项,启用“自动旋转屏幕”。
  2. 手动切换:如果自动旋转未开启,可以通过下拉通知栏,找到“屏幕旋转”图标并点击,手动切换横竖屏。
  3. 应用内设置:部分应用支持横竖屏切换,可以在应用内设置或通过应用界面手势进行调整。

确保设备支持横竖屏切换,并根据具体需求选择合适的方式。

回到顶部