HarmonyOS鸿蒙Next中当Ability设置了auto_rotation_landscape_restricted还能设置为竖屏
HarmonyOS鸿蒙Next中当Ability设置了auto_rotation_landscape_restricted还能设置为竖屏 当Ability设置了auto_rotation_landscape_restricted,还能在Entry中将窗口动态设置为竖屏模式么?
版本为最新版
使用代码控制横竖屏,可以设置主窗口的显示方向属性 setPreferredOrientation。
在EntryAbility中:
// EntryAbility.ets
import { UIAbility } from '@kit.AbilityKit';
import { BusinessError } from '@kit.BasicServicesKit';
export default class EntryAbility extends UIAbility {
// ...
onWindowStageCreate(windowStage: window.WindowStage): void {
let windowClass: window.Window | undefined = undefined;
windowStage.getMainWindow((err: BusinessError, data) => {
const errCode: number = err.code;
if (errCode) {
return;
}
windowClass = data;
let orientation = window.Orientation.AUTO_ROTATION_PORTRAIT;
try {
let promise = windowClass.setPreferredOrientation(orientation);
} catch (exception) {
}
});
}
}
如果需要在某个页面单独控制,可以通过Context在获取到窗口后使用:
window.getLastWindow(this.getUIContext().getHostContext()).then(win => {
this.isFullScreen = !this.isFullScreen;
win.setPreferredOrientation(this.isFullScreen ? window.Orientation.AUTO_ROTATION_LANDSCAPE :
window.Orientation.PORTRAIT)
});
可以参考一下 系统旋转锁定功能对应用不生效。
如果需求是整个应用都是横屏的话。可以直接在 onWindowStageCreate 的时候通过API setPreferredOrientation 设置整个应用为横屏。
示例代码
// EntryAbility.ets
import { UIAbility } from '@kit.AbilityKit';
import { BusinessError } from '@kit.BasicServicesKit';
export default class EntryAbility extends UIAbility {
// ...
onWindowStageCreate(windowStage: window.WindowStage): void {
console.info('onWindowStageCreate');
let windowClass: window.Window | undefined = undefined;
windowStage.getMainWindow((err: BusinessError, data) => {
const errCode: number = err.code;
if (errCode) {
console.error(`Failed to obtain the main window. Cause code: ${err.code}, message: ${err.message}`);
return;
}
windowClass = data;
let orientation = window.Orientation.AUTO_ROTATION_LANDSCAPE;
try {
// 设置为跟随传感器自动横向旋转,可以旋转到横屏、反向横屏,无法旋转到竖屏、反向竖屏,且不受控制中心的旋转开关控制。
windowClass.setPreferredOrientation(orientation, (err: BusinessError) => {
const errCode: number = err.code;
if (errCode) {
console.error(`Failed to set window orientation. Cause code: ${err.code}, message: ${err.message}`);
return;
}
console.info('Succeeded in setting window orientation.');
});
} catch (exception) {
console.error(`Failed to set window orientation. Cause code: ${exception.code}, message: ${exception.message}`);
}
});
}
}
可以使用 window.setPreferredOrientation 接口在运行时设置横竖屏模式,参考:https://developer.huawei.com/consumer/cn/doc/harmonyos-references/arkts-apis-window-window#setpreferredorientation9
let orientation = window.Orientation.PORTRAIT;
try {
windowClass.setPreferredOrientation(orientation, (err: BusinessError) => {
const errCode: number = err.code;
if (errCode) {
console.error(`Failed to set window orientation. Cause code: ${err.code}, message: ${err.message}`);
return;
}
console.info('Succeeded in setting window orientation.');
});
} catch (exception) {
console.error(`Failed to set window orientation. Cause code: ${exception.code}, message: ${exception.message}`);
}
官方还提供了一个横竖屏切换的最佳实践,讲解不同类型应用如何开发横竖屏切换,可以参考阅读:https://developer.huawei.com/consumer/cn/doc/best-practices/bpta-landscape-and-portrait-development
在HarmonyOS Next中,若Ability已设置auto_rotation_landscape_restricted,则无法强制设为竖屏。该配置限制屏幕仅可在横屏方向自动旋转,竖屏方向被锁定禁止。
在HarmonyOS Next中,当Ability的config.json配置文件里设置了"orientation": "auto_rotation_landscape_restricted"时,该Ability的显示方向被限制为“自动旋转,但锁定为横屏方向”。这意味着系统将响应设备的重力感应旋转,但只会允许在横屏(landscape)方向(包括正反两个方向)之间切换,而不会旋转到竖屏(portrait)方向。
关于在Entry中动态设置为竖屏的问题:
结论是:不能。 这个配置项是Ability级别的、在应用启动时由系统解析并强制执行的基础显示策略。它设定了Ability窗口方向行为的“天花板”或“边界”。
- 配置优先:
auto_rotation_landscape_restricted这个配置的优先级高于在UI代码(Entry)中通过window相关API(例如window.getLastWindow(this.context)后设置方向)进行的动态设置。 - 系统限制:系统在创建和管理该Ability的窗口时,会依据此配置进行约束。即使你在UI代码中请求设置为竖屏(
orientation: Orientation.PORTRAIT),系统也会忽略这个请求,因为基础配置已经明确禁止了竖屏方向。 - 行为表现:你的Ability窗口将始终保持横屏状态。当设备物理方向改变时,窗口只会在
landscape(正常横屏)和landscape_inverted(反向横屏)之间自动切换。
如果需要动态切换横竖屏:
你必须修改Ability的基础配置。auto_rotation_landscape_restricted这个值本身不具备动态切换到竖屏的能力。你可以根据业务场景考虑以下配置:
- 使用
auto_rotation:如果希望完全由系统根据重力感应自动在横竖屏之间切换,应将配置改为"orientation": "auto_rotation"。然后你可以在代码中通过windowAPI动态锁定或解锁某个特定方向(例如,在播放视频时锁定横屏)。 - 使用
unspecified:如果希望完全由你的应用代码通过windowAPI来动态控制所有方向,可以将配置设为"orientation": "unspecified"。这样你就获得了完全的控制权,但需要自己处理所有方向逻辑。
总结:
auto_rotation_landscape_restricted 是一个限制性很强的配置,它明确排除了竖屏方向。在此配置下,任何试图在运行时将窗口设置为竖屏的代码操作都将无效。要改变这一行为,必须更改Ability在config.json中的orientation配置项。


