仓颉 ArkUI 弹性布局(Flex)完全指南:主轴对齐、交叉轴对齐、换行与自适应拉伸实战
仓颉 ArkUI 弹性布局(Flex)完全指南:主轴对齐、交叉轴对齐、换行与自适应拉伸实战
弹性布局(Flex)是仓颉 ArkUI 声明式 UI 中最灵活的布局容器之一,用于对容器内子元素进行排列、对齐和剩余空间分配。本文基于当前仓库中的官方开发指南,结合 Flex 组件 API 参考 与 Flex 布局通用属性参考,系统讲解主轴/交叉轴概念、direction 布局方向、wrap 换行、justifyContent 主轴对齐、alignItems/alignSelf/alignContent 交叉轴对齐以及 flexBasis/flexGrow/flexShrink 自适应拉伸的完整用法。读完本文,你将能够在仓颉 ArkUI 应用中熟练搭建头部导航栏、页面框架与多行数据排列等弹性布局场景。
概述:Flex 解决什么问题
弹性布局(Flex)提供更加有效的方式对容器中的子元素进行排列、对齐和分配剩余空间。它常用于页面头部导航栏的均匀分布、页面框架的搭建、多行数据的排列等场景。
在 布局概述 中,弹性布局被定位为与线性布局(Row、Column)类似的布局方式,区别在于:
弹性布局默认能够使子组件压缩或拉伸。在子组件需要计算拉伸或压缩比例时优先使用此布局,可使得多个容器内子组件能有更好的视觉上的填充效果。
Flex 容器默认存在 主轴 与 交叉轴,子元素默认沿主轴排列。子元素在主轴方向的尺寸称为 主轴尺寸,在交叉轴方向的尺寸称为 交叉轴尺寸。理解这两个轴是掌握全部 Flex 属性的前提。
基本概念
- 主轴:Flex 组件布局方向的轴线,子元素默认沿着主轴排列。主轴开始的位置称为主轴起始点,结束位置称为主轴结束点。
- 交叉轴:垂直于主轴方向的轴线。交叉轴开始的位置称为交叉轴起始点,结束位置称为交叉轴结束点。
Flex 组件基础:构造函数与默认值
Flex 以弹性方式布局子组件,创建时通过命名参数一次性配置方向、换行与对齐策略。其构造签名(见 Flex 组件 API)为:
public init(direction!: ?FlexDirection = None, wrap!: ?FlexWrap = None,
justifyContent!: ?FlexAlign = None, alignItems!: ?ItemAlign = None,
alignContent!: ?FlexAlign = None, child!: () -> Unit = {=>})
各命名参数的默认值整理如下:
| 参数名 | 类型 | 默认值(初始值) | 说明 |
|---|---|---|---|
| direction | ?FlexDirection | FlexDirection.Row | 子组件在 Flex 容器上排列的方向,即主轴的方向 |
| wrap | ?FlexWrap | FlexWrap.NoWrap | Flex 容器是单行/列还是多行/列排列 |
| justifyContent | ?FlexAlign | FlexAlign.Start | 所有子组件在 Flex 容器主轴上的对齐格式 |
| alignItems | ?ItemAlign | ItemAlign.Start | 所有子组件在 Flex 容器交叉轴上的对齐格式 |
| alignContent | ?FlexAlign | FlexAlign.Start | 交叉轴有额外空间时多行内容的对齐方式,仅在 wrap 为 Wrap 或 WrapReverse 下生效 |
| child | () -> Unit | {=>} | 声明容器内的子组件 |
说明(来自组件 API 参考):Flex 组件在渲染时存在二次布局过程,因此在对性能有严格要求的场景下建议使用 Column、Row 代替。此外,Flex 组件主轴默认不设置时撑满父容器;主轴长度可设置为 auto 使 Flex 自适应子组件布局,自适应时长度受 constraintSize 属性以及父容器传递的最大最小长度限制,且 constraintSize 属性优先级更高。
所有示例均以 @Entry + @Component 结构声明页面,开头固定导入:
package ohos_app_cangjie_entry
import kit.ArkUI.*
import ohos.arkui.state_macro_manage.*
布局方向:direction 控制主轴
在弹性布局中,容器的子元素可以按照任意方向排列。通过设置 direction 参数决定主轴方向,从而控制子元素的排列方向。direction 取值来自 FlexDirection 枚举:
| 枚举值 | 主轴方向 | 子元素排布 |
|---|---|---|
| FlexDirection.Row(默认值) | 水平方向 | 子元素从起始端沿着水平方向开始排布 |
| FlexDirection.RowReverse | 水平方向 | 子元素从终点端沿着与 Row 相反的方向开始排布 |
| FlexDirection.Column | 垂直方向 | 子元素从起始端沿着垂直方向开始排布 |
| FlexDirection.ColumnReverse | 垂直方向 | 子元素从终点端沿着与 Column 相反的方向开始排布 |
Row:默认水平排列
package ohos_app_cangjie_entry
import kit.ArkUI.*
import ohos.arkui.state_macro_manage.*
@Entry
@Component
class EntryView {
func build() {
Flex(direction: FlexDirection.Row) {
Text('1')
.width(33.percent)
.height(50)
.backgroundColor(0xF5DEB3)
Text('2')
.width(33.percent)
.height(50)
.backgroundColor(0xD2B48C)
Text('3')
.width(33.percent)
.height(50)
.backgroundColor(0xF5DEB3)
}
.height(70)
.width(90.percent)
.padding(10)
.backgroundColor(0xAFEEEE)
}
}
RowReverse:水平反向排列
package ohos_app_cangjie_entry
import kit.ArkUI.*
import ohos.arkui.state_macro_manage.*
@Entry
@Component
class EntryView {
func build() {
Flex(direction: FlexDirection.RowReverse) {
Text('3')
.width(33.percent)
.height(50)
.backgroundColor(0xF5DEB3)
Text('2')
.width(33.percent)
.height(50)
.backgroundColor(0xD2B48C)
Text('1')
.width(33.percent)
.height(50)
.backgroundColor(0xF5DEB3)
}
.height(70)
.width(90.percent)
.padding(10)
.backgroundColor(0xAFEEEE)
}
}
Column:垂直排列
package ohos_app_cangjie_entry
import kit.ArkUI.*
import ohos.arkui.state_macro_manage.*
@Entry
@Component
class EntryView {
func build() {
Flex(direction: FlexDirection.Column) {
Text('1')
.width(100.percent)
.height(50)
.backgroundColor(0xF5DEB3)
Text('2')
.width(100.percent)
.height(50)
.backgroundColor(0xD2B48C)
Text('3')
.width(100.percent)
.height(50)
.backgroundColor(0xF5DEB3)
}
.height(70)
.width(90.percent)
.padding(10)
.backgroundColor(0xAFEEEE)
}
}
ColumnReverse:垂直反向排列
package ohos_app_cangjie_entry
import kit.ArkUI.*
import ohos.arkui.state_macro_manage.*
@Entry
@Component
class EntryView {
func build() {
Flex(direction: FlexDirection.ColumnReverse) {
Text('1')
.width(100.percent)
.height(50)
.backgroundColor(0xF5DEB3)
Text('2')
.width(100.percent)
.height(50)
.backgroundColor(0xD2B48C)
Text('3')
.width(100.percent)
.height(50)
.backgroundColor(0xF5DEB3)
}
.height(70)
.width(90.percent)
.padding(10)
.backgroundColor(0xAFEEEE)
}
}
布局换行:wrap 控制单行/多行
弹性布局分为单行布局和多行布局。默认情况下,Flex 容器中的子元素都排在一条线(又称“轴线”)上。wrap 属性控制当子元素主轴尺寸之和大于容器主轴尺寸时,Flex 是单行布局还是多行布局;在多行布局时,通过交叉轴方向确认新行排列方向。wrap 取值来自 FlexWrap 枚举:
| 枚举值 | 行为 |
|---|---|
| FlexWrap.NoWrap(默认值) | 不换行。如果子元素的宽度总和大于父元素的宽度,则子元素会被压缩宽度 |
| FlexWrap.Wrap | 换行,每一行子元素按照主轴方向排列 |
| FlexWrap.WrapReverse | 换行,每一行子元素按照主轴反方向排列 |
从枚举定义看,NoWrap 模式下子元素尽可能约束在容器内,但当子元素有最小尺寸约束等设置时,Flex 容器不会对其强制弹性压缩;Wrap 模式下子项允许超出容器。
NoWrap:不换行、压缩子元素
package ohos_app_cangjie_entry
import kit.ArkUI.*
import ohos.arkui.state_macro_manage.*
@Entry
@Component
class EntryView {
func build() {
Flex(wrap: FlexWrap.NoWrap) {
Text('1')
.width(50.percent)
.height(50)
.backgroundColor(0xF5DEB3)
Text('2')
.width(50.percent)
.height(50)
.backgroundColor(0xD2B48C)
Text('3')
.width(50.percent)
.height(50)
.backgroundColor(0xF5DEB3)
}
.width(90.percent)
.padding(10)
.backgroundColor(0xAFEEEE)
}
}
三个子元素各占 50%,总和超过容器宽度,NoWrap 模式下子元素宽度会被压缩以适配容器。
Wrap:按主轴方向换行
package ohos_app_cangjie_entry
import kit.ArkUI.*
import ohos.arkui.state_macro_manage.*
@Entry
@Component
class EntryView {
func build() {
Flex(wrap: FlexWrap.Wrap) {
Text('1')
.width(50.percent)
.height(50)
.backgroundColor(0xF5DEB3)
Text('2')
.width(50.percent)
.height(50)
.backgroundColor(0xD2B48C)
Text('3')
.width(50.percent)
.height(50)
.backgroundColor(0xD2B48C)
}
.width(90.percent)
.padding(10)
.backgroundColor(0xAFEEEE)
}
}
前两个子元素占满第一行(各 50%),第三个子元素自动换到第二行,且每一行内的子元素仍然按照主轴方向排列。
WrapReverse:换行且新行反向
package ohos_app_cangjie_entry
import kit.ArkUI.*
import ohos.arkui.state_macro_manage.*
@Entry
@Component
class EntryView {
func build() {
Flex(wrap: FlexWrap.WrapReverse) {
Text('1')
.width(50.percent)
.height(50)
.backgroundColor(0xF5DEB3)
Text('2')
.width(50.percent)
.height(50)
.backgroundColor(0xD2B48C)
Text('3')
.width(50.percent)
.height(50)
.backgroundColor(0xF5DEB3)
}
.width(90.percent)
.padding(10)
.backgroundColor(0xAFEEEE)
}
}
WrapReverse 模式下,新生成的每一行按照主轴反方向排列,适合需要从下往上或从右往左填充的场景。
主轴对齐方式:justifyContent
通过 justifyContent 参数设置子元素在主轴方向的对齐方式,取值来自 FlexAlign 枚举:
| 枚举值 | 效果 |
|---|---|
| FlexAlign.Start(默认值) | 子元素在主轴方向起始端对齐,第一个子元素与父元素边沿对齐,其他元素与前一个元素对齐 |
| FlexAlign.Center | 子元素在主轴方向居中对齐 |
| FlexAlign.End | 子元素在主轴方向终点端对齐,最后一个子元素与父元素边沿对齐,其他元素与后一个元素对齐 |
| FlexAlign.SpaceBetween | 主轴方向均匀分配弹性元素,相邻子元素之间距离相同,第一个子元素和最后一个子元素与父元素边沿对齐 |
| FlexAlign.SpaceAround | 主轴方向均匀分配弹性元素,相邻子元素之间距离相同,第一个子元素到主轴起始端的距离和最后一个子元素到主轴终点端的距离是相邻元素之间距离的一半 |
| FlexAlign.SpaceEvenly | 主轴方向元素等间距布局,相邻子元素之间的间距、第一个子元素与主轴起始端的间距、最后一个子元素到主轴终点端的间距均相等 |
这六种对齐方式的示例代码结构一致,仅 justifyContent 参数不同,下面逐一给出:
Start(默认值)
package ohos_app_cangjie_entry
import kit.ArkUI.*
import ohos.arkui.state_macro_manage.*
@Entry
@Component
class EntryView {
func build() {
Flex(justifyContent: FlexAlign.Start) {
Text('1')
.width(20.percent)
.height(50)
.backgroundColor(0xF5DEB3)
Text('2')
.width(20.percent)
.height(50)
.backgroundColor(0xD2B48C)
Text('3')
.width(20.percent)
.height(50)
.backgroundColor(0xF5DEB3)
}
.width(90.percent)
.padding(top: 10, bottom: 10)
.backgroundColor(0xAFEEEE)
}
}
Center
package ohos_app_cangjie_entry
import kit.ArkUI.*
import ohos.arkui.state_macro_manage.*
@Entry
@Component
class EntryView {
func build() {
Flex(justifyContent: FlexAlign.Center) {
Text('1')
.width(20.percent)
.height(50)
.backgroundColor(0xF5DEB3)
Text('2')
.width(20.percent)
.height(50)
.backgroundColor(0xD2B48C)
Text('3')
.width(20.percent)
.height(50)
.backgroundColor(0xF5DEB3)
}
.width(90.percent)
.padding(top: 10, bottom: 10)
.backgroundColor(0xAFEEEE)
}
}
End
package ohos_app_cangjie_entry
import kit.ArkUI.*
import ohos.arkui.state_macro_manage.*
@Entry
@Component
class EntryView {
func build() {
Flex(justifyContent: FlexAlign.End) {
Text('1')
.width(20.percent)
.height(50)
.backgroundColor(0xF5DEB3)
Text('2')
.width(20.percent)
.height(50)
.backgroundColor(0xD2B48C)
Text('3')
.width(20.percent)
.height(50)
.backgroundColor(0xF5DEB3)
}
.width(90.percent)
.padding(top: 10, bottom: 10)
.backgroundColor(0xAFEEEE)
}
}
SpaceBetween
package ohos_app_cangjie_entry
import kit.ArkUI.*
import ohos.arkui.state_macro_manage.*
@Entry
@Component
class EntryView {
func build() {
Flex(justifyContent: FlexAlign.SpaceBetween) {
Text('1')
.width(20.percent)
.height(50)
.backgroundColor(0xF5DEB3)
Text('2')
.width(20.percent)
.height(50)
.backgroundColor(0xD2B48C)
Text('3')
.width(20.percent)
.height(50)
.backgroundColor(0xF5DEB3)
}
.width(90.percent)
.padding(top: 10, bottom: 10)
.backgroundColor(0xAFEEEE)
}
}
SpaceBetween 非常适合"导航栏两端固定、中间留白"的头部布局。
SpaceAround
package ohos_app_cangjie_entry
import kit.ArkUI.*
import ohos.arkui.state_macro_manage.*
@Entry
@Component
class EntryView {
func build() {
Flex(justifyContent: FlexAlign.SpaceAround) {
Text('1')
.width(20.percent)
.height(50)
.backgroundColor(0xF5DEB3)
Text('2')
.width(20.percent)
.height(50)
.backgroundColor(0xD2B48C)
Text('3')
.width(20.percent)
.height(50)
.backgroundColor(0xF5DEB3)
}
.width(90.percent)
.padding(top: 10, bottom: 10)
.backgroundColor(0xAFEEEE)
}
}
SpaceEvenly
package ohos_app_cangjie_entry
import kit.ArkUI.*
import ohos.arkui.state_macro_manage.*
@Entry
@Component
class EntryView {
func build() {
Flex(justifyContent: FlexAlign.SpaceEvenly) {
Text('1')
.width(20.percent)
.height(50)
.backgroundColor(0xF5DEB3)
Text('2')
.width(20.percent)
.height(50)
.backgroundColor(0xD2B48C)
Text('3')
.width(20.percent)
.height(50)
.backgroundColor(0xF5DEB3)
}
.width(90.percent)
.padding(top: 10, bottom: 10)
.backgroundColor(0xAFEEEE)
}
}
对比三者的空间分配差异:SpaceBetween 首尾贴边、间隙平分;SpaceAround 首尾间隙为相邻间隙的一半;SpaceEvenly 所有间隙完全相等。
交叉轴对齐方式
容器和子元素都可以设置交叉轴对齐方式,且子元素设置的对齐方式优先级较高。交叉轴对齐涉及三个层面:容器级 alignItems、子元素级 alignSelf、多行内容级 alignContent。
容器组件设置交叉轴对齐:alignItems
通过 Flex 组件的 alignItems 参数设置子元素在交叉轴的对齐方式,取值来自 ItemAlign 枚举:
| 枚举值 | 效果 |
|---|---|
| ItemAlign.Auto | 使用 Flex 容器中默认配置 |
| ItemAlign.Start | 交叉轴方向首部对齐 |
| ItemAlign.Center | 交叉轴方向居中对齐 |
| ItemAlign.End | 交叉轴方向底部对齐 |
| ItemAlign.Stretch | 交叉轴方向拉伸填充,在未设置尺寸时,拉伸到容器尺寸 |
| ItemAlign.Baseline | 交叉轴方向文本基线对齐 |
以下示例中三个子元素高度分别为 30、40、50,便于观察不同对齐值的效果:
package ohos_app_cangjie_entry
import kit.ArkUI.*
import ohos.arkui.state_macro_manage.*
@Entry
@Component
class EntryView {
func build() {
Flex(alignItems: ItemAlign.Auto) {
Text('1')
.width(33.percent)
.height(30)
.backgroundColor(0xF5DEB3)
Text('2')
.width(33.percent)
.height(40)
.backgroundColor(0xD2B48C)
Text('3')
.width(33.percent)
.height(50)
.backgroundColor(0xF5DEB3)
}
.size(width: 90.percent, height: 80)
.padding(10)
.backgroundColor(0xAFEEEE)
}
}
将上面 alignItems: ItemAlign.Auto 分别替换为 ItemAlign.Start、ItemAlign.Center、ItemAlign.End、ItemAlign.Baseline,即可得到对应效果(ItemAlign.Stretch 的用法见下文),四个变体完整代码如下:
package ohos_app_cangjie_entry
import kit.ArkUI.*
import ohos.arkui.state_macro_manage.*
@Entry
@Component
class EntryView {
func build() {
Flex(alignItems: ItemAlign.Start) {
Text('1')
.width(33.percent)
.height(30)
.backgroundColor(0xF5DEB3)
Text('2')
.width(33.percent)
.height(40)
.backgroundColor(0xD2B48C)
Text('3')
.width(33.percent)
.height(50)
.backgroundColor(0xF5DEB3)
}
.size(width: 90.percent, height: 80)
.padding(10)
.backgroundColor(0xAFEEEE)
}
}
package ohos_app_cangjie_entry
import kit.ArkUI.*
import ohos.arkui.state_macro_manage.*
@Entry
@Component
class EntryView {
func build() {
Flex(alignItems: ItemAlign.Center) {
Text('1')
.width(33.percent)
.height(30)
.backgroundColor(0xF5DEB3)
Text('2')
.width(33.percent)
.height(40)
.backgroundColor(0xD2B48C)
Text('3')
.width(33.percent)
.height(50)
.backgroundColor(0xF5DEB3)
}
.size(width: 90.percent, height: 80)
.padding(10)
.backgroundColor(0xAFEEEE)
}
}
package ohos_app_cangjie_entry
import kit.ArkUI.*
import ohos.arkui.state_macro_manage.*
@Entry
@Component
class EntryView {
func build() {
Flex(alignItems: ItemAlign.End) {
Text('1')
.width(33.percent)
.height(30)
.backgroundColor(0xF5DEB3)
Text('2')
.width(33.percent)
.height(40)
.backgroundColor(0xD2B48C)
Text('3')
.width(33.percent)
.height(50)
.backgroundColor(0xF5DEB3)
}
.size(width: 90.percent, height: 80)
.padding(10)
.backgroundColor(0xAFEEEE)
}
}
Stretch 拉伸填充:交叉轴方向拉伸填充,在未设置交叉轴尺寸时拉伸到容器尺寸。因此下面的示例中子元素不设置 height,由容器高度 80 决定:
package ohos_app_cangjie_entry
import kit.ArkUI.*
import ohos.arkui.state_macro_manage.*
@Entry
@Component
class EntryView {
func build() {
Flex(alignItems: ItemAlign.Stretch) {
Text('1')
.width(33.percent)
.backgroundColor(0xF5DEB3)
Text('2')
.width(33.percent)
.backgroundColor(0xD2B48C)
Text('3')
.width(33.percent)
.backgroundColor(0xF5DEB3)
}
.size(width: 90.percent, height: 80)
.padding(10)
.backgroundColor(0xAFEEEE)
}
}
Baseline 文本基线对齐:交叉轴方向文本基线对齐,适用于文本内容高度不一致、需要按文字基线排齐的场景:
package ohos_app_cangjie_entry
import kit.ArkUI.*
import ohos.arkui.state_macro_manage.*
@Entry
@Component
class EntryView {
func build() {
Flex(alignItems: ItemAlign.Baseline) {
Text('1')
.width(33.percent)
.height(30)
.backgroundColor(0xF5DEB3)
Text('2')
.width(33.percent)
.height(40)
.backgroundColor(0xD2B48C)
Text('3')
.width(33.percent)
.height(50)
.backgroundColor(0xF5DEB3)
}
.size(width: 90.percent, height: 80)
.padding(10)
.backgroundColor(0xAFEEEE)
}
}
子元素设置交叉轴对齐:alignSelf
子元素的 alignSelf 属性也可以设置子元素在父容器交叉轴的对齐格式,会覆盖 Flex 布局容器中 alignItems 的配置。其 API 签名为:
func alignSelf(value: ?ItemAlign): T
参数 value 为 ItemAlign 类型,初始值为 ItemAlign.Auto。根据 Flex 布局属性参考,该属性仅在父组件是 Flex、Column、Row、GridRow(仅针对 alignSelf)时生效。
如下例所示,Flex 容器通过 alignItems: ItemAlign.Center 设置子元素居中,但设置了 alignSelf 的子元素分别按 ItemAlign.Start 与 ItemAlign.Baseline 显示:
package ohos_app_cangjie_entry
import kit.ArkUI.*
import ohos.arkui.state_macro_manage.*
@Entry
@Component
class EntryView {
func build() {
Flex(direction: FlexDirection.Row, alignItems: ItemAlign.Center) { // 容器组件设置子元素居中
Text('alignSelf Start')
.width(25.percent)
.height(80)
.alignSelf(ItemAlign.Start)
.backgroundColor(0xF5DEB3)
Text('alignSelf Baseline')
.alignSelf(ItemAlign.Baseline)
.width(25.percent)
.height(80)
.backgroundColor(0xD2B48C)
Text('alignSelf Baseline')
.width(25.percent)
.height(100)
.backgroundColor(0xF5DEB3)
.alignSelf(ItemAlign.Baseline)
Text('no alignSelf')
.width(25.percent)
.height(100)
.backgroundColor(0xD2B48C)
Text('no alignSelf')
.width(25.percent)
.height(100)
.backgroundColor(0xF5DEB3)
}
.width(90.percent)
.height(220)
.backgroundColor(0xAFEEEE)
}
}
上例中,Flex 容器 alignItems 将交叉轴子元素对齐方式设置为居中,但子元素自身设置了 alignSelf 的实例会覆盖父组件的 alignItems 值,表现为 alignSelf 的定义;未设置 alignSelf 的子元素(如 no alignSelf)仍遵循容器级居中配置。
内容对齐:alignContent
可以通过 alignContent 参数设置子元素各行在交叉轴剩余空间内的对齐方式,它只在多行的 Flex 布局中生效(wrap 为 Wrap 或 WrapReverse)。可选值与主轴对齐类似,语义作用于"行":
| 枚举值 | 效果 |
|---|---|
| FlexAlign.Start | 子元素各行与交叉轴起点对齐 |
| FlexAlign.Center | 子元素各行在交叉轴方向居中对齐 |
| FlexAlign.End | 子元素各行与交叉轴终点对齐 |
| FlexAlign.SpaceBetween | 子元素各行与交叉轴两端对齐,各行间垂直间距平均分布 |
| FlexAlign.SpaceAround | 子元素各行间距相等,是元素首尾行与交叉轴两端距离的两倍 |
| FlexAlign.SpaceEvenly | 子元素各行间距、子元素首尾行与交叉轴两端距离都相等 |
以 Start 为例,其余五种仅替换 alignContent 取值:
package ohos_app_cangjie_entry
import kit.ArkUI.*
import ohos.arkui.state_macro_manage.*
@Entry
@Component
class EntryView {
func build() {
Flex(justifyContent: FlexAlign.SpaceBetween, wrap: FlexWrap.Wrap, alignContent: FlexAlign.Start) {
Text('1')
.width(30.percent)
.height(20)
.backgroundColor(0xF5DEB3)
Text('2')
.width(60.percent)
.height(20)
.backgroundColor(0xD2B48C)
Text('3')
.width(40.percent)
.height(20)
.backgroundColor(0xD2B48C)
Text('4')
.width(30.percent)
.height(20)
.backgroundColor(0xF5DEB3)
Text('5')
.width(20.percent)
.height(20)
.backgroundColor(0xD2B48C)
}
.width(90.percent)
.height(100)
.backgroundColor(0xAFEEEE)
}
}
五种变体代码(参数部分):
// FlexAlign.Center:各行在交叉轴方向居中
Flex(justifyContent: FlexAlign.SpaceBetween, wrap: FlexWrap.Wrap, alignContent: FlexAlign.Center) { ... }
// FlexAlign.End:各行与交叉轴终点对齐
Flex(justifyContent: FlexAlign.SpaceBetween, wrap: FlexWrap.Wrap, alignContent: FlexAlign.End) { ... }
// FlexAlign.SpaceBetween:各行与交叉轴两端对齐
Flex(justifyContent: FlexAlign.SpaceBetween, wrap: FlexWrap.Wrap, alignContent: FlexAlign.SpaceBetween) { ... }
// FlexAlign.SpaceAround:各行间距相等,为首尾行与两端距离的两倍
Flex(justifyContent: FlexAlign.SpaceBetween, wrap: FlexWrap.Wrap, alignContent: FlexAlign.SpaceAround) { ... }
// FlexAlign.SpaceEvenly:各行间距、首尾行与两端距离均相等
Flex(justifyContent: FlexAlign.SpaceBetween, wrap: FlexWrap.Wrap, alignContent: FlexAlign.SpaceEvenly) { ... }
自适应拉伸:flexBasis / flexGrow / flexShrink
在弹性布局父组件尺寸过小时,可通过子元素的以下属性设置其在父容器的占比,达到自适应布局。这三个属性均来自 Flex 布局通用属性,仅当父组件是 Flex、Column、Row 时生效。
flexBasis:设置基准尺寸
flexBasis 设置子元素在父容器主轴方向上的基准尺寸。如果设置了该属性,子项占用的空间为该属性所设置的值;如果没设置该属性,子项的空间为 width/height 的值。API 签名为:
func flexBasis(value: ?Length): T
关键取值规则(来自属性参考):
- 初始值为
'auto',表示组件在主轴方向上的基准尺寸为组件原本的大小; - 不支持设置 percent,取值范围 (0, +∞),默认单位为 vp;
- 异常值(如 0 之外的非法值)的实际布局效果均与
'auto'一致。
package ohos_app_cangjie_entry
import kit.ArkUI.*
import ohos.arkui.state_macro_manage.*
@Entry
@Component
class EntryView {
func build() {
Flex() {
Text('flexBasis("auto")')
.flexBasis(0) // 未设置width以及flexBasis值为0,内容自身宽度
.height(100)
.backgroundColor(0xF5DEB3)
Text('flexBasis("auto")' + ' width("40%")')
.width(40.percent)
.flexBasis(0) // 设置width以及flexBasis值为0,使用width的值
.height(100)
.backgroundColor(0xD2B48C)
Text('flexBasis(100)') // 未设置width以及flexBasis值为100,宽度为100.vp
.flexBasis(100)
.height(100)
.backgroundColor(0xF5DEB3)
Text('flexBasis(100)')
.flexBasis(100)
.width(200) // flexBasis值为100,覆盖width的设置值,宽度为100.vp
.height(100)
.backgroundColor(0xD2B48C)
}
.width(90.percent)
.height(120)
.padding(10)
.backgroundColor(0xAFEEEE)
}
}
示例中的四条规则可以归纳为:
- 未设置 width 且 flexBasis 为 0 时,取内容自身宽度;
- 设置了 width 且 flexBasis 为 0 时,使用 width 的值;
- 未设置 width 且 flexBasis 为 100 时,宽度为 100.vp;
- 同时设置 flexBasis(100) 与 width(200) 时,flexBasis 覆盖 width 的设置值,宽度为 100.vp。
flexGrow:按比例分配剩余空间
flexGrow 设置父容器的剩余空间分配给此属性所在组件的比例,用于分配父组件的剩余空间。支持 ?Float64 与 ?Int64 两种重载,取值范围 (0, +∞),初始值为 0。
package ohos_app_cangjie_entry
import kit.ArkUI.*
import ohos.arkui.state_macro_manage.*
@Entry
@Component
class EntryView {
func build() {
Flex() {
Text('flexGrow(2)')
.flexGrow(2)
.width(100)
.height(100)
.backgroundColor(0xF5DEB3)
Text('flexGrow(3)')
.flexGrow(3)
.width(100)
.height(100)
.backgroundColor(0xD2B48C)
Text('no flexGrow')
.width(100)
.height(100)
.backgroundColor(0xF5DEB3)
}
.width(380)
.height(120)
.padding(10)
.backgroundColor(0xAFEEEE)
}
}
分配逻辑推演:父容器宽度 420.vp,三个子元素原始宽度为 100.vp,左右 padding 为 20.vp,总占用 320.vp,剩余空间 100.vp 根据 flexGrow 值的占比分配给子元素,未设置 flexGrow 的子元素不参与分配。第一个元素与第二个元素以 2:3 分配剩下的 100.vp:
- 第一个元素:100.vp + 100.vp × 2/5 = 140.vp
- 第二个元素:100.vp + 100.vp × 3/5 = 160.vp
- 第三个元素(无 flexGrow):保持 100.vp 不变
flexShrink:按比例压缩
flexShrink 设置当父容器空间不足时子元素的压缩比例,同样支持 ?Float64 与 ?Int64 两种重载。需要注意初始值的差异(来自属性参考):父容器为 Column、Row 时初始值为 0;父容器为 Flex 时初始值为 1.0。
package ohos_app_cangjie_entry
import kit.ArkUI.*
import ohos.arkui.state_macro_manage.*
@Entry
@Component
class EntryView {
func build() {
Flex(direction: FlexDirection.Row) {
Text('flexShrink(3)')
.flexShrink(3)
.width(200)
.height(100)
.backgroundColor(0xF5DEB3)
Text('no flexShrink')
.width(200)
.height(100)
.backgroundColor(0xD2B48C)
Text('flexShrink(2)')
.flexShrink(2)
.width(200)
.height(100)
.backgroundColor(0xF5DEB3)
}
.width(380)
.height(120)
.padding(10)
.backgroundColor(0xAFEEEE)
}
}
三个子元素原始宽度均为 200.vp,远超容器 380.vp 的可用空间,此时 flexShrink 值决定各自被压缩的比例:flexShrink(3) 与 flexShrink(2) 的子元素按 3:2 分担压缩量,未设置 flexShrink 的子元素按 Flex 容器默认值 1 参与压缩。
补充:对于 Column/Row 作为父容器的场景,需设置主轴方向的尺寸(width/height),flexShrink 才生效;且即使设置了 constraintSize,若未设置主轴尺寸,Column/Row 仍会在主轴上自适应子组件尺寸,此时 flexShrink 不生效。
场景示例:导航栏式的水平均分布局
使用弹性布局,可以实现子元素沿水平方向排列、两端对齐、子元素间距平分、垂直方向中子元素居中的效果——这是页面头部导航栏的典型结构:
package ohos_app_cangjie_entry
import kit.ArkUI.*
import ohos.arkui.state_macro_manage.*
@Entry
@Component
class EntryView {
func build() {
Column() {
Column(space: 5) {
Flex(
direction: FlexDirection.Row, wrap: FlexWrap.NoWrap,
justifyContent: FlexAlign.SpaceBetween, alignItems: ItemAlign.Center) {
Text('1')
.width(30.percent)
.height(50)
.backgroundColor(0xF5DEB3)
Text('2')
.width(30.percent)
.height(50)
.backgroundColor(0xD2B48C)
Text('3')
.width(30.percent)
.height(50)
.backgroundColor(0xF5DEB3)
}
.height(70)
.width(90.percent)
.backgroundColor(0xAFEEEE)
}
.width(100.percent)
.margin(top: 5)
}.width(100.percent)
}
}
该示例将四项能力组合使用:
direction: FlexDirection.Row:子元素水平排列;justifyContent: FlexAlign.SpaceBetween:两端对齐、子元素间距平分;alignItems: ItemAlign.Center:垂直方向上子元素居中;wrap: FlexWrap.NoWrap:单行不换行。
总结与选型建议
| 关注点 | 结论 |
|---|---|
| 排列方向 | 用 direction(Row / RowReverse / Column / ColumnReverse)决定主轴方向 |
| 单行还是多行 | 用 wrap(NoWrap / Wrap / WrapReverse)控制换行行为 |
| 主轴对齐 | 用 justifyContent(Start / Center / End / SpaceBetween / SpaceAround / SpaceEvenly) |
| 交叉轴对齐 | 容器用 alignItems,子元素可用 alignSelf 覆盖,多行用 alignContent |
| 自适应拉伸 | 用 flexBasis 定基准、flexGrow 分剩余空间、flexShrink 控压缩比例 |
| 性能 | Flex 渲染存在二次布局过程,对性能要求严格的场景建议改用 Column/Row |
弹性布局与线性布局的最大差异在于默认具备压缩/拉伸能力。当页面中的子组件需要按比例分配剩余空间、或需要在不同屏幕尺寸下自适应伸缩时,优先选择 Flex;当子组件数量固定、排列规则简单且对性能敏感时,可退回使用 Column/Row。更完整的布局体系介绍与各布局选型对照可继续阅读 布局概述,Flex 组件完整 API 与更多组合示例见 Flex 组件参考。