入门图库示例
当前不维护了
去鸿蒙社区了 https://harmonyos.51cto.com/column/43
一次开发,多段部署
分布式任务调度
鸿蒙os 发布形式是 app Pack
一个app 只能有一个 entry类型 以及多个 多个feature
- entry:应用的主模块。一个APP中,对于同一设备类型必须有且只有一个entry类型的HAP,可独立安装运行。
- feature:应用的动态特性模块。一个APP可以包含一个或多个feature类型的HAP,也可以不含。只有包含Ability的HAP才能够独立运行。
目录结构
base
base 翻译后是基础 也就是说 这里面的文件是app 运行时必须有的 基础文件目录,有如下路面结构。
- element 参数配置文件
存放如 颜色 尺寸 描述信息 以json格式存储。
示例
颜色
"color": [
{
"name": "colorPrimary",
"value": "#E6000000"
},]
float 数字类型配置
"float": [
{
"name": "textSizeHeadline6",
"value": "30fp"
},]
# 颜色
"string": [
{
"name": "mainability_description",
"value": "Java_Phone_Category List Ability"
},]
- graphic
这个存放着 形状参数 类似前端的骨架 如果不对请指出来web前端跟鸿蒙还是有很大区别的开发思想上
示例
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:ohos="http://schemas.huawei.com/res/ohos"
ohos:shape="rectangle">
<corners
ohos:radius="$float:searchBarCornerRadius"/>
<solid
ohos:color="$color:colorSearchBarBackground"/>
</shape>
- media
媒体资源,包括图片、音频、视频等非文本格式的文件
- layout
布局资源
- profile
其他文件资源目录
生命周期
Page页面生命周期
Service生命周期
渲染
数据
RecycleItemProvider
提供一个RecycleItemProvider对象,以将ComponentProvider项与组件的基本数据关联起来。
列表循环使用
页面布局
尺寸单位 布局容器
ComponentContainer 布局容器
Button button = new Button(getContext());
//相对内容尺寸盛满
button.setWidth(ComponentContainer.LayoutConfig.MATCH_CONTENT);
button.setHeight(ComponentContainer.LayoutConfig.MATCH_CONTENT);
布局组件
例如以单一方向排列的DirectionalLayout、 类似前端 div
以相对位置排列的DependentLayout、 类似div + position的相对定位
以确切位置排列的PositionLayout等。 类似div + position的绝对定位
也就是说如果你想实现布局就须使用上诉的布局组件
代码示例
包括 颜色 间距 点击时间 布局模式
package com.example.myapplication.slice;
import com.example.myapplication.ResourceTable;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.colors.RgbColor;
import ohos.agp.components.*;
import ohos.agp.components.element.Element;
import ohos.agp.components.element.ShapeElement;
import ohos.agp.utils.Color;
import ohos.agp.utils.TextAlignment;
import ohos.hiviewdfx.HiLog;
import ohos.hiviewdfx.HiLogLabel;
/**
* 我为了前端开发者好理解我就用前端知识点写注释
*/
public class MainAbilitySlice extends AbilitySlice {
@Override
public void onStart(Intent intent) {
super.onStart(intent);
//添加div 这里需要添加 getContext 格式化上下文必须添加至于原因以后再说
DirectionalLayout directionalLayout = new DirectionalLayout(getContext());
//宽度这里如果是最外层的 div 就是相当于 100%
directionalLayout.setWidth(ComponentContainer.LayoutConfig.MATCH_PARENT);
directionalLayout.setHeight(ComponentContainer.LayoutConfig.MATCH_PARENT);
Button button = new Button(getContext());
button.setWidth(ComponentContainer.LayoutConfig.MATCH_CONTENT);
button.setHeight(ComponentContainer.LayoutConfig.MATCH_CONTENT);
// extends Element 模型
ShapeElement shapeElement = new ShapeElement();
shapeElement.setRgbColor(new RgbColor(0,0,0));//设置模型颜色
shapeElement.setCornerRadius(20);
button.setBackground(shapeElement);
button.setText("跳转");
button.setPadding(10, 10, 10, 10);
button.setTextSize(50);
button.setTextColor(Color.WHITE);
button.setClickedListener(new Component.ClickedListener() {
@Override
public void onClick(Component component) {
// 此处添加按钮被点击需要执行的操作
HiLogLabel label = new HiLogLabel(HiLog.LOG_APP, 0x00201, "MY_TAG");
HiLog.info(label,"我点击了我添加的跳转按钮");
}
});
// 相对定位布局
DependentLayout dependentLayout = new DependentLayout(getContext());
Text text = new Text(getContext());
text.setText("我第一次自学");
text.setTextAlignment(TextAlignment.CENTER);
text.setWidth(ComponentContainer.LayoutConfig.MATCH_PARENT);
text.setTextSize(40);
dependentLayout.addComponent(text);
directionalLayout.addComponent(dependentLayout);
directionalLayout.addComponent(button);
//水平布局 VERTICAL垂直
directionalLayout.setOrientation(Component.VERTICAL);
// 将布局作为根布局添加到视图树中
super.setUIContent(directionalLayout);
}
@Override
public void onActive() {
super.onActive();
}
@Override
public void onForeground(Intent intent) {
super.onForeground(intent);
}
}
尺寸单位
AttrHelper
AttrHelper.vp2px 参数如下
float value, float density 值 密度
根据屏幕密度将虚拟像素(vp)转换为像素值。
案例
upperList.setHeight(AttrHelper.vp2px(LIST_ITEM_HEIGHT, this) * UPPER_LIST_LEN);