今天研究了一下华为鸿蒙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