问题现象
设备开启旋转锁定,但应用内页面仍跟随传感器自动旋转。
问题定位
- 检查module.json5配置文件中abilities标签的orientation字段设置是否正确。
- 在日志中搜索关键字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
修改建议
- 根据实际需求设置合理的字段,例如设置为auto_rotation_unspecified受开关控制且由系统判定的自动旋转。
- 使用setPreferredOrientation将主窗口的显示方向属性设置为AUTO_ROTATION_LANDSCAPE_RESTRICTED。
常见FAQ
Q:当Ability设置了auto_rotation_landscape_restricted,如何将窗口动态设置为竖屏模式?
A:使用window.setPreferredOrientation接口,在运行时设置orientation为PORTRAIT,从而将屏幕变成竖屏模式。