鸿蒙开发之设置横竖屏

我的需求是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)
  }

这三步即可设置部分页面横屏

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值