公司动态
HarmonyOS7 GridRow 与 GridCol 构建响应式 12 列栅格
文章目录前言代码讲解页面入口怎么安排数据模型先稳住状态变量控制页面反馈布局代码不要从样式开始读Builder 和方法承担复用使用方式完整代码关键代码解析interface 不是摆设State 是交互的开关列表和卡片要靠数据驱动事件回调要短一点样式参数别急着抽常量总结前言栅格和网格类页面的重点是节奏感。列宽、间距、选中态都会影响最终观感。我建议把这篇当成一个可以直接改造的 ArkUI 页面模板来看。先跑起来确认页面展示和交互都正常再把里面的模拟数据替换成接口数据最后再调整颜色、间距和业务字段。这样改起来比较稳不容易一边改样式一边把状态逻辑弄乱。这篇文章会按「页面意图、数据结构、状态流转、布局组织、交互细节」这条线来读代码。你不用从第一行样式硬看到最后先抓住主线后面的细节会轻松很多。代码讲解页面入口怎么安排代码里的入口组件是ResponsiveGridRowDemoPage。如果你把它当成单独页面预览保留Entry就可以如果要塞进现有工程的路由体系通常会去掉Entry再由外层页面或路由模块统一管理。这个习惯很重要。一个项目里入口页面太多预览时容易混后期拆组件也会麻烦。我的做法是案例阶段保留入口接业务时只保留真正需要被路由访问的页面。数据模型先稳住本案例涉及的数据模型主要有GridDemoItem、DashboardCard_mtfg、TabParams。这些interface的作用不是为了“看起来规范”而是给 UI 层划边界。比如列表项需要标题、描述、颜色、选中态就应该在模型里说清楚。后续从接口拿数据时可以在请求层做字段映射别让后端字段名直接污染页面代码。如果你准备接真实业务建议把模拟数组替换成接口返回后的 ViewModel。页面只关心自己要渲染什么不关心接口原始字段长什么样。状态变量控制页面反馈本案例里的关键状态包括currentExample。State的职责是让 UI 对用户操作有反馈。比如选中某个卡片、切换分类、展开内容、修改输入值、控制加载中状态这些都应该由状态驱动。状态一变相关 UI 自动刷新这也是 ArkUI 声明式开发最核心的体验。这里有个容易踩的坑不要把所有数据都塞进State。静态配置用普通数组就够了只有用户会改、接口会更新、页面需要重新渲染的数据才适合放进状态里。布局代码不要从样式开始读这个页面里比较关键的组件有Row横向排列、左右分栏、按钮组或卡片行都依赖它来控制水平方向的节奏。Column纵向组织标题、内容、操作区适合搭页面主骨架。GridRow适合做响应式栅格把页面拆成可配置的列。GridCol和 GridRow 搭配使用核心是 span、offset 这些列宽规则。Scroll用于长内容容器锚点跳转和返回顶部都离不开滚动控制。读布局时我更建议先找大容器再看内部如何拆块。比如一个页面通常会先用Column纵向放标题区、内容区、操作区内容区里再用Row、Grid、List或其他容器细分。这样看代码层级会比逐行看.padding()、.fontSize()清楚得多。如果页面显示错位优先检查容器关系而不是马上改像素。很多问题不是某个.margin()不对而是外层容器的宽高、滚动方向、权重分配或对齐方式没想清楚。Builder 和方法承担复用这篇没有额外拆出 Builder页面结构主要集中在build()中。辅助方法不多逻辑主要写在事件回调和渲染表达式里。Builder更适合复用 UI 片段比如卡片、列表行、头部区域、按钮组。普通方法更适合放计算逻辑比如筛选列表、统计数量、切换状态、生成颜色。两者分清楚后页面会干净很多。我不建议把所有东西都塞进build()。刚开始代码少还行需求一多build()会变成几百行后面改一个按钮都要找半天。使用方式把下面的完整代码放到 ArkTS 页面文件里即可运行。文章中的组件名已经是语义化命名不包含编号式案例名称接路由时直接使用ResponsiveGridRowDemoPage就行。如果你要改成业务页面优先改三处数据模型、状态变量、事件方法。样式可以后调先保证数据流和交互是通的。完整代码// ResponsiveGridRowDemoPage: 响应式栅格布局 - GridRow/GridCol 栅格系统 // 知识点: GridRow、GridCol、span/offset、断点响应式、栅格布局实战 interface GridDemoItem { demoId: number demoTitle: string demoSpan: number demoColor: string demoEmoji: string } interface DashboardCard_mtfg { cardId: number cardTitle: string cardValue: string cardUnit: string cardEmoji: string cardBg: string cardSpan: number trendUp: boolean trendValue: string } interface TabParams { val: number, label: string } Entry Component struct ResponsiveGridRowDemoPage { State currentExample: number 1 private basicItems: GridDemoItem[] [ { demoId: 1, demoTitle: span:12 (全宽), demoSpan: 12, demoColor: #3B82F6, demoEmoji: }, { demoId: 2, demoTitle: span:6 (半宽), demoSpan: 6, demoColor: #10B981, demoEmoji: }, { demoId: 3, demoTitle: span:6 (半宽), demoSpan: 6, demoColor: #F59E0B, demoEmoji: }, { demoId: 4, demoTitle: span:4 (1/3), demoSpan: 4, demoColor: #8B5CF6, demoEmoji: ▪️ }, { demoId: 5, demoTitle: span:4 (1/3), demoSpan: 4, demoColor: #EC4899, demoEmoji: ▪️ }, { demoId: 6, demoTitle: span:4 (1/3), demoSpan: 4, demoColor: #EF4444, demoEmoji: ▪️ }, { demoId: 7, demoTitle: span:3, demoSpan: 3, demoColor: #14B8A6, demoEmoji: ▫️ }, { demoId: 8, demoTitle: span:3, demoSpan: 3, demoColor: #6366F1, demoEmoji: ▫️ }, { demoId: 9, demoTitle: span:3, demoSpan: 3, demoColor: #F97316, demoEmoji: ▫️ }, { demoId: 10, demoTitle: span:3, demoSpan: 3, demoColor: #84CC16, demoEmoji: ▫️ }, ] private dashboardCards: DashboardCard_mtfg[] [ { cardId: 1, cardTitle: 今日收入, cardValue: 12,580, cardUnit: 元, cardEmoji: , cardBg: #F0F4FF, cardSpan: 6, trendUp: true, trendValue: 8.2% }, { cardId: 2, cardTitle: 新增用户, cardValue: 1,234, cardUnit: 人, cardEmoji: , cardBg: #F0FFF4, cardSpan: 6, trendUp: true, trendValue: 12.5% }, { cardId: 3, cardTitle: 订单总数, cardValue: 3,891, cardUnit: 单, cardEmoji: , cardBg: #FFFBF0, cardSpan: 4, trendUp: false, trendValue: -3.1% }, { cardId: 4, cardTitle: 转化率, cardValue: 23.4, cardUnit: %, cardEmoji: , cardBg: #FDF0FF, cardSpan: 4, trendUp: true, trendValue: 1.8% }, { cardId: 5, cardTitle: 退款率, cardValue: 1.2, cardUnit: %, cardEmoji: , cardBg: #FFF0F0, cardSpan: 4, trendUp: false, trendValue: 0.3% }, { cardId: 6, cardTitle: 月度综合评分, cardValue: 98.5, cardUnit: 分, cardEmoji: ⭐, cardBg: #F8F0FF, cardSpan: 12, trendUp: true, trendValue: 2.1分 }, ] build() { Column({ space: 0 }) { // 标题栏 Row({ space: 0 }) { Text(GridRow 栅格布局) .fontSize(18) .fontWeight(FontWeight.Bold) .fontColor(#1A1A1A) .layoutWeight(1) Text(12列系统) .fontSize(13) .fontColor(#FFFFFF) .padding({ left: 8, right: 8, top: 4, bottom: 4 }) .backgroundColor(#007DFF) .borderRadius(10) } .width(100%) .padding({ left: 16, right: 16, top: 16, bottom: 12 }) .backgroundColor(#FFFFFF) // 示例切换 Row({ space: 8 }) { ForEach([ { val: 1, label: 基础 span }, { val: 2, label: 数据看板 }, { val: 3, label: offset 偏移 } ], (tab:TabParams) { Text(tab.label) .fontSize(13) .fontColor(this.currentExample tab.val ? #FFFFFF : #666666) .padding({ left: 12, right: 12, top: 7, bottom: 7 }) .backgroundColor(this.currentExample tab.val ? #007DFF : #F5F5F5) .borderRadius(20) .onClick(() { this.currentExample tab.val }) }, (tab:TabParams) tab.val.toString()) } .padding({ left: 16, right: 16, top: 10, bottom: 10 }) .width(100%) .backgroundColor(#FFFFFF) .border({ width: { bottom: 1 }, color: #F0F0F0 }) Scroll() { Column({ space: 12 }) { // 示例1基础 span 演示 if (this.currentExample 1) { Column({ space: 12 }) { Text(12列栅格系统 - span 基础演示) .fontSize(15) .fontWeight(FontWeight.Bold) .fontColor(#1A1A1A) .width(100%) Text(GridRow 默认12列GridCol 通过 span 属性指定占用的列数所有列之和超过12则自动换行。) .fontSize(13) .fontColor(#888888) .lineHeight(20) .width(100%) GridRow({ columns: 12, gutter: { x: 8, y: 8 } }) { ForEach(this.basicItems, (item: GridDemoItem) { GridCol({ span: item.demoSpan }) { Row({ space: 6 }) { Text(item.demoEmoji) .fontSize(14) Text(item.demoTitle) .fontSize(12) .fontColor(#FFFFFF) .fontWeight(FontWeight.Medium) .maxLines(1) .textOverflow({ overflow: TextOverflow.Ellipsis }) } .width(100%) .height(44) .padding({ left: 10, right: 10 }) .backgroundColor(item.demoColor) .borderRadius(8) .justifyContent(FlexAlign.Center) .alignItems(VerticalAlign.Center) } }, (item: GridDemoItem) item.demoId.toString()) } .width(100%) } .width(100%) .padding(16) .backgroundColor(#FFFFFF) .borderRadius(12) } // 示例2数据看板 if (this.currentExample 2) { Column({ space: 12 }) { Text(数据看板 - 混合 span 实现不等宽布局) .fontSize(15) .fontWeight(FontWeight.Bold) .fontColor(#1A1A1A) .width(100%) GridRow({ columns: 12, gutter: { x: 10, y: 10 } }) { ForEach(this.dashboardCards, (card: DashboardCard_mtfg) { GridCol({ span: card.cardSpan }) { Column({ space: 8 }) { Row({ space: 0 }) { Text(card.cardEmoji) .fontSize(22) .width(40) .height(40) .textAlign(TextAlign.Center) .backgroundColor(card.cardBg) .borderRadius(10) .flexShrink(0) Column({ space: 2 }) { Text(card.cardTitle) .fontSize(12) .fontColor(#888888) Row({ space: 4 }) { Text(card.cardValue) .fontSize(card.cardSpan 12 ? 24 : 18) .fontWeight(FontWeight.Bold) .fontColor(#1A1A1A) Text(card.cardUnit) .fontSize(12) .fontColor(#AAAAAA) .alignSelf(ItemAlign.End) .margin({ bottom: 2 }) } .alignItems(VerticalAlign.Bottom) } .layoutWeight(1) .padding({ left: 10 }) .alignItems(HorizontalAlign.Start) Text(card.trendValue) .fontSize(12) .fontColor(card.trendUp ? #10B981 : #EF4444) .fontWeight(FontWeight.Bold) .padding({ left: 6, right: 6, top: 3, bottom: 3 }) .backgroundColor(card.trendUp ? #F0FFF4 : #FFF0F0) .borderRadius(6) .flexShrink(0) } .width(100%) .alignItems(VerticalAlign.Center) // 宽卡片底部进度条 if (card.cardSpan 12) { Column({ space: 4 }) { Row({ space: 0 }) { Text(月度完成进度) .fontSize(12) .fontColor(#888888) .layoutWeight(1) Text(98.5%) .fontSize(12) .fontColor(#007DFF) .fontWeight(FontWeight.Bold) } .width(100%) Row({ space: 0 }) { Row() .height(6) .width(98.5%) .backgroundColor(#007DFF) .borderRadius(3) } .width(100%) .height(6) .backgroundColor(#F0F0F0) .borderRadius(3) } } } .padding(12) .backgroundColor(#FFFFFF) .borderRadius(10) .shadow({ radius: 4, color: #0A000000, offsetY: 1 }) } }, (card: DashboardCard_mtfg) card.cardId.toString()) } .width(100%) } .width(100%) .padding(16) .backgroundColor(#F8F9FA) .borderRadius(12) } // 示例3offset 偏移 if (this.currentExample 3) { Column({ space: 12 }) { Text(GridCol offset 偏移演示) .fontSize(15) .fontWeight(FontWeight.Bold) .fontColor(#1A1A1A) .width(100%) Text(offset 设置 GridCol 的列偏移量可以实现居中、右对齐等特殊排版效果。) .fontSize(13) .fontColor(#888888) .lineHeight(20) .width(100%) GridRow({ columns: 12, gutter: { x: 8, y: 10 } }) { // 无偏移 GridCol({ span: 6, offset: 0 }) { Text(span:6 offset:0) .fontSize(12) .fontColor(#FFFFFF) .width(100%) .height(40) .textAlign(TextAlign.Center) .backgroundColor(#3B82F6) .borderRadius(8) } // offset 右对齐 GridCol({ span: 6, offset: 6 }) { Text(span:6 offset:6) .fontSize(12) .fontColor(#FFFFFF) .width(100%) .height(40) .textAlign(TextAlign.Center) .backgroundColor(#10B981) .borderRadius(8) } // 居中 GridCol({ span: 6, offset: 3 }) { Text(span:6 offset:3 (居中)) .fontSize(12) .fontColor(#FFFFFF) .width(100%) .height(40) .textAlign(TextAlign.Center) .backgroundColor(#F59E0B) .borderRadius(8) } // 小偏移 GridCol({ span: 4, offset: 1 }) { Text(span:4 offset:1) .fontSize(11) .fontColor(#FFFFFF) .width(100%) .height(40) .textAlign(TextAlign.Center) .backgroundColor(#8B5CF6) .borderRadius(8) } GridCol({ span: 4, offset: 2 }) { Text(span:4 offset:2) .fontSize(11) .fontColor(#FFFFFF) .width(100%) .height(40) .textAlign(TextAlign.Center) .backgroundColor(#EC4899) .borderRadius(8) } GridCol({ span: 8, offset: 2 }) { Text(span:8 offset:2左右各留1/12) .fontSize(12) .fontColor(#FFFFFF) .width(100%) .height(40) .textAlign(TextAlign.Center) .backgroundColor(#EF4444) .borderRadius(8) } } .width(100%) } .width(100%) .padding(16) .backgroundColor(#FFFFFF) .borderRadius(12) } } .padding({ left: 12, right: 12, top: 12, bottom: 24 }) } .scrollBar(BarState.Off) .layoutWeight(1) } .width(100%) .height(100%) .backgroundColor(#F5F6FA) } }关键代码解析interface不是摆设很多新手写 ArkUI 页面时会直接在ForEach里使用一堆临时对象。页面小的时候没问题但字段一多就很难知道每个字段到底用于哪里。本案例把数据结构提前声明出来读代码时能很快判断每个 UI 区块依赖哪些字段。后续如果接接口也建议保留这层结构。接口可以变页面模型尽量稳定。这样页面不会因为后端多返回一个字段、少返回一个字段就跟着大改。State是交互的开关本案例的交互反馈都围绕状态展开。点击、切换、输入、刷新这类行为本质上都是修改某个状态值然后让 UI 重新计算显示结果。写这类代码时有一个简单判断如果某个值变化后页面应该立刻变那它大概率应该是状态如果它只是配置项、静态文案、固定颜色就不要放进状态。列表和卡片要靠数据驱动只要页面里出现重复结构就应该优先想到数据数组加ForEach。这样新增一项、删除一项、调整顺序都只动数据不用复制粘贴一段 UI。这个案例的完整代码里已经把主要内容抽成数组或方法。你可以试着多加一条数据看页面是否能自然渲染出来。如果能说明结构是健康的如果加一条就要改很多 UI说明组件拆分还不够。事件回调要短一点.onClick()、onChange()这类回调里可以直接改状态但不建议堆太多逻辑。简单切换可以写在回调里复杂逻辑最好拆成方法。这样做有两个好处一是读页面结构时不会被业务逻辑打断二是后面要加埋点、请求接口、异常处理时有明确的位置可以改。样式参数别急着抽常量这个案例里有不少颜色、间距、圆角、字号。学习阶段直接写在组件链上反而更容易看出效果。等你确定页面风格稳定后再把主题色、通用间距、卡片圆角提到统一常量里。过早抽象会让示例代码变绕。先让页面清楚再谈工程化这是我比较推荐的顺序。总结这个案例真正值得学的不只是某个 ArkUI 组件怎么写而是一个页面如何从数据走到状态再走到布局和交互。你可以按这个顺序改造先替换数据模型再确认状态更新是否正确接着调整布局容器最后打磨样式细节。只要这条主线不乱页面规模变大也不会失控。如果后续继续扩展我会优先把可复用的卡片、列表行、筛选栏或控制区拆成独立组件。这样主页面只保留数据和流程代码会更接近真实项目里的写法。