HarmonyOS 悬浮窗在横屏下如何强制显示竖屏?

应用会创建一个悬浮窗,想让悬浮窗在横屏状态下也是竖屏显示,按如下方式配置了悬浮窗强制竖屏,但是不生效

阅读 682
1 个回答

可以通过设置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)
  }
}
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进