文档开发者中心
我的
|

ArkUI (方舟UI框架)

UI

HarmonyOS 5

HarmonyOS 页面orientation横竖屏切换 (API12+)

Reactnative开发。

入口EntryAbility在module.json5的设置如下,orientation设置了竖屏:

收起
自动换行
深色代码主题
复制
"abilities": [
  {
    "name": "EntryAbility",
    "srcEntry": "./ets/entryability/EntryAbility.ets",
    "description": "$string:EntryAbility_desc",
    "icon": "$media:layered_image",
    "label": "$string:EntryAbility_label",
    "startWindowIcon": "$media:app_icon",
    "startWindowBackground": "$color:start_window_background",
    "exported": true,
    "orientation": "portrait",
    "skills": [
      {
        "entities": [
          "entity.system.home"
        ],
        "actions": [
          "action.system.home"
        ]
      }
    ]
  }
]

现在需要跳转到原生的另一个pages中。目前项目有两个pages,Index.ets,Second.ets。

Second的aboutToAppear如下:

收起
自动换行
深色代码主题
复制
async aboutToAppear() {
  const monitor = new SystemNetworkMonitor()
  monitor.startMonitoring((netType) => {
    this.netType = netType
  })
  await this.requestPermissions();
  const params = router.getParams() as RouteParams; // 类型断言
  if (params.pushUrl) { // 使用点表示法访问
    this.pushUrl = params.pushUrl;
  }
  // 设置屏幕为横屏
  const windowClass = await window.getLastWindow(this.context);
  await windowClass.setPreferredOrientation(window.Orientation.LANDSCAPE);
  this.player = createV2TXLivePusher(getContext(), V2TXLiveMode.V2TXLiveModeRTMP);
  const videoEncoder = new V2TXLiveVideoEncoderParam(V2TXLiveVideoResolution.V2TXLiveVideoResolution1280x720)
  videoEncoder.videoBitrate = 2500
  videoEncoder.videoResolutionMode = V2TXLiveVideoResolutionMode.V2TXLiveVideoResolutionModeLandscape
  this.player.setVideoQuality(videoEncoder)
  this.player.setRenderView(this.viewId);
  this.player.setRenderMirror(V2TXLiveMirrorType.V2TXLiveMirrorTypeDisable)
  this.player.setRenderFillMode(V2TXLiveFillMode.V2TXLiveFillModeFit)
  this.player.setObserver(this.playerObserver);
  this.player.startCamera(false)
  this.player.startMicrophone()
}

当关闭Second后,返回首页还是横屏,希望首页orientation模式不变如何处理。

点赞
收藏
回复
1
分享
举报
浏览64 发布于2026-04-02 11:25未知归属地
全部评论
最多点赞
最新发布
最早发布

页面加载时在aboutToAppear方法中设置页面横屏,页面消失时在onPageHide方法中设置页面竖屏。

代码示例如下:

收起
自动换行
深色代码主题
复制
import { router, window } from '@kit.ArkUI';
import { BusinessError } from '@kit.BasicServicesKit';

@Entry
@Component
struct LandscapeScreen {
  @State message: string = '横屏页面';

  // 设置横屏
  aboutToAppear(): void {
    let orientation = window.Orientation.LANDSCAPE
    this.setScreenOrientation(orientation)
  }

  // 设置竖屏
  onPageHide(): 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(this.message)
        .fontSize(30)
        .textAlign(TextAlign.Center)
        .width('100%')
        .fontWeight(500)
        .height('50%')
        .onClick(() => {
          // 跳转index页面,验证取消横屏是否成功
          router.pushUrl({ url: 'pages/Index' })
        })
    }
    .justifyContent(FlexAlign.Center)
    .width('100%')
    .backgroundColor(Color.White)
    .height('100%')
  }
}
展开全部
1楼回复于2026-04-02 11:25 未知归属地
写回答
新增插入模板功能
一键使用模板,快速填写内容,轻松发帖~
知道了
  • 为了保障您的信息安全,请勿上传您的敏感个人信息(如您的密码等信息)和您的敏感资产信息(如关键源代码、签名私钥、调试安装包、业务日志等信息),且您需自行承担由此产生的信息泄露等安全风险。
  • 如您发布的内容为转载内容,请注明内容来源。

我要提问题

了解社区公约,与您携手共创和谐专业的开发者社区。