arkts v3.2 Release 中,设置横屏方式代码
async hengButton() {
let screenClass: screen.Screen | null = null;
// this.isLand = true
screen.setScreenRotationLocked(false).then(() => {
screen.getAllScreens((err, data) => {
if (err.code) {
console.error('Failed to get all screens. Cause: === ' + JSON
.stringify(err));
return;
}
console.info('Succeeded in getting all screens. Data:===' + JSON.stringify(data));
screenClass = data[0];
screenClass.setOrientation(screen.Orientation.HORIZONTAL, (err) => {
if (err.code) {
console.error('Failed to set the vertical orientation. Code: ===' + JSON.stringify(err));
return;
}
console.info('Succeeded in setting the vertical orientation.===')
});
})
})
}
使用方法
引入import screen from '@ohos.screen';
在生命周期aboutToAppear中执行即可
this.hengButton()