可以通过设置windowClass.setPreferredOrientation(window.Orientation.LOCKED),LOCKED会根据当前显示的方向锁定屏幕禁止旋转import { window } from '@kit.ArkUI'; @Entry @Component struct Test { @State message: string = 'Hello World'; build() { Column() { Text(this.message) .id('Index4HelloWorld') .fontSize(50) .fontWeight(FontWeight.Bold) .fontColor(Color.Black) .alignRules({ center: { anchor: '__container__', align: VerticalAlign.Center }, middle: { anchor: '__container__', align: HorizontalAlign.Center } }) Button('锁定').onClick(() => { window.getLastWindow(getContext(this), (err, win) => { win.setPreferredOrientation(window.Orientation.LOCKED) }) }) .fontColor(Color.Black) Button('解除锁定') .fontColor(Color.Black) .onClick(() => { window.getLastWindow(getContext(this), (err, win) => { win.setPreferredOrientation(window.Orientation.AUTO_ROTATION) }) }) } .height('100%') .width('100%') .backgroundColor(Color.Orange) } }
可以通过设置windowClass.setPreferredOrientation(window.Orientation.LOCKED),LOCKED会根据当前显示的方向锁定屏幕禁止旋转