当前位置:   article > 正文

前端开发 | 鸿蒙(HarmonyOS)应用开发——UI设计_鸿蒙前端开发文档

鸿蒙前端开发文档

掌握UI界面的基本布局和简单组件的使用,能完成登录UI界面的实现。

  • 涉及知识点
  1. 掌握Flex弹性布局
  2. 掌握容器组件Column、Row
  3. 掌握基础组件Text、TextInput、Button等
  • 准备
  1. 技能要求:建议具备基本的编程知识。
  2. 实验环境要求:
  3. 操作系统Windows
  4. 安装DevEco Studio

  • 详细过程

本实验,我们实现登录UI界面。最终效果如下:

步骤 1: 初始化项目

  1. 配置环境、安装编译器deveco

环境:下载nodejs、ohpm、sdk

新建空项目

2.在page目录下创建login.ets文件,在rawfile目录下创建image目录并导入所需图像资源

步骤 2: 设计登陆页面

1.整体结构:

struct Login 定义了一个名为 Login 的结构体或者组件。build() 方法内部描述了整个登录界面的布局结构。

2.布局组件:使用 Column() 和 Row() 组件来定义垂直和水平布局。

Column() {

  // 内部子组件

}

.width('100%')

.height('100%')

.justifyContent(FlexAlign.SpaceAround)

3.具体实现

  1. Column() {
  2.   Image($rawfile('images/pic0.png')).width(100).height(100)
  3.   Text('智慧论坛')
  4.     .fontSize(33)
  5.     .fontWeight(FontWeight.Bold)
  6.     .margin({ top: 40 })
  7.     .fontColor(0x317aff)
  8. }

在一个新的 Column 中添加一个图片和一个标题文字。使用 Image 组件加载本地图片 images/pic0.png,并设置宽高为 100 像素。使用 Text 组件显示标题 "智慧论坛"。设置字体大小为 33。设置字体为粗体。设置文字上方的外边距为 40。设置字体颜色为蓝色(十六进制颜色代码 0x317aff)。

效果展示:

包含了2个输入框(TextInput)、分隔线(Divider)、按钮(Button)和一些文本标签(Text)。输入框使用了不同的属性设置,如高度、边框半径、背景颜色等。每个输入框之间有一个占满宽度的分隔线。

一个 Row 包含了两个忘记密码和快速注册的文本,它们被放置在按钮下方,并采用了相应的样式。

按钮被定义为一个启动按钮,具有点击事件的处理函数。

  1. Column() {
  2.   TextInput({ placeholder: '请输入用户名/手机号/邮箱' })
  3.     .height(47)
  4.     .width('95%')
  5.     .borderRadius(10)
  6.     .type(InputType.Normal)
  7.     .backgroundColor(Color.White)
  8.   Divider().strokeWidth(1).width('100%')
  9.   TextInput({ placeholder: '请输入密码' })
  10.     .height(47)
  11.     .width('95%')
  12.     .borderRadius(10)
  13.     .type(InputType.Normal)
  14.     .backgroundColor(Color.White)
  15.   Row() {
  16.     Text('记住密码').fontColor('#4798fd')
  17.     Text('快速注册').fontColor('#4798fd')
  18.   }
  19.   .width('100%')
  20.   .justifyContent(FlexAlign.SpaceBetween)
  21.   Button('登陆', { type: ButtonType.Normal, stateEffect: true })
  22.     .borderRadius(8)
  23.     .backgroundColor(0x317aff)
  24.     .width('100%')
  25.     .onClick(() => {
  26.       console.log('ButtonType.Normal')
  27.     })
  28. }
  29. .width('94%')
  30. .height('25%')
  31. .justifyContent(FlexAlign.SpaceAround)

1TextInput 组件:

placeholder:设置输入框的提示文字。

height(47):设置输入框的高度为 47 像素。

width('95%'):设置输入框的宽度为父容器的 95%。

borderRadius(10):设置输入框的圆角半径为 10 像素。

type(InputType.Normal):设置输入框的类型为普通文本。

backgroundColor(Color.White):设置输入框的背景颜色为白色。

2Divider 组件:

strokeWidth(1):设置分割线的宽度为 1 像素。

width('100%'):设置分割线的宽度为父容器的 100%。

(3)记住密码和快速注册:

创建一个水平布局。

Text('记住密码').fontColor('#4798fd'):显示 "记住密码" 文本,字体颜色为蓝色。

Text('快速注册').fontColor('#4798fd'):显示 "快速注册" 文本,字体颜色为蓝色。

.width('100%'):设置 Row 的宽度为父容器的 100%。

.justifyContent(FlexAlign.SpaceBetween):子组件之间的间距为均匀分布。

4Button 组件:

Button('登陆', { type: ButtonType.Normal, stateEffect: true }):创建一个按钮,显示文字 "登陆"。

.borderRadius(8):设置按钮的圆角半径为 8 像素。

.backgroundColor(0x317aff):设置按钮的背景颜色为蓝色。

.width('100%'):设置按钮的宽度为父容器的 100%。

.onClick(() => { console.log('ButtonType.Normal') }):设置按钮的点击事件,点击时输出日志。

5布局:

.width('94%') 和 .height('25%'):设置 Column 的宽度为父容器的 94%,高度为 25%。

.justifyContent(FlexAlign.SpaceAround):子组件之间的间距为均匀分布。

效果展示:

(三)

  1. Column() {
  2.   Row() {
  3.     Divider().strokeWidth(1).width('30%')
  4.     Text('其它方式登陆')
  5.     Divider().strokeWidth(1).width('30%')
  6.   }
  7.   .width('100%')
  8.   .justifyContent(FlexAlign.SpaceBetween)
  9.   Row() {
  10.     Image($rawfile('images/pic1.png')).width(30).height(30)
  11.     Image($rawfile('images/pic2.png')).width(30).height(30)
  12.     Image($rawfile('images/pic3.png')).width(30).height(30)
  13.     Image($rawfile('images/pic4.png')).width(30).height(30)
  14.     Image($rawfile('images/pic5.png')).width(30).height(30)
  15.   }
  16.   .width('70%')
  17.   .justifyContent(FlexAlign.SpaceEvenly)}

包含 "其它方式登陆" 文本及两侧的分割线。包含多个小图标,用于展示其他登录方式。

Divider 分割线,宽度为父容器的 30%,线宽为 1 像素。

Text('其它方式登陆'):显示文字 "其它方式登陆"。

.width('100%'):宽度为父容器的 100%。

.justifyContent(FlexAlign.SpaceBetween):子组件之间的间距均匀分布。

效果展示:

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/从前慢现在也慢/article/detail/981669
推荐阅读
相关标签
  

闽ICP备14008679号