如何锁定HarmonyOS鸿蒙Next设备竖屏,使得窗口不随屏幕旋转

如何锁定HarmonyOS鸿蒙Next设备竖屏,使得窗口不随屏幕旋转 解决措施

采用窗口的setPreferredOrientation方法可以实现该效果,将orientation参数设置为window.Orientation.PORTRAIT时,可锁定屏幕为竖屏。

代码示例

import { BusinessError } from '@kit.BasicServicesKit'; 
import { window } from '@kit.ArkUI'; 

//1.获取窗口实例对象,新建窗口使用createWindow方法,获取已有的窗口使用findWindow方法 
let windowClass: window.Window | undefined = undefined; 
let config: window.Configuration = { 
  name: "alertWindow",  
  windowType: window.WindowType.TYPE_SYSTEM_ALERT,  
  ctx: this.context 
}; 
try { 
  let promise = window.createWindow(config); 
  promise.then((data) => { 
    windowClass = data; 
    console.info('Succeeded in creating the window. Data:' + JSON.stringify(data)); 
  }).catch((err: BusinessError) =>{ 
    console.error('Failed to create the Window. Cause:' + JSON.stringify(err)); 
  });
}catch (exception) { 
  console.error('Failed to create the window. Cause: ' + JSON.stringify(exception)); 
} 
//2.窗口实例使用setPreferredOrientation方法,设置窗口的显示方向,PORTRAIT为固定竖屏,其他方向可参照参考链接 
let orientation = window.Orientation.AUTO_ROTATION; 
try { 
  let windowClass: window.Window = window.findWindow("test"); 
  windowClass.setPreferredOrientation(orientation, (err: BusinessError) => { 
    const errCode: number = err.code; 
    if (errCode) { 
      console.error('Failed to set window orientation. Cause: ' + JSON.stringify(err)); 
      return; 
    } 
    console.info('Succeeded in setting window orientation.'); 
  }); 
} catch (exception) { 
  console.error('Failed to set window orientation. Cause: ' + JSON.stringify(exception)); 
}

参考链接

window.Orientation

3 回复

在module.json5里面可以设置orientation,设置orientation为portrait即可。

更多关于如何锁定HarmonyOS鸿蒙Next设备竖屏,使得窗口不随屏幕旋转的实战系列教程也可以访问 https://www.itying.com/category-93-b0.html


没找到啊,

在HarmonyOS鸿蒙Next设备上,锁定竖屏以防止窗口随屏幕旋转的步骤如下:

  1. 打开“设置”应用。
  2. 进入“显示和亮度”选项。
  3. 找到“屏幕旋转”或类似选项。
  4. 关闭“自动旋转”功能,确保设备始终保持竖屏状态。

这样设置后,设备将不会自动旋转屏幕,窗口将始终保持竖屏显示。确保系统更新至最新版本以获得最佳体验。

回到顶部