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

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

HarmonyOS
2024-01-21 13:36:04
3161浏览
收藏 0
回答 1
回答 1
按赞同
/
按时间
lzhlzh263

采用窗口的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方法,设置窗口的显示方向,PROTRAIT为固定竖屏,其他方向可参照参考链接 
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)); 
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.

已于2024-1-31 11:50:52修改
分享
微博
QQ
微信
回复
2024-01-22 15:42:49
相关问题
HarmonyOS 怎么监听屏幕横竖旋转
740浏览 • 1回复 待解决
如何获取当前是横还是啊?
5709浏览 • 1回复 待解决
HarmonyOS Camera录制视频如何适配
666浏览 • 0回复 待解决
HarmonyOS 如何监听屏幕旋转事件?
588浏览 • 1回复 待解决
如何实现应用的屏幕自动旋转
2951浏览 • 1回复 待解决
HarmonyOS 如何获取屏幕旋转角度
703浏览 • 1回复 待解决
HarmonyOS如何设置应用跟随屏幕旋转
962浏览 • 1回复 待解决
HarmonyOS 查询当前状态是横还是
1096浏览 • 1回复 待解决