HarmonyOS ArkTS 横竖屏设置

一、全局横竖屏设置,在module.json5下,对当前UIAbility配置orientation,如下:

{
  "module": {
    "name": "entry",
    "type": "entry",
    ...
    "abilities": [
      {
        "name": "EntryAbility",
        ...
        "orientation": "auto_rotation",
        ...
      }
    ]
  }
}

orientation属性列表如下:

orientation属性         描述
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
传感器开关关闭,方向锁定

二、对单个Entry,自定义页面设置横竖屏

在页面内通过 Window 对象的 setPreferredOrientation() 方法实现横竖屏切换,如下:

@Component
@Entry
struct DemoPage {
  ...

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

相关推荐

  1. HarmonyOS ArkTS 横竖设置

    2024-04-12 17:46:02       45 阅读
  2. uniapp横竖配置

    2024-04-12 17:46:02       30 阅读
  3. Android 横竖切换 窗口全

    2024-04-12 17:46:02       63 阅读
  4. iOS 控制每个vc横竖

    2024-04-12 17:46:02       42 阅读
  5. iOS 横竖的正确打开姿势

    2024-04-12 17:46:02       39 阅读
  6. iOS 让APP支持横竖

    2024-04-12 17:46:02       26 阅读
  7. Android : ViewModel_解决textView 横竖数据丢失

    2024-04-12 17:46:02       51 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-04-12 17:46:02       94 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-04-12 17:46:02       100 阅读
  3. 在Django里面运行非项目文件

    2024-04-12 17:46:02       82 阅读
  4. Python语言-面向对象

    2024-04-12 17:46:02       91 阅读

热门阅读

  1. Linux查看硬件信息

    2024-04-12 17:46:02       36 阅读
  2. TCP/IP协议介绍

    2024-04-12 17:46:02       48 阅读
  3. 前端编译时和运行时(简单易懂快速理解)

    2024-04-12 17:46:02       42 阅读
  4. arm 的system IP有哪些?

    2024-04-12 17:46:02       125 阅读
  5. 碳交易机制下考虑需求响应的优化运行-MATLAB复现

    2024-04-12 17:46:02       36 阅读
  6. Linux命令学习—linux 的文件系统

    2024-04-12 17:46:02       47 阅读
  7. [MySQL] 慢查询

    2024-04-12 17:46:02       50 阅读
  8. Python格式化输出的三种常用方式

    2024-04-12 17:46:02       37 阅读
  9. react中useMemo 钩子函数的使用

    2024-04-12 17:46:02       40 阅读
  10. 滑动窗口和螺旋矩阵(二十天)

    2024-04-12 17:46:02       107 阅读
  11. 人工智能在哪些行业赋能

    2024-04-12 17:46:02       90 阅读
  12. iperf3使用记录

    2024-04-12 17:46:02       40 阅读
  13. C#设计简单的WinForms多窗口应用程序[示例]

    2024-04-12 17:46:02       32 阅读
  14. Linux apt 命令

    2024-04-12 17:46:02       33 阅读
  15. Python_day26

    2024-04-12 17:46:02       40 阅读