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

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

HarmonyOS
2024-01-21 13:36:04
浏览
收藏 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)); 
}

已于2024-1-31 11:50:52修改
分享
微博
QQ
微信
回复
2024-01-22 15:42:49
相关问题
如何实现应用的屏幕自动旋转
986浏览 • 1回复 待解决
如何获取当前是横还是啊?
3690浏览 • 1回复 待解决
有谁知道如何监听屏幕旋转
857浏览 • 1回复 待解决
如何监听手机屏幕的灭和亮
35浏览 • 1回复 待解决
屏幕自动旋转的示例有哪些?
71浏览 • 1回复 待解决
如何设置屏幕方向为横
487浏览 • 1回复 待解决
横竖旋转demo有哪些?
411浏览 • 1回复 待解决
屏幕旋转计算,有什么好的方案?
459浏览 • 1回复 待解决
如何监听手机屏幕的横竖切换?
38浏览 • 1回复 待解决
禁用屏幕旋转的问题有知道的吗?
1544浏览 • 1回复 待解决
鸿蒙js如何获取设备屏幕宽和高?
4121浏览 • 1回复 待解决
如何监听设备横竖切换状态呢
672浏览 • 1回复 待解决
如何获取设备横竖的状态变化通知
823浏览 • 1回复 待解决
如何、录、投
504浏览 • 1回复 待解决