我的需求是APP大部分是竖屏显示,在某个页面需要显示横屏。
直接上答案
1、在EntryAbility中获取globalThis.windowClass
onWindowStageCreate(windowStage: window.WindowStage) {
// Main window is created, set main page for this ability
hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onWindowStageCreate');
windowStage.getMainWindow((err, data) => {
if (err.code) {
console.error('Failed to obtain the main window. Cause: ' + JSON.stringify(err));
return;
}
//保存globalThis.windowClass
globalThis.windowClass = data
console.info('Succeeded in obtaining the main window. Data: ' + JSON.stringify(data));
});
windowStage.loadContent('pages/BarrageTabPage', (err, data) => {
if (err.code) {
hilog.error(0x0000, 'testTag', 'Failed to load the content. Cause: %{public}s', JSON.stringify(err) ?? '');
return;
}
hilog.info(0x0000, 'testTag', 'Succeeded in loading the content. Data: %{public}s', JSON.stringify(data) ?? '');
});
}
2、在需要横屏显示的页面中添加
//设置该页面为横屏
aboutToAppear() {
globalThis.windowClass.setPreferredOrientation(window.Orientation.LANDSCAPE)
}
3、返回到竖屏显示的页面,需要在竖屏页面添加
onPageShow() {
globalThis.windowClass.setPreferredOrientation(window.Orientation.PORTRAIT)
}
这三步即可设置部分页面横屏