第 7 节:常用布局 Row / Column / Stack / Flex
学习目标
- 用 Row/Column 搭出导航栏、列表项这类最常见的线性结构。
- 说清主轴、交叉轴、justifyContent、alignItems、layoutWeight 的含义。
- 用 Stack 做图片盖标题、按钮浮层这类层叠效果。
- 用 Flex 实现标签流自动换行。
一、引入:组件是「点」,布局是「面」
上一节的组件是点,这一节的布局是面。同样一组 Text 和 Image,用不同的容器包起来,就是列表、卡片、表单、宫格。90% 的界面都是 Row/Column 搭骨架,这一节先把最常用的四个线性/层叠容器讲透。
二、核心概念与正文
2.1 Row / Column:最常用的线性布局
Row 把子元素水平排列,Column 垂直排列。每个线性容器都有两条轴:
- 主轴:排列方向。Row 主轴是水平,Column 主轴是垂直,用
justifyContent(FlexAlign)对齐; - 交叉轴:与主轴垂直。Row 用
alignItems(VerticalAlign),Column 用alignItems(HorizontalAlign); - 间距:构造参数
{ space: 12 }在相邻子元素间均匀留 12vp,比手动加 margin 干净。
@Entry
@ComponentV2
struct RowColumnDemo {
build() {
Column({
space: 20 }) {
// 顶部导航:三个文字左右分布,垂直居中
Row() {
Text('← 返回')
Text('标题').fontWeight(FontWeight.Bold)
Text('编辑')
}
.width('100%')
.justifyContent(FlexAlign.SpaceBetween) // 主轴:左右两端贴边
.alignItems(VerticalAlign.Center) // 交叉轴:垂直居中
// 一排三个标签,均分
Row({
space: 12 }) {
Text('推荐').layoutWeight(1).backgroundColor('#FDD').textAlign(TextAlign.Center)
Text('热点').layoutWeight(1).backgroundColor('#DFD').textAlign(TextAlign.Center)
Text('关注').layoutWeight(1).backgroundColor('#DDF').textAlign(TextAlign.Center)
}
.width('100%')
}
.width('100%').height('100%').padding(16)
}
}
运行效果:第一行「返回」贴左、「编辑」贴右、标题居中;第二行三个等宽彩色标签各占三分之一。
FlexAlign 六个值的区别:SpaceBetween 两端贴边、中间均分;SpaceAround 每个元素左右外边距相等(首尾离边缘是中间间隔的一半);SpaceEvenly 所有间隔完全相等(含首尾到边缘),视觉上最「匀」。
2.2 layoutWeight:按权重分配剩余空间
layoutWeight(number) 在 Row/Column/Flex 里按权重分配主轴剩余空间,1 占一份、2 占两份。这是「列表项右边箭头被顶到最右」「左右分栏 1:2」的关键:
@Entry
@ComponentV2
struct LayoutWeightDemo {
build() {
Column({
space: 12 }) {
// 左右按 1:2 分配
Row({
space: 12 }) {
Text('左侧 1/3')
.layoutWeight(1).height(60)
.backgroundColor('#FDD').textAlign(TextAlign.Center)
Text('右侧 2/3')
.layoutWeight(2).height(60)
.backgroundColor('#DFD').textAlign(TextAlign.Center)
}
.width('100%')
}
.width('100%').padding(16)
}
}
注意:layoutWeight 只在父容器是 Row/Column/Flex 时生效;子元素不要再写死主轴方向的 width(写了也会被权重覆盖)。
真实业务里 90% 列表项的骨架是 Row 套 Column——左边图标、中间两行文字、右边箭头,中间 Column 用 layoutWeight(1) 吃掉剩余宽度:
Row({
space: 12 }) {
// 左:图标占位
Column().width(40).height(40).backgroundColor('#E8EFFF').borderRadius(8)
// 中:标题 + 副标题,纵向排
Column({
space: 4 }) {
Text('消息通知').fontSize(16).fontColor('#111')
Text('接收来自应用的推送').fontSize(12).fontColor('#999')
}
.alignItems(HorizontalAlign.Start)
.layoutWeight(1) // 吃掉中间剩余空间,把箭头挤到右边
// 右:箭头
Text('›').fontSize(18).fontColor('#CCC')
}
.width('100%').padding(12).backgroundColor(Color.White)
2.3 Stack:层叠布局
Stack 把子元素叠放,后写的压在上面。典型用途:图片上盖文字、按钮浮在角落、加载蒙层。构造参数 { alignContent: Alignment.BottomEnd } 决定子元素默认对齐方式。
@Entry
@ComponentV2
struct StackDemo {
build() {
Stack({
alignContent: Alignment.BottomEnd }) {
// 底层:封面图
Image($r('app.media.icon'))
.width('100%').height(180)
.objectFit(ImageFit.Cover).borderRadius(12)
// 中层:半透明黑色遮罩
Column()
.width('100%').height(60)
.backgroundColor('#66000000')
// 顶层:标题文字,靠左下
Text('山海有灵 · 封面故事')
.fontColor(Color.White).fontSize(16)
.padding({
left: 12, bottom: 8 })
}
.width('100%')
}
}
运行效果:圆角封面图底部压一层半透明黑条,白色标题贴左下角。alignContent 九个取值是「水平 × 垂直」组合:TopStart / Top / TopEnd、Start / Center / End、BottomStart / Bottom / BottomEnd。做「图片右下角小角标」就用它。
2.4 Flex:弹性布局,支持换行
Flex 和 Row 很像,但它支持换行(wrap: FlexWrap.Wrap),特别适合标签流、搜索历史、可换行按钮组:
@Entry
@ComponentV2
struct FlexDemo {
@Local tags: string[] = ['鸿蒙', 'ArkUI', '声明式', 'ArkTS',
'Stage模型', '状态管理', '布局', '实战'];
build() {
Flex({
direction: FlexDirection.Row,
wrap: FlexWrap.Wrap,
justifyContent: FlexAlign.Start,
alignItems: ItemAlign.Center
}) {
ForEach(this.tags, (tag: string) => {
Text(tag)
.fontSize(13).fontColor('#0A59F7')
.backgroundColor('#E8EFFF')
.padding({
left: 12, right: 12, top: 6, bottom: 6 })
.borderRadius(14)
.margin({
right: 8, bottom: 8 })
})
}
.width('100%').padding(16)
}
}
运行效果:一排蓝色圆角小标签,一行放不下自动折到下一行。这正是「搜索历史」「话题标签」该用的容器——Row 做不到换行,超出会被压缩或溢出。
2.5 容易混的尺寸控制 API
集中列几个:
.width(number / '100%'):固定或百分比尺寸;.constraintSize({ minWidth, maxWidth, minHeight, maxHeight }):约束尺寸,组件在范围内自适应但不越界;.aspectRatio(1):宽高比,配.width('30%')做出正方形图。
// 约束高度:内容少时至少 48,多时最多 80
Text('一段可能很长的说明文字,超出会被截断。')
.width('100%').padding(12).backgroundColor('#F5F6F8')
.constraintSize({
minHeight: 48, maxHeight: 80 })
.textOverflow({
overflow: TextOverflow.Ellipsis })
三、完整示例:一个图文卡片骨架
把 Row + 百分比 + aspectRatio + layoutWeight 组合成一个自适应图文条目:
@Entry
@ComponentV2
struct AdaptiveCard {
build() {
Column() {
Row({
space: 12 }) {
// 图:宽度父容器 30%,高宽比 1:1(正方形)
Image($r('app.media.icon'))
.width('30%').aspectRatio(1)
.borderRadius(8).objectFit(ImageFit.Cover)
// 文:剩下 70%,用 layoutWeight 吃掉
Column({
space: 6 }) {
Text('鸿蒙 ArkUI 布局实战')
.fontSize(16).fontWeight(FontWeight.Bold)
.maxLines(1).textOverflow({
overflow: TextOverflow.Ellipsis })
Text('从 Row/Column 到栅格,一次讲透')
.fontSize(13).fontColor('#999')
.maxLines(1).textOverflow({
overflow: TextOverflow.Ellipsis })
}
.layoutWeight(1)
.alignItems(HorizontalAlign.Start)
}
.width('100%').padding(12)
.backgroundColor(Color.White).borderRadius(12)
}
.width('100%').padding(16)
}
}
运行效果:左边占 30% 宽的正方形缩略图,右边标题副标题占满剩余。手机上窄、平板上宽,图始终是 30%、文字始终是 70%,不会错位。
四、踩坑提示
⚠️ 坑 1:layoutWeight 和写死的 width 冲突。 子元素一旦
layoutWeight(1),主轴方向就别再写死 width,否则被权重覆盖,表现和预期不符。⚠️ 坑 2:用 Row 排标签却发现挤在一起。 Row 不换行,超出会压缩或溢出。标签流一定用 Flex +
FlexWrap.Wrap。⚠️ 坑 3:Stack 里想单独让某个元素靠右下。 别改 Stack 的 alignContent(那是所有子元素默认值),给该子元素单独加对齐,或用透明容器撑开。
⚠️ 坑 4:照设计稿写死 vp 像素。 设计稿是 375 宽就写
.width(375)在真机上必偏。正确做法是量比例——写.layoutWeight(1)或.width('50%'),vp 已帮你做密度换算。
五、小结
- Row 水平、Column 垂直;主轴用 justifyContent,交叉轴用 alignItems。
layoutWeight按权重分配剩余空间,是列表项分栏、箭头靠右的核心。- Stack 层叠、后写的在上,用 alignContent 控制九个方向对齐。
- Flex 支持换行,标签流/搜索历史用它。
- 多端友好三件套:
'100%'百分比、layoutWeight(1)、aspectRatio(1)。
六、思考题 / 实践任务
- 动手任务:用 Row 搭一个 App 顶部导航(左返回、中标题、右更多按钮),标题居中、两侧贴边。
- 动手任务:用 Stack 做一个头像,右下角叠一个红色小圆点(未读角标)。
- 实践任务:用 Flex 做一个可换行的「兴趣标签」选择区,点击标签切换选中态背景色。
线性和层叠布局够用了,但列表怎么滚动、宫格怎么排、多端怎么自动换列?下一节讲 RelativeContainer、GridRow 栅格、List/Grid 列表和 Scroll 滚动容器,把复杂布局一次讲透。