华为鸿蒙OS应用开发更换启动首页

  今天研究了一下华为鸿蒙OS应用开发更换启动首页,首先我们创建个测试项目如下图,我这里设备选择TV,你也可以选择Wearable,模板选择测试Empty Feature Ability(Java)模板

点击Next 跳到如下图

点击Finish按钮后进入项目,定位到下图目录下并创建一个新页面

跳到下图设置Ability名称

点击Finish后工具在创建Ability的同时还帮我们创建了这个Ability对应的AbilitySlice,如下图

接下来将NewsAbilitySlice中的文本内容更改为“测试首页”,代码如下

public class NewsAbilitySlice extends AbilitySlice {

    private DirectionalLayout myLayout = new DirectionalLayout(this);

    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        LayoutConfig config = new LayoutConfig(LayoutConfig.MATCH_PARENT, LayoutConfig.MATCH_PARENT);
        myLayout.setLayoutConfig(config);
        ShapeElement element = new ShapeElement();
        element.setRgbColor(new RgbColor(255, 255, 255));
        myLayout.setBackground(element);

        Text text = new Text(this);
        text.setLayoutConfig(config);
        text.setText("测试首页");
        text.setTextColor(new Color(0xFF000000));
        text.setTextSize(50);
        text.setTextAlignment(TextAlignment.CENTER);
        myLayout.addComponent(text);
        super.setUIContent(myLayout);
    }

现在如果我们运行项目会是默认的首页,如何运行这里就不演示了,我们讲重点,如何更换程序启动首页,首先我们打开main目录下的config.json文件,代码如下

{
  "app": {
    "bundleName": "com.example.myfirsthome",
    "vendor": "example",
    "version": {
      "code": 1,
      "name": "1.0"
    },
    "apiVersion": {
      "compatible": 3,
      "target": 3
    }
  },
  "deviceConfig": {},
  "module": {
    "package": "com.example.myfirsthome",
    "name": ".MyFirstHome",
    "reqCapabilities": [
      "video_support"
    ],
    "deviceType": [
      "tv"
    ],
    "distro": {
      "deliveryWithInstall": true,
      "moduleName": "entry",
      "moduleType": "entry"
    },
    "abilities": [
      {
        "skills": [
          {
            "entities": [
              "entity.system.home"
            ],
            "actions": [
              "action.system.home"
            ]
          }
        ],
        "orientation": "landscape",
        "formEnabled": false,
        "name": "com.example.myfirsthome.MainAbility",
        "icon": "$media:icon",
        "description": "$string:mainability_description",
        "label": "MyFirstHome",
        "type": "page",
        "launchType": "standard"
      },
      {
        "orientation": "landscape",
        "formEnabled": false,
        "name": "com.example.myfirsthome.NewsAbility",
        "icon": "$media:icon",
        "description": "$string:newsability_description",
        "label": "entry",
        "type": "page",
        "launchType": "standard"
      }
    ]
  }
}

重点就是在abilities节点下的第一个对象里面的skills配置,如果我们讲这个skills移到我们刚才建的那个ability对象里,代码如下

{
  "app": {
    "bundleName": "com.example.myfirsthome",
    "vendor": "example",
    "version": {
      "code": 1,
      "name": "1.0"
    },
    "apiVersion": {
      "compatible": 3,
      "target": 3
    }
  },
  "deviceConfig": {},
  "module": {
    "package": "com.example.myfirsthome",
    "name": ".MyFirstHome",
    "reqCapabilities": [
      "video_support"
    ],
    "deviceType": [
      "tv"
    ],
    "distro": {
      "deliveryWithInstall": true,
      "moduleName": "entry",
      "moduleType": "entry"
    },
    "abilities": [
      {

        "orientation": "landscape",
        "formEnabled": false,
        "name": "com.example.myfirsthome.MainAbility",
        "icon": "$media:icon",
        "description": "$string:mainability_description",
        "label": "MyFirstHome",
        "type": "page",
        "launchType": "standard"
      },
      {
        "skills": [
          {
            "entities": [
              "entity.system.home"
            ],
            "actions": [
              "action.system.home"
            ]
          }
        ],
        "orientation": "landscape",
        "formEnabled": false,
        "name": "com.example.myfirsthome.NewsAbility",
        "icon": "$media:icon",
        "description": "$string:newsability_description",
        "label": "entry",
        "type": "page",
        "launchType": "standard"
      }
    ]
  }
}

那么现在如果我们在重新运行项目则启动首页则变成了我们刚才新增的页面,如下图

到这里首页更换就完成了,至于config.json中的配置节点作用可以去华为官方查看链接地址为https://developer.harmonyos.com/cn/docs/documentation/doc-guides/basic-config-file-elements-0000000000034463

  • 2
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值