HarmonyOS 怎么设置某个 page 横屏展示

应用开发中,大部分page 是竖屏展示,某个page 需要横屏展示,怎么设置呢?

HarmonyOS
2024-12-25 11:26:26
浏览
收藏 0
回答 1
回答 1
按赞同
/
按时间
put_get

1、首先打开entry下面的module.json5文件,在abilities节点下添加一个orientation的属性:

“orientation”: ‘unspecified’
  • 1.

2、第一个页面:

import { router } from '@kit.ArkUI';

@Entry
@Component
struct aboutScreen1 {
  @State message: string = '';

  build() {
    Column() {
      Text('首页,点我跳转')
        .fontSize(30)
        .textAlign(TextAlign.Center)
        .width('100%')
        .fontWeight(500)
        .height('100%')
        .onClick(() => {
          router.pushUrl({url: "pages/aboutScreen2" })
        })
    }
    .justifyContent(FlexAlign.Center)
    .width('100%')
    .backgroundColor(Color.White)
    .height('100%')
  }
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.

3、第二个页面:

import { router, window } from '@kit.ArkUI';
import { BusinessError } from '@kit.BasicServicesKit';

@Entry
@Component
struct aboutScreen1 {
  @State message: string = '';

  aboutToAppear(): void {
    let orientation = window.Orientation.LANDSCAPE
    this.setScreenOrientation(orientation)
  }

  aboutToDisappear(): void {
    let orientation = window.Orientation.PORTRAIT
    this.setScreenOrientation(orientation)
  }

  setScreenOrientation(orientation: window.Orientation) {
    let windowClass: window.Window | undefined = undefined;
    try{
      let promise = window.getLastWindow(getContext())
      promise.then((data) => {
        windowClass = data
        windowClass.setPreferredOrientation(orientation)
      }).catch((err: BusinessError) => {
        console.error('getLastWindow error')
      })
    } catch (e) {
      console.error('setScreenOrientation error')
    }
  }

  build() {
    Column() {
      Text('我横屏啦')
        .fontSize(30)
        .textAlign(TextAlign.Center)
        .width('100%')
        .fontWeight(500)
        .height('50%')
    }
    .justifyContent(FlexAlign.Center)
    .width('100%')
    .backgroundColor(Color.White)
    .height('100%')
  }
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
  • 37.
  • 38.
  • 39.
  • 40.
  • 41.
  • 42.
  • 43.
  • 44.
  • 45.
  • 46.
  • 47.
  • 48.
分享
微博
QQ
微信
回复
2024-12-25 14:28:10
相关问题
page页面如何设置显示
2284浏览 • 1回复 待解决
HarmonyOS webView视频展示
740浏览 • 1回复 待解决
如何设置屏幕方向为
2285浏览 • 1回复 待解决
HarmonyOS 子窗口要如何设置
610浏览 • 1回复 待解决
HarmonyOS 播放问题
1297浏览 • 1回复 待解决
ArkUI 时应用自动重启怎么回事?
2675浏览 • 1回复 待解决
HarmonyOS 后布局问题
1140浏览 • 1回复 待解决
HarmonyOS video如何播放?
975浏览 • 1回复 待解决
HarmonyOS 查询当前状态是还是竖
1001浏览 • 1回复 待解决
HarmonyOS 如何让app支持
901浏览 • 1回复 待解决