在鸿蒙应用开发体系中,Column 容器组件作为垂直布局的核心载体,通过声明式语法实现子组件的有序垂直排列。作为线性布局的重要组成部分,其简洁的属性体系与强大的适配能力,完美覆盖列表展示、表单输入、信息分层等 80% 以上的垂直布局场景。本文将从基础原理到工程实践,系统解析 Column 组件的核心能力与实战技巧,帮助开发者构建高效、灵活的垂直界面体系。
@Entry
@Component
struct ColumnBasicDemo {
build() {
Column() { // 垂直布局根容器
Text('主标题')
.fontSize(18)
.fontWeight(FontWeight.Bold)
Divider()
.margin({ top: 12,bottom:12 }) // 间隔分割线
Button('操作按钮')
.width(120)
.height(44)
}
.width('100%') // 撑满父容器宽度
.padding(24) // 全局内边距
}
}
Column({ space: 16 }) { // 16vp垂直间距
Text('列表项1')
.width('100%')
Text('列表项2')
.width('100%')
Text('列表项3')
.width('100%')
}
鸿蒙编码习惯,一般都是4的倍数,写的时候尽量4、8、12、16、20等
如果公司有要求还是以公司为准
通过FlexAlign
枚举实现垂直方向精准分布:
对齐方式 | 应用场景 | 代码示例 |
---|---|---|
Start | 列表 / 表单顶部对齐 | Column().justifyContent(FlexAlign.Start) |
Center | 登录界面 / 弹窗居中 | Column().justifyContent(FlexAlign.Center) |
End | 底部操作栏 / 页脚对齐 | Column().justifyContent(FlexAlign.End) |
SpaceBetween | 分段信息垂直分布 | Column().justifyContent(FlexAlign.SpaceBetween) |
通过HorizontalAlign
枚举实现水平方向对齐:
@Entry
@Component
struct LoginFormDemo {
build() {
Column() { // 水平居中
Image($r('app.media.icon')).size(24)
Text('图标与文本居中').fontSize(14).margin({ left: 8 })
}
.alignItems(HorizontalAlign.Center) // 水平居中
.width("100%") //不设置宽度,会根据内容自适应宽度,上面那个会失效
.height("100%")
}
}
Start
:左对齐(文本默认)Center
:水平居中(图文混排)Stretch
:拉伸填充(表单输入框)width('100%')
(表单容器)width(200)
(固定卡片)Flex
组件实现动态分配 Column() {
Flex({ direction: FlexDirection.Row }) { // 水平弹性子布局
Text('标签').width(80)
TextInput().flexGrow(1) // 弹性填充剩余空间
}
Button('提交').width('100%')
}
@Entry
@Component
struct LoginFormDemo {
@State username: string = ''
@State password: string = ''
build() {
Column({ space: 16 }) {
// 用户名输入区
Text('用户名')
.fontSize(14)
.fontColor('#333')
.width('100%') // 文本宽度适配容器
.textAlign(TextAlign.Start) // 左对齐优化
TextInput({ placeholder: '请输入账号', text: this.username })
.onChange((value: string) => {
this.username = value // 状态更新
})
.width('100%')
.height(48)
.borderRadius(24)
.backgroundColor('#F5F5F5')
.padding({ left: 16, right: 16 }) // 增加右侧内边距
// 密码输入区
Text('密码')
.fontSize(14)
.fontColor('#333')
.width('100%')
.textAlign(TextAlign.Start)
TextInput({ placeholder: '请输入密码', text: this.password })
.type(InputType.Password) // 密码输入类型
.onChange((value: string) => {
this.password = value
})
.width('100%')
.height(48)
.borderRadius(24)
.backgroundColor('#F5F5F5')
.padding({ left: 16, right: 16 })
// 登录按钮
Button('登录', { type: ButtonType.Capsule }) // 使用胶囊按钮规范
.width('100%')
.height(50)
.margin({ top: 8 })
.backgroundColor('#007DFF') // 标准主题色
.fontColor(Color.White)
.onClick(() => {
// 添加登录逻辑
})
}
.width('100%')
.padding({ top: 40, left: 24, right: 24 })
.backgroundColor('#F9F9F9')
.alignItems(HorizontalAlign.Start) // 左对齐容器
}
}
@Entry
@Component
struct NotificationDemo {
build() {
Column({ space: 16 }) {
this.NotificationCard('系统更新', '版本1.2.0已发布,新增多端协同功能')
this.NotificationCard('活动提醒', '您有一场线上会议即将开始,点击查看详情')
this.NotificationCard('订单通知', '您的包裹已发货,预计2日内送达')
} // 卡片间距16vp
.width('100%')
.padding({ top: 20, left: 24, right: 24 })
.backgroundColor('#F5F5F5')
}
@Builder
NotificationCard(title: string, content: string) {
Column() {
Text(title)
.fontSize(16)
.fontWeight(FontWeight.Bold)
Text(content)
.fontSize(14)
.margin({ top: 8 })
.lineHeight(20)
Text('2024-06-22')
.fontSize(12)
.fontColor('#999')
.margin({ top: 12 })
}
.alignItems(HorizontalAlign.Start)
.padding(20)
.backgroundColor('#FFFFFF')
.borderRadius(12)
.shadow({
radius: 4,
color: '#00000010',
offsetX: 2,
offsetY: 2
})
}
}
// 优化前(深层嵌套)
Column() {
Column() {
Column() { /* 内容 */ }
}
}
// 优化后(单层Column+space)
Column({ space: 20 }) { /* 直接排列子组件 */ }
在前端开发中,布局嵌套的数量通常建议不要超过3级。这是因为过度的嵌套可能会导致页面加载变慢,且在维护时会变得更加复杂。在实际开发过程中,开发者应根据具体需求和性能测试结果来调整嵌套的深度,以确保应用的性能和可维护性。
.width(DeviceType.isPhone() ? '90%' : '70%') // 手机/平板差异化宽度
Scroll() { // 包裹Scroll实现滚动
Column({ space: 16 }) {
// 100条列表项...
}
.width('100%')
}
Column() {
Text('左对齐文本')
.width('80%') // 显式设置宽度时需手动调整对齐
Button('居中按钮')
.width('100%')
}
.alignItems(HorizontalAlign.Center)
Column() {
// 顶部水平导航
Row({ space: 20 }) {
Text('首页').fontSize(16)
Text('分类').fontSize(16)
Text('我的').fontSize(16)
}
.padding({ top: 16, bottom: 12 })
// 垂直内容区
Column({ space: 16 }) {
/* 内容列表 */
}
.flexGrow(1)
}
鸿蒙 Column 组件通过标准化的属性体系,实现了垂直布局的高效开发,是构建现代化界面的基础组件。掌握以下核心能力即可应对各类垂直布局需求:
space
控制垂直间距,justifyContent
与alignItems
实现精准对齐随着鸿蒙生态向全场景拓展,Column 组件作为基础布局单元的重要性日益凸显。建议开发者通过官方模拟器多设备预览功能,深入实践不同场景下的布局效果,逐步建立垂直布局的设计思维,为用户提供简洁、高效的交互体验。