HarmonyOS应用开发实战萌宠日记 - Grid 网格布局实现照片墙前言照片墙是相册页的核心展示区域它以网格布局展示所有照片。在萌宠日记的 AlbumPage 中我们使用Grid 组件实现3 列照片网格每张照片以正方形比例展示利用aspectRatio(1)属性确保宽高比为 1:1。本文将从萌宠日记的 Grid 照片墙实现出发深入解析 Grid 组件的 columnsTemplate、rowsGap、columnsGap 等属性配置以及照片墙的布局和响应式设计。一、Grid 照片墙完整实现// AlbumPage.ets — 照片墙 Grid() { ForEach([0, 1, 2, 3, 4, 5], (index: number) { GridItem() { Column() { Text() .fontSize(32) } .width(100%) .aspectRatio(1) // 正方形比例 .backgroundColor(#FFF3E0) .borderRadius(8) .justifyContent(FlexAlign.Center) } }) } .columnsTemplate(1fr 1fr 1fr) // 3 列等宽 .rowsGap(8) // 行间距 8vp .columnsGap(8) // 列间距 8vp .width(100%)二、Grid 核心属性属性值说明columnsTemplate1fr 1fr 1fr3 列等宽rowsGap8行间距 8vpcolumnsGap8列间距 8vpwidth100%撑满容器三、GridItem 设计3.1 正方形比例GridItem() { Column() { Text().fontSize(32) } .width(100%) .aspectRatio(1) // 关键宽高比 1:1 }四、Grid vs layoutWeight对比GridRow layoutWeight适用场景多行多列网格单行等分布局自动换行支持不支持间距控制rowsGap columnsGapspace五、总结本文从萌宠日记的Grid 照片墙实现出发深入解析了网格布局的完整方案Grid 组件3 列等宽网格GridItem正方形比例展示间距配置8vp 行列间距占位设计Emoji 浅橙背景Grid 组件是构建照片墙的最佳选择它提供了灵活的列数控制和间距管理。如果这篇文章对你有帮助欢迎点赞、收藏⭐、关注你的支持是我持续创作的动力相关资源Grid 组件https://developer.huawei.com/consumer/cn/doc/harmonyos-references/ts-container-gridGridItem 组件https://developer.huawei.com/consumer/cn/doc/harmonyos-references/ts-container-griditemaspectRatio 属性https://developer.huawei.com/consumer/cn/doc/harmonyos-references/ts-universal-attributes-aspect-ratio7.1 属性对比属性类型必填默认值说明widthLength否100%组件宽度heightLength否自适应组件高度paddingPadding否0内边距marginMargin否0外边距backgroundColorColor否透明背景颜色borderRadiusLength否0圆角半径shadowShadow否无阴影效果6、常见问题与解决方案6.1 布局问题在实际开发中开发者经常会遇到布局相关的常见问题组件不显示检查是否设置了正确的宽高或父容器是否限制了子组件大小布局错乱检查alignItems和justifyContent设置是否正确滚动失效确认 Scroll 组件的高度是否被正确限制提示布局问题通常可以通过borderWidth: 1临时添加边框来定位问题区域。6.2 性能问题列表卡顿使用 LazyForEach 替代 ForEach 实现懒加载频繁刷新合理使用状态管理装饰器避免不必要的组件重绘内存泄漏在页面销毁时及时释放资源和取消订阅// 懒加载示例 LazyForEach(this.dataSource, (item: DataItem) { ListItem() { Text(item.name).fontSize(16) } }, (item: DataItem) item.id.toString())7、最佳实践建议7.1 代码组织良好的代码组织可以显著提升项目的可维护性组件化设计将可复用的 UI 部分抽取为独立组件样式分离将常量和样式定义集中管理类型安全使用 interface 定义数据模型避免 any 类型7.2 性能优化减少不必要的重绘使用State时只声明需要响应式更新的变量合理使用布局避免过深的嵌套层级图片优化使用适当的图片格式和尺寸提示HarmonyOS 提供了一系列性能分析工具建议在开发过程中持续关注应用性能。8、与其他方案的对比8.1 方案对比对比维度本方案传统方案优势实现复杂度低中等更简洁维护成本低高更易维护扩展性好一般更灵活性能优秀良好更高效8.2 适用场景适合需要快速实现相关功能的场景不适合对性能有极致要求的场景需考虑其他方案总结本文从萌宠日记的实际开发案例出发深入解析了Grid网格布局实现照片墙的实现方案。通过本文的学习读者可以掌握以下核心知识点组件用法理解相关 ArkUI 组件的核心属性和使用场景布局技巧掌握常见的布局模式及其适用范围交互实现学习点击事件、状态管理等交互设计方案最佳实践了解实际项目中的工程化实践和性能考量在实际开发中建议根据具体业务需求灵活调整组件参数和布局结构以达到最佳的用户体验效果。如果这篇文章对你有帮助欢迎点赞、收藏⭐、关注你的支持是我持续创作的动力相关资源Text 组件https://developer.huawei.com/consumer/cn/doc/harmonyos-references/ts-basic-components-textColumn 容器https://developer.huawei.com/consumer/cn/doc/harmonyos-references/ts-container-columnRow 容器https://developer.huawei.com/consumer/cn/doc/harmonyos-references/ts-container-rowScroll 组件https://developer.huawei.com/consumer/cn/doc/harmonyos-references/ts-container-scrollTabs 组件https://developer.huawei.com/consumer/cn/doc/harmonyos-references/ts-container-tabsNavigation 组件https://developer.huawei.com/consumer/cn/doc/harmonyos-references/ts-basic-components-navigationGrid 组件https://developer.huawei.com/consumer/cn/doc/harmonyos-references/ts-container-gridList 组件https://developer.huawei.com/consumer/cn/doc/harmonyos-references/ts-container-listTextInput 组件https://developer.huawei.com/consumer/cn/doc/harmonyos-references/ts-basic-components-textinputTextArea 组件https://developer.huawei.com/consumer/cn/doc/harmonyos-references/ts-basic-components-textarea6.1 完整示例代码以下是一个完整的实际应用示例展示了相关组件的综合使用// 完整的组件使用示例 Component export struct DemoComponent { State message: string Hello HarmonyOS State count: number 0 build() { Column({ space: 16 }) { Text(this.message) .fontSize(24) .fontWeight(FontWeight.Bold) .fontColor(#333333) Text(点击次数: ${this.count}) .fontSize(16) .fontColor(#666666) Button(点击增加) .fontSize(16) .fontColor(Color.White) .backgroundColor(#F5A623) .borderRadius(20) .onClick(() { this.count }) } .width(100%) .height(100%) .padding(24) .backgroundColor(#FFF8F0) } }6.2 状态管理代码// 状态管理示例 State isActive: boolean false Prop title: string Link sharedValue: number build() { Column() { Text(this.title) .fontSize(18) .fontColor(this.isActive ? #F5A623 : #999999) Button(this.isActive ? 取消 : 激活) .onClick(() { this.isActive !this.isActive }) } }6.3 数据绑定与渲染// ForEach 循环渲染示例 interface DataItem { id: number name: string icon: string } State dataList: DataItem[] [ { id: 1, name: 示例一, icon: ⭐ }, { id: 2, name: 示例二, icon: }, { id: 3, name: 示例三, icon: ✨ } ] build() { Column() { ForEach(this.dataList, (item: DataItem) { Row({ space: 12 }) { Text(item.icon).fontSize(24) Text(item.name).fontSize(16) } .padding(12) .width(100%) }) } }6.4 样式与主题配置// 样式配置示例 const ThemeStyles { primaryColor: #F5A623, backgroundColor: #FFF8F0, cardBackground: #FFFFFF, textPrimary: #333333, textSecondary: #999999, borderRadius: 12, spacing: 16 } Styles function CardStyle() { .backgroundColor(ThemeStyles.cardBackground) .borderRadius(ThemeStyles.borderRadius) .shadow({ radius: 4, color: #10000000, offsetY: 2 }) }
HarmonyOS应用开发实战:萌宠日记 - Grid 网格布局实现照片墙
HarmonyOS应用开发实战萌宠日记 - Grid 网格布局实现照片墙前言照片墙是相册页的核心展示区域它以网格布局展示所有照片。在萌宠日记的 AlbumPage 中我们使用Grid 组件实现3 列照片网格每张照片以正方形比例展示利用aspectRatio(1)属性确保宽高比为 1:1。本文将从萌宠日记的 Grid 照片墙实现出发深入解析 Grid 组件的 columnsTemplate、rowsGap、columnsGap 等属性配置以及照片墙的布局和响应式设计。一、Grid 照片墙完整实现// AlbumPage.ets — 照片墙 Grid() { ForEach([0, 1, 2, 3, 4, 5], (index: number) { GridItem() { Column() { Text() .fontSize(32) } .width(100%) .aspectRatio(1) // 正方形比例 .backgroundColor(#FFF3E0) .borderRadius(8) .justifyContent(FlexAlign.Center) } }) } .columnsTemplate(1fr 1fr 1fr) // 3 列等宽 .rowsGap(8) // 行间距 8vp .columnsGap(8) // 列间距 8vp .width(100%)二、Grid 核心属性属性值说明columnsTemplate1fr 1fr 1fr3 列等宽rowsGap8行间距 8vpcolumnsGap8列间距 8vpwidth100%撑满容器三、GridItem 设计3.1 正方形比例GridItem() { Column() { Text().fontSize(32) } .width(100%) .aspectRatio(1) // 关键宽高比 1:1 }四、Grid vs layoutWeight对比GridRow layoutWeight适用场景多行多列网格单行等分布局自动换行支持不支持间距控制rowsGap columnsGapspace五、总结本文从萌宠日记的Grid 照片墙实现出发深入解析了网格布局的完整方案Grid 组件3 列等宽网格GridItem正方形比例展示间距配置8vp 行列间距占位设计Emoji 浅橙背景Grid 组件是构建照片墙的最佳选择它提供了灵活的列数控制和间距管理。如果这篇文章对你有帮助欢迎点赞、收藏⭐、关注你的支持是我持续创作的动力相关资源Grid 组件https://developer.huawei.com/consumer/cn/doc/harmonyos-references/ts-container-gridGridItem 组件https://developer.huawei.com/consumer/cn/doc/harmonyos-references/ts-container-griditemaspectRatio 属性https://developer.huawei.com/consumer/cn/doc/harmonyos-references/ts-universal-attributes-aspect-ratio7.1 属性对比属性类型必填默认值说明widthLength否100%组件宽度heightLength否自适应组件高度paddingPadding否0内边距marginMargin否0外边距backgroundColorColor否透明背景颜色borderRadiusLength否0圆角半径shadowShadow否无阴影效果6、常见问题与解决方案6.1 布局问题在实际开发中开发者经常会遇到布局相关的常见问题组件不显示检查是否设置了正确的宽高或父容器是否限制了子组件大小布局错乱检查alignItems和justifyContent设置是否正确滚动失效确认 Scroll 组件的高度是否被正确限制提示布局问题通常可以通过borderWidth: 1临时添加边框来定位问题区域。6.2 性能问题列表卡顿使用 LazyForEach 替代 ForEach 实现懒加载频繁刷新合理使用状态管理装饰器避免不必要的组件重绘内存泄漏在页面销毁时及时释放资源和取消订阅// 懒加载示例 LazyForEach(this.dataSource, (item: DataItem) { ListItem() { Text(item.name).fontSize(16) } }, (item: DataItem) item.id.toString())7、最佳实践建议7.1 代码组织良好的代码组织可以显著提升项目的可维护性组件化设计将可复用的 UI 部分抽取为独立组件样式分离将常量和样式定义集中管理类型安全使用 interface 定义数据模型避免 any 类型7.2 性能优化减少不必要的重绘使用State时只声明需要响应式更新的变量合理使用布局避免过深的嵌套层级图片优化使用适当的图片格式和尺寸提示HarmonyOS 提供了一系列性能分析工具建议在开发过程中持续关注应用性能。8、与其他方案的对比8.1 方案对比对比维度本方案传统方案优势实现复杂度低中等更简洁维护成本低高更易维护扩展性好一般更灵活性能优秀良好更高效8.2 适用场景适合需要快速实现相关功能的场景不适合对性能有极致要求的场景需考虑其他方案总结本文从萌宠日记的实际开发案例出发深入解析了Grid网格布局实现照片墙的实现方案。通过本文的学习读者可以掌握以下核心知识点组件用法理解相关 ArkUI 组件的核心属性和使用场景布局技巧掌握常见的布局模式及其适用范围交互实现学习点击事件、状态管理等交互设计方案最佳实践了解实际项目中的工程化实践和性能考量在实际开发中建议根据具体业务需求灵活调整组件参数和布局结构以达到最佳的用户体验效果。如果这篇文章对你有帮助欢迎点赞、收藏⭐、关注你的支持是我持续创作的动力相关资源Text 组件https://developer.huawei.com/consumer/cn/doc/harmonyos-references/ts-basic-components-textColumn 容器https://developer.huawei.com/consumer/cn/doc/harmonyos-references/ts-container-columnRow 容器https://developer.huawei.com/consumer/cn/doc/harmonyos-references/ts-container-rowScroll 组件https://developer.huawei.com/consumer/cn/doc/harmonyos-references/ts-container-scrollTabs 组件https://developer.huawei.com/consumer/cn/doc/harmonyos-references/ts-container-tabsNavigation 组件https://developer.huawei.com/consumer/cn/doc/harmonyos-references/ts-basic-components-navigationGrid 组件https://developer.huawei.com/consumer/cn/doc/harmonyos-references/ts-container-gridList 组件https://developer.huawei.com/consumer/cn/doc/harmonyos-references/ts-container-listTextInput 组件https://developer.huawei.com/consumer/cn/doc/harmonyos-references/ts-basic-components-textinputTextArea 组件https://developer.huawei.com/consumer/cn/doc/harmonyos-references/ts-basic-components-textarea6.1 完整示例代码以下是一个完整的实际应用示例展示了相关组件的综合使用// 完整的组件使用示例 Component export struct DemoComponent { State message: string Hello HarmonyOS State count: number 0 build() { Column({ space: 16 }) { Text(this.message) .fontSize(24) .fontWeight(FontWeight.Bold) .fontColor(#333333) Text(点击次数: ${this.count}) .fontSize(16) .fontColor(#666666) Button(点击增加) .fontSize(16) .fontColor(Color.White) .backgroundColor(#F5A623) .borderRadius(20) .onClick(() { this.count }) } .width(100%) .height(100%) .padding(24) .backgroundColor(#FFF8F0) } }6.2 状态管理代码// 状态管理示例 State isActive: boolean false Prop title: string Link sharedValue: number build() { Column() { Text(this.title) .fontSize(18) .fontColor(this.isActive ? #F5A623 : #999999) Button(this.isActive ? 取消 : 激活) .onClick(() { this.isActive !this.isActive }) } }6.3 数据绑定与渲染// ForEach 循环渲染示例 interface DataItem { id: number name: string icon: string } State dataList: DataItem[] [ { id: 1, name: 示例一, icon: ⭐ }, { id: 2, name: 示例二, icon: }, { id: 3, name: 示例三, icon: ✨ } ] build() { Column() { ForEach(this.dataList, (item: DataItem) { Row({ space: 12 }) { Text(item.icon).fontSize(24) Text(item.name).fontSize(16) } .padding(12) .width(100%) }) } }6.4 样式与主题配置// 样式配置示例 const ThemeStyles { primaryColor: #F5A623, backgroundColor: #FFF8F0, cardBackground: #FFFFFF, textPrimary: #333333, textSecondary: #999999, borderRadius: 12, spacing: 16 } Styles function CardStyle() { .backgroundColor(ThemeStyles.cardBackground) .borderRadius(ThemeStyles.borderRadius) .shadow({ radius: 4, color: #10000000, offsetY: 2 }) }