目录

旋转锁定失效

问题现象

设备开启旋转锁定,但应用内页面仍跟随传感器自动旋转。

背景知识

问题定位

  1. 检查module.json5配置文件中abilities标签的orientation字段设置是否正确。
  2. 在日志中搜索关键字setWindowOrientation或SetPreferredOrientation,如果发现有setWindowOrientation: 7 Succeeded.或OnSetPreferredOrientation end, window [xxxx, xxxx] orientation=7信息,则说明应用将主窗口的显示方向属性设置为AUTO_ROTATION_LANDSCAPE。

    AUTO_ROTATION_LANDSCAPE为Orientation枚举值之一,若日志结果为其他枚举值,可参考背景知识。

    com.examp....ittoggle I  setWindowOrientation: 7 Succeeded., %{public}s
    SetRequestedOrientation(1869): id: 1167 lastReqOrientation: 1 target: 7 state: 2
    [invalidDomain][]: OnSetPreferredOrientation end, window [1167, rrsp0] orientati

分析结论

修改建议

常见FAQ

Q:当Ability设置了auto_rotation_landscape_restricted,如何将窗口动态设置为竖屏模式?

A:使用window.setPreferredOrientation接口,在运行时设置orientation为PORTRAIT,从而将屏幕变成竖屏模式。