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()

posted @ 2024-02-24 17:36  好运的菜狗  阅读(84)  评论(0编辑  收藏  举报