第 8 节:复杂布局 RelativeContainer / Grid / List / Scroll
学习目标
- 用 RelativeContainer 表达「这个贴那个边」的不规则相对布局。
- 用 GridRow/GridCol 栅格做手机一列、平板三列的响应式布局。
- 用 List/Grid 做可滚动列表与宫格,并解决「List 没有高度不显示」的坑。
- 用 Scroll 实现「头部底部固定、中间滚动」的标准结构。
一、引入:流式布局不够用时怎么办
Row/Column 是流式的——子元素按顺序排。但有些界面需要「这个元素贴在那个元素右边」「两个元素中心对齐」,套三层 Row/Column 才能勉强实现;还有些界面要滚动、要宫格、要随屏幕宽度换列。这一节把剩下几个核心容器讲完,最后给你一张选型表。
二、核心概念与正文
2.1 RelativeContainer:相对约束布局
给每个子元素一个 id,再用 alignRules 声明它相对谁的哪条边对齐。__container__ 是容器自身的保留 id。
@Entry
@ComponentV2
struct RelativeDemo {
build() {
RelativeContainer() {
Text('A')
.id('a').width(80).height(60)
.backgroundColor('#FDD').textAlign(TextAlign.Center)
.alignRules({
top: {
anchor: '__container__', align: VerticalAlign.Top },
start: {
anchor: '__container__', align: HorizontalAlign.Start }
})
Text('B')
.id('b').width(80).height(60)
.backgroundColor('#DFD').textAlign(TextAlign.Center)
.alignRules({
top: {
anchor: 'a', align: VerticalAlign.Bottom }, // B 顶边贴 A 底边
start: {
anchor: 'a', align: HorizontalAlign.Start }
})
Text('C')
.width(80).height(60)
.backgroundColor('#DDF').textAlign(TextAlign.Center)
.alignRules({
center: {
anchor: 'b', align: VerticalAlign.Center }, // C 垂直中心对齐 B
end: {
anchor: '__container__', align: HorizontalAlign.End }
})
}
.width('100%').height(220).backgroundColor('#FAFAFA')
}
}
alignRules 字段读法:top/bottom/start/end/center/baseline 是「我自己的哪条边」,anchor 是「相对谁」(对方 id),align 是「贴到对方哪条边」。top: { anchor: 'a', align: VerticalAlign.Bottom } 读作「我的顶边贴着 a 的底边」。
注意:RelativeContainer 适合「不规则、有明确相对关系」的布局(如商品卡片里价格、原价、标签、按钮四角分布);常规列表、表单、导航栏用 Row/Column 更直观,不要为了「高级」而用它。
2.2 GridRow / GridCol:响应式栅格
栅格把一行均匀分成 N 列(通常 12 列),每个 GridCol 声明自己在不同断点下占几列,一套代码适配手机/平板:
@Entry
@ComponentV2
struct GridRowDemo {
@Local items: string[] = ['卡片一', '卡片二', '卡片三', '卡片四', '卡片五', '卡片六'];
@Builder
card(text: string) {
Text(text)
.width('100%').height(100)
.backgroundColor('#EFEFEF').borderRadius(8)
.textAlign(TextAlign.Center)
}
build() {
Column() {
GridRow({
columns: 12, gutter: 12 }) {
ForEach(this.items, (item: string) => {
GridCol({
span: {
xs: 12, sm: 6, md: 4, lg: 3 } }) {
this.card(item)
}
})
}
.width('100%')
}
.width('100%').padding(12)
}
}
运行效果:手机上(xs)每张占满 12 列即一行一张;平板/折叠展开(md)每张占 4 列即一行三张。断点对应宽度档位:
| 断点 | 宽度范围(vp) | 典型设备 |
|---|---|---|
| xs | < 320 | 折叠屏外屏、小屏手机 |
| sm | ≥ 320 且 < 600 | 普通手机竖屏 |
| md | ≥ 600 且 < 840 | 折叠屏展开、竖屏平板 |
| lg | ≥ 840 | 横屏平板、2in1、智慧屏 |
@Builder 是 ArkUI 里复用一段 UI 描述的「函数」,可传参,写在 struct 内部;它不持有状态、不参与生命周期,纯布局片段——无状态小单元用 @Builder 比抽 struct 更轻。
2.3 List / Grid:列表与网格
List 是最常用的滚动列表容器,子元素必须是 ListItem;Grid 是网格,子元素是 GridItem,用 columnsTemplate 定义列数。二者都配合 ForEach 从数组生成子项。
@Entry
@ComponentV2
struct ListGridDemo {
@Local fruits: string[] = ['苹果', '香蕉', '橙子', '葡萄', '西瓜', '梨', '樱桃', '芒果'];
build() {
Column({
space: 12 }) {
// 普通列表
List({
space: 8 }) {
ForEach(this.fruits, (item: string, index: number) => {
ListItem() {
Row({
space: 12 }) {
Text(`${
index + 1}`)
.width(28).height(28).textAlign(TextAlign.Center)
.backgroundColor('#E8EFFF').fontColor('#0A59F7')
Text(item).fontSize(16).layoutWeight(1)
Text('›').fontColor('#999')
}
.width('100%').padding(12)
.backgroundColor(Color.White).borderRadius(8)
}
})
}
.layoutWeight(1) // ★ List 必须有明确高度或被权重撑开
.width('100%')
.backgroundColor('#F5F6F8')
}
.width('100%').height('100%').padding(12)
}
}
换成 Grid 就是三列宫格:
Grid() {
ForEach(this.fruits, (item: string) => {
GridItem() {
Text(item)
.width('100%').height(80)
.backgroundColor(Color.White).borderRadius(8)
.textAlign(TextAlign.Center)
}
})
}
.columnsTemplate('1fr 1fr 1fr') // 三列,每列等宽
.rowsGap(8).columnsGap(8)
.layoutWeight(1).width('100%')
List 常用属性:.scrollBar(BarState.Off) 关滚动条;.divider({ strokeWidth: 1, color: '#EEE' }) 加分隔线;.scroller(this.scroller) 绑控制器做「滚动到顶部」。数据上百行时把 ForEach 换成 LazyForEach,只创建可见区域的 ListItem 并回收复用。
2.4 Scroll:单向滚动容器
内容不是结构化列表(长图文、长表单)时,用 Scroll 包一个 Column/Row,一次只滚一个方向。
@Entry
@ComponentV2
struct ScrollDemo {
@Local lines: number[] = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
build() {
Column() {
// 顶部固定标题
Text('长文章标题').fontSize(20).fontWeight(FontWeight.Bold)
// 下面这部分可滚动
Scroll() {
Column({
space: 12 }) {
ForEach(this.lines, (n: number) => {
Text(`第 ${
n} 段正文:ArkUI 用 Column、Row、Stack 等容器描述界面,状态变化时框架自动刷新。`)
.width('100%').padding(12)
.backgroundColor(Color.White).borderRadius(8)
})
}
.width('100%')
}
.layoutWeight(1) // ★ 同样要给滚动区域高度
.width('100%')
.scrollable(ScrollDirection.Vertical)
.edgeEffect(EdgeEffect.Spring) // 边缘回弹
// 底部固定按钮
Button('回到顶部').width('100%')
}
.width('100%').height('100%').padding(12)
}
}
运行效果:顶部标题和底部按钮固定,中间十段文字可上下滑动,边缘有回弹。这就是「头部/底部固定 + 中间滚动区」的标准结构。注意 Scroll 里不要再放 List(除非真要嵌套滚动);重复同质行项用 List(有懒加载和复用),复杂整块内容用 Scroll。
三、完整示例:九大容器选型表
收尾用这张表,什么场景用哪个一目了然:
| 容器 | 排列方式 | 典型场景 | 关键参数 |
|---|---|---|---|
| Row | 水平线性 | 导航栏、标签行、按钮组 | justifyContent + alignItems(VerticalAlign) |
| Column | 垂直线性 | 表单、卡片内容、页面骨架 | justifyContent + alignItems(HorizontalAlign) |
| Stack | 层叠 | 封面+标题、按钮浮层、蒙层 | alignContent 九方向 |
| Flex | 可换行弹性 | 标签流、可换行按钮 | direction + wrap(FlexWrap.Wrap) |
| RelativeContainer | 相对约束 | 四角分布、不规则位置 | id + alignRules({ anchor, align }) |
| GridRow/GridCol | 12 列栅格 | 多端响应式 | span: { xs, sm, md, lg } |
| List | 可滚动列表 | 消息流、设置项、聊天记录 | 必须给高度;ListItem 子项 |
| Grid | 二维网格 | 相册、宫格入口 | columnsTemplate('1fr 1fr') |
| Scroll | 单向滚动 | 长文章、长表单 | scrollable + edgeEffect |
四、踩坑提示
⚠️ 坑 1:List 不显示或报 height is not set。 List 是可滚动容器,自己不知道多高。裸放在 Column 里不给高度也不给 layoutWeight,要么不显示要么报错。解法:外层给
.layoutWeight(1),或 List 自己.height('100%')。⚠️ 坑 2:Column 里套 List 导致页面错乱。 Column 垂直方向把子元素高度加起来,无高度的 List 会「无限高」。正确结构:Column 里放一个
.layoutWeight(1)的 List,固定元素排前后。⚠️ 坑 3:Scroll 里再嵌 List。 双重滚动会让手势冲突,除非刻意要「列表里再滚」。同质行用 List,整块复杂内容用 Scroll。
⚠️ 坑 4:栅格 span 写了 xs 却在平板上不变。 断点按宽度档位触发,不按设备型号;确认你测试窗口的实际宽度落在哪个档。
五、小结
- RelativeContainer 用 id + alignRules 表达不规则相对关系,但别为高级而用它。
- GridRow/GridCol 把一行分 12 列,按 xs/sm/md/lg 断点声明 span,一套代码多端换列。
- List 配 ListItem、Grid 配 GridItem,二者都必须给高度(layoutWeight 或固定)。
- Scroll 实现「头尾固定 + 中间滚动」,边缘用 EdgeEffect.Spring。
- 90% 界面:Row/Column 搭骨架,List/Scroll 承载长内容,Stack 做层叠,GridRow 做多端。
六、思考题 / 实践任务
- 动手任务:用 List 做一个待办清单,每项是「序号 + 标题 + 右侧删除按钮」,数据存在
@Local todos: string[]。 - 动手任务:把上面的宫格改成
columnsTemplate('1fr 1fr 1fr')三列,再试两列、四列,观察变化。 - 实践任务:做一个页面:顶部固定搜索栏、中间 List 滚动、底部固定「全选」按钮,体会 layoutWeight 如何分配高度。
页面能看了,但数据一变界面不刷新、跨组件传值一团乱——这正是接下来「状态管理」要根治的。下一节先认识 V1 那套 @State/@Prop/@Link/@Provide,理解它解决了什么、又留下了什么坑。