Flex 弹性布局
用一维 flex 容器排列内容,提供方向、间距、对齐、分布和换行能力。
何时使用
- 工具栏、按钮行、状态行和卡片内部的一维排列。
- 需要在主轴分配空间或在窄窗口换行时。
- 需要 24 列响应式栅格用 Grid 栅格;需要固定分隔面板用 Splitter 分割面板。
- 只想给内联控件增加统一间距时,用 Space 间距。
在线体验
Flex 弹性布局 · 在线体验真实 Go 组件
在独立页面打开 ↗基本用法
go
import (
"github.com/tangoui/tango/ui"
"github.com/tangoui/tango/ui/layout"
)
func page(c *ui.Context) {
ui.Flex(c).Gap(ui.Small).Align(layout.AlignCenter).Body(func() {
ui.Button(c, "取消")
ui.Button(c, "保存").Primary()
})
}Flex 默认横向排列,Gap 使用 Small、Middle、Large 设计令牌。
常见用法
两端对齐工具栏
JustifySpaceBetween 把首尾内容推向两端,适合标题和操作按钮共存的行。
go
import (
"github.com/tangoui/tango/ui"
"github.com/tangoui/tango/ui/layout"
)
func page(c *ui.Context) {
ui.Flex(c).Justify(layout.JustifySpaceBetween).Align(layout.AlignCenter).Body(func() {
ui.Text(c, "项目设置").Strong()
ui.Button(c, "保存").Primary()
})
}竖向内容
Vertical() 或 Orientation("vertical") 将主轴改为纵向,Orientation("horizontal") 改回横向。可配合 Gap 和 Style 的布局选项构造内容区。
go
import (
"github.com/tangoui/tango/ui"
"github.com/tangoui/tango/ui/layout"
)
func page(c *ui.Context) {
ui.Flex(c).Vertical().Gap(ui.Middle).Style(layout.Pad(16)).Body(func() {
ui.Text(c, "标题").Strong()
ui.Text(c, "说明文字").Secondary()
})
}换行与像素间距
Wrap() 允许子项换行,GapPx 适合设计稿给出明确像素值的场景。
go
import "github.com/tangoui/tango/ui"
func page(c *ui.Context) {
ui.Flex(c).Wrap().GapPx(12).Body(func() {
for _, label := range []string{"一", "二", "三", "四"} {
ui.Button(c, label)
}
})
}主题与样式
Flex 的间距令牌是 Small=8、Middle=16、Large=24,不同主题只改变控件外观,不改变这些布局语义。需要精确值可用 GapPx,需要外边距、宽度或高度可用 Style(layout.Option...)。
注意事项
Flex适合一维布局;不要用大量嵌套 Flex 模拟二维栅格。- 子节点需要占满剩余空间时,用子容器的
layout.Grow(1);不要把不确定的百分比硬编码到每个子项。 - 换行后主轴和交叉轴的对齐仍由
Justify、Align控制,窄屏应检查长文本和按钮是否可读。 - 键盘顺序遵循子节点顺序;不要仅用
Justify或视觉顺序表达操作流程。 - RTL 会自动镜像主轴方向,避免用绝对定位覆盖 Flex 排版。
示例代码
以下是上方在线演示运行的 Go 源码(来自官方 gallery),点击标题展开。函数接收的 c *ui.Context 由窗口的 UI 循环每帧传入,接入方式见 Go GUI 入门。
API
签名与说明由 site/tools/compdoc 从 Go 源码和文档注释提取;点击名称跳到 Go API 参考 的对应条目。
入口函数
ui.Flex
go
func Flex(c *Context) *FlexWFlex starts a horizontal flex box; chain options, then Body.
返回 *FlexW,可链式调用的方法见 FlexW 方法。
FlexW 方法
ui.FlexW:FlexW is a flex container with design-token gaps (Ant Flex).
| 方法 | 说明 | 签名 |
|---|---|---|
Align | Align positions children on the cross axis. | Align(a layout.Align) *FlexW |
Body | Body adds the children. | Body(fn func()) *Box |
Gap | Gap sets the gap with a size token (Small 8, Middle 16, Large 24). | Gap(s ControlSize) *FlexW |
GapPx | GapPx sets the gap in pixels. | GapPx(px float32) *FlexW |
Justify | Justify distributes free space on the main axis. | Justify(j layout.Justify) *FlexW |
Orientation | Orientation sets the direction: "horizontal" (default) or "vertical" (Ant v6 orientation). | Orientation(o string) *FlexW |
Style | Style applies extra layout options. | Style(opts ...layout.Option) *FlexW |
Vertical | Vertical lays children top to bottom. | Vertical() *FlexW |
Wrap | Wrap wraps children onto new lines. | Wrap() *FlexW |