HarmonyOS 如何让设备,小屏幕不支持设备旋转,中大屏幕支持设备旋转?

如何让设备,小屏幕不支持设备旋转,中大屏幕支持设备旋转,目前的代码都不旋转

阅读 610
1 个回答

应用设置屏幕自动旋转:在模块配置文件module.json5中给EntryAbility设置"orientation": “auto\_rotation\_restricted”,再打开手机自动旋转即可。

使用deviceInfo.deviceType去获取设备类型。

Demo:

Index.ets:

onPageShow() {
  console.log(deviceInfo.deviceType);
  //如果设备类型为手机(或判断屏幕大小)
  if (deviceInfo.deviceType == "phone"){
    //将窗口设置为受旋转开关控制的自动旋转
    window.getLastWindow(getContext(this),(err,win)=>{
      win.setPreferredOrientation(window.Orientation.AUTO_ROTATION_RESTRICTED)
    })
  }
}