如何实现应用的屏幕自动旋转

2024-11-19 08:48:08
172次阅读
0个评论

如何实现应用的屏幕自动旋转

有开发者问,如何实现应用的屏幕自动旋转,其实只要在module.json5添加属性"orientation": "auto_rotation"。就可以。

orientation标识当前UIAbility组件启动时的方向。orientation支持的取值如下:

  • - unspecified:未指定方向,由系统自动判断显示方向。
  • - landscape:横屏。
  • - portrait:竖屏。
  • - follow_recent:跟随背景窗口的旋转模式。
  • - landscape_inverted:反向横屏。
  • - portrait_inverted:反向竖屏。
  • - auto_rotation:随传感器旋转。
  • - auto_rotation_landscape:传感器横屏旋转,包括横屏和反向横屏。
  • - auto_rotation_portrait:传感器竖屏旋转,包括竖屏和反向竖屏。
  • - auto_rotation_restricted:传感器开关打开,方向可随传感器旋转。
  • - auto_rotation_landscape_restricted:传感器开关打开,方向可随传感器旋转为横屏, 包括横屏和反向横屏。
  • - auto_rotation_portrait_restricted:传感器开关打开,方向随可传感器旋转为竖屏, 包括竖屏和反向竖屏。
  • - locked:传感器开关关闭,方向锁定。
  • - auto_rotation_unspecified:受开关控制和由系统判定的自动旋转模式。
  • - follow_desktop:跟随桌面的旋转模式。

如下所示:

"abilities": [ 
  { 
    "name": "EntryAbility", 
    "srcEntry": "./ets/entryability/EntryAbility.ets", 
    "description": "$string:EntryAbility_desc", 
    "icon": "$media:icon", 
    "label": "$string:EntryAbility_label", 
    "startWindowIcon": "$media:icon", 
    "startWindowBackground": "$color:start_window_background", 
    "exported": true, 
    "skills": [ 
      { 
        "entities": [ 
          "entity.system.home" 
        ], 
        "actions": [ 
          "action.system.home" 
        ] 
      } 
    ], 
    "orientation": "auto_rotation", // 随传感器旋转 
  } 
]

再打开手机自动旋转按钮即可。

所以了解了吗?

备注

作者:夏天

链接:https://www.nutpi.net/

出处:https://www.arkui.club/

来源:坚果派

著作权归作者所有,禁止任何未经授权的个人或组织以任何形式将本案例集及其附属资料、创新、创意、架构设计、算法、衍生作品等用于任何商业目的、盈利活动、各类竞赛(比赛)、直播教学、录播教学、线下课程、书籍编写、教材编写、会议、培训、公益活动、项目课题、毕业设计、毕业论文、学术论文等。商业转载请联系作者获得授权,非商业转载请注明出处。否则追究相关责任。

收藏00

登录 后评论。没有帐号? 注册 一个。