当前位置: 首页 > news >正文

HarmonyOS NEXT仓颉开发语言实战案例:动态广场

大家好,今日要分享的是使用仓颉语言开发动态广场页面,也比较像朋友圈页面:

整个页面分为两部分,分别是导航栏和状态列表,导航栏比较简单,我们可以先写下导航栏的具体代码和页面的基本结构:

Column{Row(10){Text('推荐').fontColor(Color.BLACK).fontSize(17).fontWeight(FontWeight.Bold)Text('关注').fontColor(Color.GRAY).fontSize(16)}.width(100.percent).height(60).justifyContent(FlexAlign.Center)List(space:10){}.width(100.percent).layoutWeight(1).backgroundColor(Color(247, 247, 247))
}
.width(100.percent)
.height(100.percent)

这样的话导航栏和列表容器撑满了整个页面,接下来的工作就是开发状态列表。

这里的内容也分为个人信息、状态内容和图片列表几部分,整个的布局方式是纵向的,要注意其中个人信息部分头像和名字时间是横向布局,这个比较简单。还有图片列表,我使用的方案是Grid,这样能够快速适配不同数量的图片。

话不多说,来看看代码怎么实现。对于状态列表,我们首先要定义数据结构:

public class RowItem{private let name: String;private let time: String;private let cover: CJResource;private let content: String;private let images : ArrayList<CJResource>;public RowItem(name:String, time:String,cover:CJResource,content:String,images:ArrayList<CJResource>){this.name = namethis.content = contentthis.time = timethis.images = imagesthis.cover = cover}public func getName():String{return this.name}public func getContent():String{return this.content}public func getTime():String{return this.time}public func getCover():CJResource{return this.cover}public func getImages():ArrayList<CJResource>{return this.images}
}

我们今天不涉及网络请求,直接在本地定义数组:

@State var rowList:ArrayList<RowItem> = ArrayList<RowItem>(RowItem('Tom','7小时前',@r(app.media.icon1),'美丽的风景',ArrayList<CJResource>([@r(app.media.fj1),@r(app.media.fj2),@r(app.media.fj3)])),RowItem('PLANK','10小时前',@r(app.media.icon2),'晨跑,空气很清新,顺便用个早餐',ArrayList<CJResource>([@r(app.media.cp1)]))
)

最后在List容器中循环遍历实现列表:

List(space:10){ForEach(rowList, itemGeneratorFunc: {item: RowItem, index: Int64 =>ListItem{Column(10){Row(6){Image(item.getCover()).width(40).height(40).borderRadius(20)Column(4){Text(item.getName()).fontColor(Color.BLACK).fontSize(15)Text(item.getTime()).fontColor(Color.GRAY).fontSize(15)}.alignItems(HorizontalAlign.Start)}Text('美丽的风景').fontSize(18).fontColor(Color.BLACK).margin(top:3)Grid {ForEach(item.getImages(), itemGeneratorFunc: {img:CJResource,tag:Int64 =>GridItem{Image(img).width(112).height(112).borderRadius(8).onClick({e =>imglist = item.getImages()dialogController.open()})}})}.width(100.percent).columnsTemplate('1fr 1fr 1fr').columnsGap(12).rowsGap(12).backgroundColor(0xFFFFFF)}.padding(12).alignItems(HorizontalAlign.Start).backgroundColor(Color.WHITE)}})
}
.width(100.percent)
.layoutWeight(1)
.backgroundColor(Color(247, 247, 247))

今天的内容就是这样,感谢阅读。##HarmonyOS语言##仓颉##休闲娱乐#

http://www.lqws.cn/news/560719.html

相关文章:

  • Java面试题030:一文深入了解MySQL(2)
  • SpringMVC系列(六)(Restful架构风格(中))
  • Python助力自动驾驶:深度学习模型优化全攻略
  • 什么是 PoS(权益证明)
  • 如何用VS Code、Sublime Text开发51单片机
  • uni-app subPackages 分包加载:优化应用性能的利器
  • Geollama 辅助笔记:raw_to_prompt_strings_geo.py
  • IDEA2024.3 tomcat需要按两次停止按钮停止问题
  • 区块链使用那些技术?
  • 太速科技-670-3U VPX PCIe桥扩展3路M.2高速存储模块
  • Linux测试是否能联网
  • 大事件项目记录8-文章分类接口开发-文章分类列表
  • 2025年健康医疗大数据开放共享:现状、挑战与未来发展
  • 计算机操作系统(十七)内存管理
  • Grab×亚矩阵云手机:以“云端超级节点”重塑东南亚出行与数字生活生态
  • 用鸿蒙打造真正的跨设备数据库:从零实现分布式存储
  • 【AI智能体】Dify 核心组件从使用到实战操作详解
  • 信号处理学习——文献精读与code复现之TFN——嵌入时频变换的可解释神经网络(上)
  • 数据湖 vs 数据仓库:数据界的“自来水厂”与“瓶装水厂”?
  • 阿里 Qwen3 模型更新,吉卜力风格get
  • 对话式数据分析与Text2SQL Agent产品可行性分析思考
  • 安卓中静态和动态添加子 View 到容器
  • Zotero 7 插件:翻译与护眼主题
  • 如何快速学习一门新编程语言
  • 使用asyncio构建高性能网络爬虫
  • Vue 项目中 Excel 导入导出功能笔记
  • 开疆智能CCLinkIE转ModbusTCP网关连接傲博机器人配置案例
  • 道路交通标志检测数据集-智能地图与导航 交通监控与执法 智慧城市交通管理-2,000 张图像
  • 鸿蒙5:ArkTS基本介绍
  • Ubuntu20.04离线安装Realtek b852无线网卡驱动