HarmonyOS 用List组件实现组合列表项

devtools/2024/9/25 4:28:08/

界面分析:
由于整体UX设计图符合从上至下的布局,所以可以采用Column组件作为外层容器

简介:

  1. 最上方的标题可以使用Text组件进行加载渲染
  2. 中间的Banner图和简介分别可以使用Image组件和Text组件进行加载
  3. 最后一部分可以看作是一个列表,它由若干个列表项ListItem构成

在这里插入图片描述

实现标题、Banner、简介:

  1. 标题栏可以采用Text组件进行加载渲染
1. Column() {
2. Text('标题')
3. .fontFamily('HarmonyHeiTi-Bold')
4. .fontSize(24)
5. .fontColor(Color.Black)
6. .textAlign(TextAlign.Start)
7. .lineHeight(33)
8. .fontWeight(700)
9. .width('100%')
10. }
11. .padding({ top: 12, right: 16, bottom: 12, left: 16})
12. .backgroundColor('#F1F3F5')
  1. 放入图片资源:./src/main/resources/base/media目录中
  2. 使用Image组件加载放入的Banner图片,设置其宽度百分百,并设置圆角。同时注意到banner图与上方的title间有19单位的空隙,与下方的组件有8单位的空隙
1. Image($r('app.media.yourpicture'))
2. .width('100%')
3. .borderRadius(16)
4. .margin({ top: 19, bottom: 8 })
  1. 简介的部分也可以采用Text组件来进行数据的渲染
1. Text('简介')
2. .fontFamily('HarmonyHeiTi')
3. .fontSize(14)
4. .fontColor('rgba(0,0,0,0.60)')
5. .fontWeight(400)
6. .textAlign(TextAlign.Start)
  1. 实现List的列表项
// @Component功能更加多样,有自己的生命周期,还能支持预览效果
// 而@Builder更加轻量,能满足基础的组件封装,性能更好,但是不支持预览1. @Component
2. export struct Item {
3. build() {
4. Row(){6. }
7. .width('100%')
8. .height(48)
9. .borderRadius(16)
10. .alignItems(VerticalAlign.Center)
11. .padding({ left: 12, right: 12 })
12. .backgroundColor('#F1F3F5')
13. }
14. }
  1. 导出数据接口
1. export struct Item {
2. @State navBarItem: NavBarItemType = {
3. order: '01',
4. title: '标题'
5. };
6. ...
7. }
  1. 实现序号
1. @Component
2. export struct Item {
3. ...5. build() {
6. Row() {
7. Text(this.navBarItem.order)
8. .margin({ right: 6 })
9. .fontFamily('HarmonyHeiTi-Bold')
10. .fontSize(21)
11. .fontColor('#182431')
12. .textAlign(TextAlign.Start)
13. .lineHeight(22)
14. .fontWeight(700)
15. }
16. ...
17. }
18. }
  1. 实现标题
1. @Component
2. export struct Item {
3. ...5. build() {
6. Row() {
7. Text(this.navBarItem.order)
8. ...
9. Text(this.navBarItem.title)
10. .fontFamily('HarmonyHeiTi-Medium')
11. .fontSize(16)
12. .fontColor('#182431')
13. .textAlign(TextAlign.Start)
14. .lineHeight(22)
15. .fontWeight(500)
16. }18. }
19. }
  1. 将指向下一页的图标放置于 ./src/main/resources/base/media目录下,并填充
1. @Component
2. export struct Item {
3. ...5. build() {
6. Row() {
7. Text(this.navBarItem.order)
8. ...
9. Text(this.navBarItem.title)
10. ...
11. Blank()
12. Image($r('app.media.ic_arrow'))
13. .width(12)
14. .height(24)
15. }
16. ...
17. }
18. }

组合列表项:

  1. 定义数据集,在分栏Tab的首页进行定义
1. @State navBarList: NavBarItemType[] = [
2. { order: '01', title: '准备与学习' },
3. { order: '02', title: '构建应用' },
4. { order: '03', title: '应用测试' },
5. { order: '04', title: '上架' },
6. { order: '05', title: '运营增长' },
7. { order: '06', title: '商业变现' },
8. { order: '07', title: '更多' }
9. ];
  1. 循环渲染
1. List({ space: 12 }) {
2. ForEach(this.navBarList, (item: NavBarItemType, index: number) => {
3. ListItem() {
4. NavBarItem({ navBarItem: item })
5. }
6. .width('100%')
7. }, (item: NavBarItemType): string => item.title)
8. }
9. .width('100%')
10. .margin({ top: 24 })
  1. 在build()函数中,加入Scroll组件。可以解决当内容条数不足时,Scroll组件滚动时会出现空白区域的错误效果。edgeEffect用于设置边缘滑动效果,设置为EdgeEffect.Spring表示设置为弹性物理动效

http://www.ppmy.cn/devtools/88561.html

相关文章

图片转换之heic转jpg(使用ImageMagick)

缘由:iphone的图库,用jpg拍照保存后内存占比较大,heic格式会微缩不少。问题来了,电脑不能直接小图预览heic。 分析:现在就是解决小图预览的问题(大图用wps可以看) 解决:查找了一些…

距离-小米2023笔试(codefun2000)

题目链接 距离-小米2023笔试(codefun2000) 题目内容 在x轴上,给定当前坐标x0 ,以及若干个坐标及坐标的值xi :vali ,要求找到离x0最近的那个坐标并输出其值val。如果恰好在两个坐标的正中间,那么输出它们值的平均值。 输入描述 包…

《LeetCode热题100》---<5.③普通数组篇五道>

本篇博客讲解LeetCode热题100道普通数组篇中的五道题 第五道:缺失的第一个正数(困难) 第五道:缺失的第一个正数(困难) 方法一:将数组视为哈希表 class Solution {public int firstMissingPosi…

Android14音频进阶之使能内核debugfs:Adsp输出日志(七十九)

简介: CSDN博客专家,专注Android/Linux系统,分享多mic语音方案、音视频、编解码等技术,与大家一起成长! 新书发布:《Android系统多媒体进阶实战》🚀 优质专栏: Audio工程师进阶系列【原创干货持续更新中……】🚀 优质专栏: 多媒体系统工程师系列【原创干货持续更…

string用法总结

1.介绍 string是一个字符串类&#xff0c;和char类型类似&#xff0c;string是动态的&#xff0c;会自动调整大小&#xff0c;节省了不必要的空间。 1.初始化及定义 //头文件 #include<string>//1. string str1; //生成空字符串//2. string str2("123456789"…

C-V2X通信协议

C-V2X&#xff08;Cellular Vehicle to Everything&#xff0c;蜂窝车联网&#xff09;是一种利用蜂窝网络将车辆与一切事物连接的技术&#xff0c;它基于3G/4G/5G等蜂窝网通信技术演进形成&#xff0c;是当前车联网领域的主流技术标准之一。以下是对C-V2X通信协议的详细介绍&a…

UE5 UE4 使用python进行编辑器操作

使用UE 4.25以上版本后&#xff0c;python代码改动相对较少。 如下类库在4.20/21/22等早起版本不适用&#xff0c;建议查询UE的python文档 unreal.EditorAssetLibrary 1.获取当前选中的资源&#xff08;Content中&#xff09; # 获取当前选中的资产selected_assets unreal.E…

解读Solana流动性质押发展现状:市场格局的悄然转变

随着区块链技术的发展和去中心化金融&#xff08;DeFi&#xff09;生态系统的壮大&#xff0c;流动性质押&#xff08;Liquid Staking&#xff09;已经成为市场中的热门话题。尽管以太坊在这一领域占据了主导地位&#xff0c;但Solana也在快速追赶&#xff0c;并展现出其独特的…