目录

如何设置窗口旋转

步骤一:通过getLastWindow()、createWindow()、findWindow()中的任一方法获取到Window实例。

步骤二:通过设置setPreferredOrientation属性来设置窗口的显示方向属性,使用callback异步回调。参数Orientation提供了窗口显示方向类型枚举。

在EntryAbility.ets中的onWindowStageCreate方法中将WindowStage设置一个AppStorage。参考代码如下:

AppStorage.setOrCreate('windowStage',windowStage);

通过setPreferredOrientation可以设置旋转模式。

import { display, window } from '@kit.ArkUI';

@Component
struct ScreenRotation {
  windowStage: window.WindowStage = AppStorage.get('windowStage') as window.WindowStage;
  // Method to get the main window
  mainWin: window.Window = this.windowStage.getMainWindowSync();
  context = this.getUIContext();

  onPageShow() {
    // Method to get the top window
    window.getLastWindow(this.context.getHostContext());
    this.mainWin.setPreferredOrientation(window.Orientation.LANDSCAPE);
    // Use display interface to get current rotation direction, can be placed in listener for continuous updates
    display.getDefaultDisplaySync().rotation;
  }

  build() {
    Row() {
      Column({ space: 10 }) {
        Text('Screen rotation demo')
          .fontSize(25)
          .margin(20)
          .fontColor(0x3399FF)
      }.width('100%')
    }.height('100%').backgroundColor(Color.White)
  }
}