HarmonyOS鸿蒙Next中stage模型中,横屏设置,以及其他配置参数详解

HarmonyOS鸿蒙Next中stage模型中,横屏设置,以及其他配置参数详解

坚果,润开鸿技术专家,InfoQ签约作者,OpenHarmony布道师,多个平台的专家博主。 主页:https://ost.51cto.com/person/posts/14830231

stage模型中,屏幕旋转须在abilities标签中配置orientation, 如果想设置默认为横屏,只需要在module.json5中配置字段为 “orientation”: “landscape”,就可以,下面。对该字段做一个说明。

orientation

标识当前UIAbility组件启动时的方向。该方向的取值范围包括:

  • unspecified:未指定方向,由系统自动判断显示方向。
  • landscape:横屏。
  • portrait:竖屏。
  • landscape_inverted:反向横屏。
  • portrait_inverted:反向竖屏。
  • auto_rotation:随传感器旋转。
  • auto_rotation_landscape:传感器横屏旋转,包括了横屏和反向横屏。
  • auto_rotation_portrait:传感器竖屏旋转,包括了竖屏和反向竖屏。
  • auto_rotation_restricted:传感器开关打开,方向可随传感器旋转。
  • auto_rotation_landscape_restricted:传感器开关打开,方向可随传感器旋转为横屏, 包括了横屏和反向横屏。
  • auto_rotation_portrait_restricted:传感器开关打开,方向随可传感器旋转为竖屏, 包括了横屏和反向横屏。
  • locked:传感器开关关闭,方向锁定。

更多属性,可以查看下面链接。 https://developer.harmonyos.com/cn/docs/documentation/doc-guides-V3/module-configuration-file-0000001427744540-V3?catalogVersion=V3 比如,visible字段,默认不可以被其他应用调用。
标识当前UIAbility组件是否可以被其他应用调用。

  • true:表示可以被其他应用调用。
  • false:表示不可以被其他应用调用。

比如launchType字段,默认为单实例模式,

标识当前UIAbility组件的启动模式,可选标签值:

  • standard:标准实例模式,每次启动创建一个新的实例。
  • singleton:单实例模式,仅第一次启动创建新实例。
  • specified:指定实例模式,运行时由开发者决定是否创建新实例

比如requestPermissions标签

  • name:需要使用的权限名称。
  • reason:当申请的权限为user_grant权限时此字段必填,用于描述申请权限的原因。
  • usedScene:当申请的权限为user_grant权限时此字段必填。描述权限使用的场景由abilities和when组成。其中abilities可以配置为多个UIAbility组件,when表示调用时机。

更多关于HarmonyOS鸿蒙Next中stage模型中,横屏设置,以及其他配置参数详解的实战教程也可以访问 https://www.itying.com/category-93-b0.html

6 回复
aboutToAppear(): void {
 let context = getContext(this) as common.UIAbilityContext
 window.getLastWindow(context).then((lastWindow) => {
   lastWindow.setPreferredOrientation(window.Orientation.LANDSCAPE)
 })
}

更多关于HarmonyOS鸿蒙Next中stage模型中,横屏设置,以及其他配置参数详解的实战系列教程也可以访问 https://www.itying.com/category-93-b0.html


你好,如果我想单独设置某个页面横屏,怎么设置呀

你可以调用相关接口,在页面内通过 Window 对象的 setPreferredOrientation() 方法实现横竖屏切换。

好的,感谢,

  • 姓名:张三
  • 性别:男
  • 年龄:28
  • 地址:北京市

点击切换横竖屏

toLandSpace() {
  let windowClass;
  try {
    console.log('-----1---' + getContext(this));
    window.getLastWindow(getContext(this), (err, data) => {
      if (err.code) {
        console.error('Failed to obtain the top window. Cause: ' + JSON.stringify(err));
        return;
      }
      windowClass = data;
      console.info('Succeeded in obtaining the top window. Data: ' + JSON.stringify(data));
    });
  } catch (exception) {
    console.error('Failed to obtain the top window. Cause: ' + JSON.stringify(exception));
  }
  console.log('-----2---' + windowClass);
  let orientation = window.Orientation.LANDSCAPE;
  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的Stage模型中,横屏设置可以通过orientation属性进行配置。开发者可以在config.json文件中设置"orientation": "landscape"来强制应用横屏显示。此外,Stage模型还支持其他配置参数,如launchType(启动类型)、minAPIVersion(最低API版本)、targetAPIVersion(目标API版本)等,以优化应用的兼容性和性能。详细配置可参考官方文档。

回到顶部