搭建鸿蒙开发环境、运行第一个鸿蒙项目、鸿蒙组件与安卓组件的简单对比 (1)

淩亂°似流年 2023-01-02 02:19 438阅读 0赞

搭建鸿蒙开发环境、运行第一个鸿蒙项目、鸿蒙组件与安卓组件的简单对比

  • 安装node.js
  • 安装DevEco Studio
    • 配置开发语言
    • 配置模拟器
  • 第一个鸿蒙程序
    • 可能出现的错误
  • 常用布局
    • DirectionalLayout
    • DependentLayout
      • 相对与同级组件的位置关系属性
      • 相对与父组件的位置关系属性
    • StackLayout
  • 总结

安装node.js

首先我们先在node.js官网下载node.js并安装node.js

下载完成后一路next,其中可以选择安装路径,到最后点击finsh安装node.js

安装DevEco Studio

鸿蒙开发的IDE的下载地址:DevEco Studio
在这里插入图片描述

下载完成后运行进入DevEco Studio安装向导,在如下安装选项界面勾选DevEco Studio launcher后,点击Next,直至安装完成。
在这里插入图片描述

配置开发语言

在菜单栏中tools->SDK Manager中配置SDK,鸿蒙安装会自动勾选Java的SDK,若想使用C/C++或JS语言需自行勾选在这里插入图片描述

配置模拟器

相比于安卓的模拟器,鸿蒙的模拟器是安装在云端的,对与本地的性能消耗会变小极多,对于我这种机械硬盘的渣渣机子简直友好的不行(安卓我只能通过真机调试)
在菜单栏tools->HVD Manager中就可以下载他的模拟器,其中可以选择各个平台的模拟器,手机,手表,电视等等等等,而且我们程序是可以无缝的在这些设备转换的
(需要注意的是:想要下载鸿蒙的模拟器需要注册鸿蒙的开发者并且通过开发者的实名认证,不然是无法下载他们的模拟器的)

在这里插入图片描述
至此我们鸿蒙的开发环境已经全部配置完成了,接下来我来运行我的第一个鸿蒙程序吧

第一个鸿蒙程序

在菜单栏file->new->new project新建一个鸿蒙程序
在这里插入图片描述

然后我们在选择手机的模拟器然后运行就可以了
在这里插入图片描述

可能出现的错误

在这里插入图片描述
若出现上图的错误,那就是你的模拟器设备和你的项目规定运行的设备不一致
在config.json中可查找到项目该运行的平台设备
查看规定的设备是否跟模拟器设备相同
只需要改变deviceType的类型和模拟器相同即可运行
在这里插入图片描述
按住shilt键点击deviceType即可查看到deviceType的值有多少种
在这里插入图片描述

常用布局

鸿蒙不仅是在系统UI上与安卓颇为一致,即使是代码实现上也是极为相似的
这也有利于我们开发者可以快速的学习开发以及方向转移

DirectionalLayout

这与安卓中的线性布局对标
在这个布局下子组件的特有属性:
ohos:layout_alignment属性规范组件位置
ohos:weight规范组件权重

例子

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <DirectionalLayout
  3. xmlns:ohos="http://schemas.huawei.com/res/ohos"
  4. ohos:height="match_parent"
  5. ohos:width="match_parent"
  6. ohos:orientation="vertical">
  7. <Text
  8. ohos:id="$+id:text_helloworld"
  9. ohos:height="match_content"
  10. ohos:width="match_content"
  11. ohos:background_element="$graphic:background_ability_main"
  12. ohos:layout_alignment="horizontal_center"
  13. ohos:text="Hello World"
  14. ohos:text_size="50"
  15. />
  16. <Text
  17. ohos:height="match_content"
  18. ohos:width="0vp"
  19. ohos:weight="1"
  20. ohos:layout_alignment="horizontal_center"
  21. ohos:text_size="50"/>
  22. <Text
  23. ohos:height="match_content"
  24. ohos:width="match_content"
  25. ohos:text="text1"
  26. ohos:background_element="$graphic:background_ability_main"
  27. ohos:layout_alignment="horizontal_center"
  28. ohos:text_size="50"
  29. />
  30. </DirectionalLayout>

Demo
在这里插入图片描述

DependentLayout

这与安卓中的约束布局相似
在这个布局中可通过两种属性来规范组件位置

相对与同级组件的位置关系属性

ohos:above:处于指定组件之上
ohos:below:处于指定组件之下
ohos:left_of:处于指定组件之左
ohos:right_of:处于指定组件之右
ohos:right_of:处于指定组件之右
ohos:start_of:处于指定组件之前
ohos:end_of:处于指定组件之后

相对与父组件的位置关系属性

ohos:align_parent_left:处于父组件左侧
ohos:align_parent_right:处于父组件右侧
ohos:align_parent_start:处于父组件开始侧
ohos:align_parent_end:处于父组件结束侧
ohos:align_parent_top:处于父组件上侧
ohos:align_parent_bottom:处于父组件下侧
ohos:center_in_parent:处于父组件中间

例子

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <DependentLayout
  3. xmlns:ohos="http://schemas.huawei.com/res/ohos"
  4. ohos:height="match_parent"
  5. ohos:width="match_parent">
  6. <Text
  7. ohos:id="$+id:top"
  8. ohos:height="match_content"
  9. ohos:width="match_content"
  10. ohos:align_parent_top="true"
  11. ohos:center_in_parent="true"
  12. ohos:background_element="$graphic:background_ability_main"
  13. ohos:text="top"
  14. ohos:text_size="50"
  15. />
  16. <Text
  17. ohos:id="$+id:text_helloworld"
  18. ohos:height="match_content"
  19. ohos:width="match_content"
  20. ohos:below="$id:top"
  21. ohos:center_in_parent="true"
  22. ohos:background_element="$graphic:background_ability_main"
  23. ohos:text="below"
  24. ohos:text_size="50"
  25. />
  26. <Text
  27. ohos:id="$+id:text_helloworld"
  28. ohos:height="match_content"
  29. ohos:width="match_content"
  30. ohos:background_element="$graphic:background_ability_main"
  31. ohos:align_parent_bottom="true"
  32. ohos:center_in_parent="true"
  33. ohos:text="bottom"
  34. ohos:text_size="50"
  35. />
  36. </DependentLayout>

Demo
在这里插入图片描述

StackLayout

层叠布局,在层叠布局中先创建的组件会显示在后创建组件的下层,后创建的组件会覆盖先创建的组件,这样就可以实现一种悬浮的样式,类似于悬浮按钮

在这个布局下的可以通过ohos:layout_alignment属性来规范组件位置

例子

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <StackLayout
  3. xmlns:ohos="http://schemas.huawei.com/res/ohos"
  4. ohos:id="$+id:stack_layout"
  5. ohos:height="match_parent"
  6. ohos:width="match_parent">
  7. <Text
  8. ohos:id="$+id:text_blue"
  9. ohos:text_alignment="bottom|horizontal_center"
  10. ohos:text_size="24fp"
  11. ohos:text="txet1"
  12. ohos:height="400vp"
  13. ohos:width="400vp"
  14. ohos:background_element="#40FF00" />
  15. <Text
  16. ohos:id="$+id:text_light_purple"
  17. ohos:text_alignment="bottom|horizontal_center"
  18. ohos:text_size="24fp"
  19. ohos:text="text2"
  20. ohos:height="200vp"
  21. ohos:width="200vp"
  22. ohos:background_element="#2EFEF7" />
  23. <Text
  24. ohos:id="$+id:text_orange"
  25. ohos:text_alignment="center"
  26. ohos:text_size="24fp"
  27. ohos:text="text2"
  28. ohos:height="100vp"
  29. ohos:width="100vp"
  30. ohos:background_element="#FF0080" />
  31. <Button
  32. ohos:id="$+id:button"
  33. ohos:height="40vp"
  34. ohos:text="button"
  35. ohos:width="80vp"
  36. ohos:layout_alignment="right"
  37. ohos:background_element="#3399FF"/>
  38. </StackLayout>

Demo
在这里插入图片描述

总结

1.鸿蒙所采用的开发模式与安卓的开发模式是类似的,这有利于我们这些安卓开发者向鸿蒙开发转移
2.相较于安卓应用,鸿蒙开发的应用可以实现多平台共同开发及无缝的衔接,节省开发者对多平台的适配时间,有利多平台的开发

作者:张森
原文地址

发表评论

表情:
评论列表 (有 0 条评论,438人围观)

还没有评论,来说两句吧...

相关阅读

    相关 鸿蒙是不是

    先说结论:安卓和鸿蒙是截然不同的操作系统 论证: 安卓系统采用的是“宏”内核,而鸿蒙系统采用的是“微”内核,这也是它们之间最根本的区别。别看只有一字之差,所产生的结果却是天