HarmonyOS 应用内如何实现分屏

HarmonyOS
2025-01-09 16:09:54
浏览
收藏 0
回答 1
回答 1
按赞同
/
按时间
superinsect

示例代码如下:

1、Index页面

import { common, StartOptions } from '@kit.AbilityKit';

@Entry
@Component
struct Index {
  @State message: string = 'Hello World';

  build() {
    Row() {
      Column({ space: 8 }) {
        Text(this.message + '首页')
          .fontSize(20)
          .fontWeight(FontWeight.Bold)
        Divider()
          .height(1).width('100%')

        Text('点击我启动分屏')
          .fontSize(16)
          .fontWeight(FontWeight.Bold)
          .margin({top:'20'})
          .onClick(() => {
            let want = {
              bundleName: 'com.example.myapplication0802',
              abilityName: 'Ability2',
              moduleName: 'entry'
            } as Want;
            let options = { windowMode: 101, } as StartOptions;
            this.startAbility(want, options);
          })
      }
    }

    .height('100%')
    .width('100%')
  }

  startAbility(want: Want, options: StartOptions,) {
    this.message = `want: ${JSON.stringify(want)}, options: ${JSON.stringify(options)}`;
    try {
      (getContext(this) as common.UIAbilityContext)
        .startAbility(want, options, (error) => {
          if (error.code) {
            // 处理业务逻辑错误
            console.info(`startAbility error`);
            return;
          }
          // 执行正常业务
          console.info(`startAbility succeed want:${JSON.stringify(want)} options: '${JSON.stringify(options)}'`);
        });
    } catch (paramError) {
      // 处理入参错误异常
      console.info('startAbility catch error.code: ' + JSON.stringify(paramError.code) +
        ' error.message: ' + JSON.stringify(paramError.message));
    }
  }
}
  • 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.
  • 49.
  • 50.
  • 51.
  • 52.
  • 53.
  • 54.
  • 55.
  • 56.

2、在entry/src/main/ets 下新建 ability2 文件夹,里面新增文件ability2

import AbilityConstant from '@ohos.app.ability.AbilityConstant';
import hilog from '@ohos.hilog';
import UIAbility from '@ohos.app.ability.UIAbility';
import Want from '@ohos.app.ability.Want';
import window from '@ohos.window';

export default class Ability2 extends UIAbility {
  onCreate(want: Want, launchParam: AbilityConstant.LaunchParam): void {
    hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onCreate');
  }

  onDestroy(): void {
    hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onDestroy');
  }

  onWindowStageCreate(windowStage: window.WindowStage): void {
    // Main window is created, set main page for this ability
    hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onWindowStageCreate');

    windowStage.loadContent('pages/Index2', (err, data) => {
      if (err.code) {
        hilog.error(0x0000, 'testTag', 'Failed to load the content. Cause: %{public}s', JSON.stringify(err) ?? '');
        return;
      }
      hilog.info(0x0000, 'testTag', 'Succeeded in loading the content. Data: %{public}s', JSON.stringify(data) ?? '');
    });
  }

  onWindowStageDestroy(): void {
    // Main window is destroyed, release UI related resources
    hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onWindowStageDestroy');
  }

  onForeground(): void {
    // Ability has brought to foreground
    hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onForeground');
  }

  onBackground(): void {
    // Ability has back to background
    hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onBackground');
  }
}
  • 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.

3、在module.json5中的 “abilities” 数组中新增配置,对应的资源字符串需要新增

{
  "name": "Ability2",
"srcEntry": "./ets/ability2/Ability2.ets",
"orientation": "auto_rotation",
"description": "$string:Ability2_desc",
"icon": "$media:layered_image",
"label": "$string:Ability2_label",
"startWindowIcon": "$media:startIcon",
"startWindowBackground": "$color:start_window_background"
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.

4、在pages中新增页面Index2,并在main_pages.json增加配置

import { common, StartOptions } from '@kit.AbilityKit';

@Entry
@Component
struct Index2 {
  @State message: string = 'Hello World';

  build() {
    Row() {
      Column({ space: 8 }) {
        Text(this.message + '第二页')
          .fontSize(20)
          .fontWeight(FontWeight.Bold)
        Divider()
          .height(1).width('100%')

        Text('我是第二页')
          .fontSize(16)
          .fontWeight(FontWeight.Bold)
          .margin({top:'20'})
      }
    }

    .height('100%')
    .width('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.
分享
微博
QQ
微信
回复
2025-01-09 19:21:39


相关问题
HarmonyOS 如何禁止应用分屏
311浏览 • 1回复 待解决
HarmonyOS 应用弹窗实现
678浏览 • 1回复 待解决
HarmonyOS 如何实现应用的语言切换
645浏览 • 1回复 待解决
HarmonyOS 应用怎么禁用分屏
309浏览 • 1回复 待解决
如何禁止应用分屏和小窗展示
2501浏览 • 1回复 待解决
HarmonyOS 分屏模式,图片跨应用拖拽
324浏览 • 1回复 待解决
如何实现设备应用的UIAbility跳转
2826浏览 • 1回复 待解决
HarmonyOS 如何关闭分屏模式
629浏览 • 1回复 待解决
基于子窗口实现应用悬浮窗
981浏览 • 1回复 待解决
HarmonyOS 应用升级服务,如何测试
361浏览 • 1回复 待解决
HarmonyOS 折叠屏一多分屏怎么实现
334浏览 • 1回复 待解决
HarmonyOS 应用升级
878浏览 • 1回复 待解决
HarmonyOS 如何实现APP全局弹窗
960浏览 • 1回复 待解决