HarmonyOS 鸿蒙Next release中设置横竖屏需要找到EntryAbility.ets
设置横竖屏(设置竖屏)
windowStage.getMainWindowSync().setPreferredOrientation(window.Orientation.PORTRAIT)
完整代码
onWindowStageCreate(windowStage: window.WindowStage): void {
//参考:https://developer.huawei.com/consumer/cn/doc/harmonyos-references-V5/js-apis-window-V5
// 沉浸式状态栏
let windowClass: window.Window | undefined = undefined;
windowStage.getMainWindow((err: BusinessError, data) => {
windowClass=data
let promise = windowClass.setWindowLayoutFullScreen(true);
promise.then(() => {
//设置状态栏透明背景
windowStage.getMainWindowSync().setWindowSystemBarEnable(['status']).then(() => {
const systemBarProperties: window.SystemBarProperties = {
statusBarColor: '#00000000'
};
//设置窗口内导航栏、状态栏的属性
windowStage.getMainWindowSync().setWindowSystemBarProperties(systemBarProperties)
.then(() => {
console.info('Succeeded in setting the system bar properties.');
}).catch((err:object) => {
console.error('Failed to set the system bar properties. Cause: ' + JSON.stringify(err));
});
})
}).catch((err: BusinessError) => {
console.error(`Failed to set the window layout to full-screen mode. Cause code: ${err.code}, message: ${err.message}`);
});
})
//设置横竖屏(设置竖屏)
windowStage.getMainWindowSync().setPreferredOrientation(window.Orientation.PORTRAIT)
windowStage.loadContent('pages/Index', (err) => {
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.');
});
}