
回复
在鸿蒙应用开发中,ColumnSplit 与 RowSplit 组件作为动态分割布局的核心工具,为开发者提供了构建交互式多面板界面的强大能力。ColumnSplit 实现垂直方向的区域分割,RowSplit 专注于水平方向的空间分配,两者通过可拖拽分割线实现区域尺寸的动态调整。这种 "所见即所得" 的布局模式,完美适配文件管理器、数据仪表盘、多任务界面等需要灵活空间分配的场景,使应用能够根据用户需求实时调整各功能区域的显示比例,大幅提升交互体验的灵活性与实用性。
ColumnSplit 与 RowSplit 基于 "弹性空间分配" 理念构建:
这种设计使界面能够根据内容重要性动态分配空间,尤其适合需要同时展示多类信息且信息优先级可变的场景。
// xxx.ets
@Entry
@Component
struct ColumnSplitExample {
build() {
Column(){
Text('The dividing line can be dragged').fontSize(9).fontColor(0xCCCCCC).width('90%')
ColumnSplit() {
Text('1').width('100%').height(50).backgroundColor(0xF5DEB3).textAlign(TextAlign.Center)
Text('2').width('100%').height(50).backgroundColor(0xD2B48C).textAlign(TextAlign.Center)
Text('3').width('100%').height(50).backgroundColor(0xF5DEB3).textAlign(TextAlign.Center)
Text('4').width('100%').height(50).backgroundColor(0xD2B48C).textAlign(TextAlign.Center)
Text('5').width('100%').height(50).backgroundColor(0xF5DEB3).textAlign(TextAlign.Center)
}
.borderWidth(1)
.resizeable(true) // 可拖动
.width('90%').height('60%')
}.width('100%')
}
}
布局特点:默认将容器垂直平分,两区域高度各占 50%,分割线可拖拽调整。
// xxx.ets
@Entry
@Component
struct RowSplitExample {
build() {
Column() {
Text('The second line can be dragged').fontSize(9).fontColor(0xCCCCCC).width('90%')
RowSplit() {
Text('1').width('10%').height(100).backgroundColor(0xF5DEB3).textAlign(TextAlign.Center)
Text('2').width('10%').height(100).backgroundColor(0xD2B48C).textAlign(TextAlign.Center)
Text('3').width('10%').height(100).backgroundColor(0xF5DEB3).textAlign(TextAlign.Center)
Text('4').width('10%').height(100).backgroundColor(0xD2B48C).textAlign(TextAlign.Center)
Text('5').width('10%').height(100).backgroundColor(0xF5DEB3).textAlign(TextAlign.Center)
}
.resizeable(true) // 可拖动
.width('90%').height(100)
}.width('100%').margin({ top: 5 })
}
}
布局特点:水平分割容器为上下两部分,默认宽度各占 50%,支持拖拽分割线调整比例。
典型配置示例:
ColumnSplit() {
/* 子组件... */
}
.resizeable(true)
.divider({
startMargin: 10,
endMargin: 10,
})
@Entry
@Component
struct SplitExample {
build() {
Column() {
// 垂直分割区域
ColumnSplit() {
Text('顶部区域').height('50%').backgroundColor(Color.Orange)
Divider()
Text('底部区域').height('50%').backgroundColor(Color.Blue)
}
// 水平分割区域
RowSplit() {
Text('左侧区域').width('40%').backgroundColor(Color.Green)
Divider().vertical(true)
Text('右侧区域').width('60%').backgroundColor(Color.Yellow)
}
.onDragStart(() => console.log('水平拖拽开始'))
.onDragEnd(() => console.log('水平拖拽结束'))
}
}
}
@Entry
@Component
struct FileManagerDemo {
@State selectedFolder: string = '根目录'
@State folderList: string[] = ['根目录', '文档', '图片', '视频', '音乐']
@State fileMap: Map<string, string[]> = new Map([
['根目录', ['README.md', 'settings.json']],
['文档', ['开发文档.docx', '设计稿.pdf']],
['图片', ['风景.jpg', '头像.png']],
['视频', ['演示视频.mp4', '会议记录.mkv']]
])
build() {
ColumnSplit() {
// 左侧文件夹列表
Column() {
ForEach(this.folderList, (folder: string) => {
Text(folder)
.padding(12)
.width('100%')
.borderRadius(4)
.backgroundColor(this.selectedFolder === folder
? $r('app.color.selected_bg')
: $r('app.color.unselected_bg'))
.fontColor(this.selectedFolder === folder
? $r('app.color.selected_text')
: $r('app.color.unselected_text'))
.onClick(() => {
this.selectedFolder = folder
})
})
}
.width('30%') // 左侧固定30%宽度
.padding(8)
// 右侧文件内容展示
Column() {
Text(`当前文件夹: ${this.selectedFolder}`)
.padding(16)
.fontSize(18)
.fontWeight(FontWeight.Bold)
.fontColor($r('app.color.title_text'))
// 文件列表
List({ space: 8 }) {
ForEach(this.fileMap.get(this.selectedFolder) || [], (file: string) => {
ListItem() {
Text(file)
.padding(12)
.width('100%')
.backgroundColor($r('app.color.item_bg'))
.borderRadius(8)
}
})
}
.width('100%')
.layoutWeight(1) // 占据剩余空间
.divider({
strokeWidth: 1,
color: $r('app.color.divider')
})
}
.layoutWeight(1) // 右侧自适应宽度
.padding(8)
}
.type(PanelType.Horizontal) // 水平分割布局
.dragBar(true) // 显示拖拽条
.onChange((width: number) => {
console.info(`左侧面板宽度: ${width}px`)
})
.width('100%')
.height('100%')
.backgroundColor($r('app.color.background'))
}
}
@Entry
@Component
struct DataDashboardDemo {
@State dataList: string[] = ['数据项1', '数据项2', '数据项3', '数据项4', '数据项5', '数据项6']
@State chartData: number[] = [25, 30, 45, 60, 40, 35] // 图表数据
build() {
RowSplit() {
// 上方图表区域
Column() {
Text('数据统计图表区域')
.padding(16)
.fontSize(18)
.fontWeight(FontWeight.Bold)
.fontColor($r('app.color.title_text'))
.width('100%')
.textAlign(TextAlign.Center)
.backgroundColor($r('app.color.header_bg'))
// 图表容器
Stack() {
// 柱状图模拟
Row({ space: 8 }) {
ForEach(this.chartData, (value, index) => {
Column() {
Text(`${value}%`)
.fontSize(12)
.fontColor($r('app.color.chart_text'))
.margin({ bottom: 4 })
Column()
.width(30)
.height(value)
.backgroundColor($r('app.color.chart_bar'))
.borderRadius(4)
Text(`数据${index + 1}`)
.fontSize(12)
.fontColor($r('app.color.chart_label'))
.margin({ top: 4 })
}
.alignItems(HorizontalAlign.Center)
})
}
.height(200)
.justifyContent(FlexAlign.End)
.width('100%')
.padding(16)
}
.width('100%')
.height(250)
.backgroundColor($r('app.color.chart_bg'))
.borderRadius(12)
.margin({ left: 16, right: 16, bottom: 16 })
}
.layoutWeight(2) // 权重比例2
// 下方数据列表
Column() {
Text('详细数据列表')
.padding(16)
.fontSize(18)
.fontWeight(FontWeight.Bold)
.fontColor($r('app.color.title_text'))
.width('100%')
.textAlign(TextAlign.Center)
.backgroundColor($r('app.color.header_bg'))
List({ space: 8 }) {
ForEach(this.dataList, (item, index) => {
ListItem() {
Row() {
Text(`数据${index + 1}`)
.width(80)
.fontColor($r('app.color.data_index'))
.fontWeight(FontWeight.Medium)
Text(item)
.flexGrow(1)
.textAlign(TextAlign.Start)
.fontColor($r('app.color.data_text'))
}
.padding(12)
}
.backgroundColor(index % 2 === 0
? $r('app.color.list_item_even')
: $r('app.color.list_item_odd'))
.borderRadius(8)
})
}
.width('100%')
.divider({
strokeWidth: 1,
color: $r('app.color.divider')
})
.scrollBar(BarState.Auto)
.layoutWeight(1) // 占据剩余空间
}
.layoutWeight(3) // 权重比例3
}
.type(PanelType.Vertical) // 垂直分割布局
.dragBar(true) // 显示拖拽条
.onChange((height: number) => {
console.info(`图表区域高度: ${height}px`)
})
.width('100%')
.height('100%')
.backgroundColor($r('app.color.background'))
}
}
鸿蒙 ColumnSplit 与 RowSplit 组件通过标准化的分割机制,为全场景应用提供了灵活的空间分配能力。从文件管理的双面板交互到数据仪表的多区域展示,分割布局在以下场景中具有显著优势:
未来随着鸿蒙生态的发展,分割布局将与更多特性融合,如:
实践建议:
DoublePanel
、TripleSplit
通过系统掌握分割布局技术,开发者能够构建更具交互性和适应性的界面,为用户提供个性化的空间分配体验,在鸿蒙全场景开发中抢占先机。