HarmonyOS开发者版主1|
ArkUI (方舟UI框架)
UI
HarmonyOS 5
HarmonyOS Flex布局如何限制显示行数? (API12+)
HarmonyOS Flex布局如何限制显示行数? (API12+)
1
浏览77 发布于2026-04-02 11:25未知归属地
全部评论
最多点赞
最新发布
最早发布

通过onAreaChange回调,收集各子项宽度和Flex组件自身宽度,通过自定义方法计算得出最大行数能容纳的子项个数,查看最后一行是否能放置折叠按钮,若不能,则削减子项个数,直到能为止。示例代码如下:
收起
自动换行
深色代码主题
复制
@Entry
@Component
struct FlexTest {
@State message: string = 'Hello World';
@State allDataList: string[] = ['1', '222', '33333', '44', '2', '222', '33333', '44', '1', '222', '33333', '44', '222', '33333']
@State showDataList: string[] = []
@State isShowAll: boolean = true
private cutIndex: number = 0
private flexWidth: number = 0
private itemWidthList: number[] = []
private isInit: boolean = false
aboutToAppear(): void {
this.initItemList()
}
initItemList() {
this.showDataList = this.allDataList
this.itemWidthList = new Array<number>(this.allDataList.length)
for (let i = 0; i < this.itemWidthList.length; i++) {
this.itemWidthList[i] = 0
}
this.isInit = false
}
isDrawFinish() {
let isFinish = true;
for (let i = 0; i < this.itemWidthList.length; i++) {
if (this.itemWidthList[i] == 0) {
isFinish = false
}
}
if (this.flexWidth == 0) {
isFinish = false;
}
if (isFinish) {
this.isInit = true
this.calculateCutIndex(3)
this.transformExpendList(false)
}
}
transformExpendList(showAll: boolean) {
if (showAll) {
this.isShowAll = true
this.showDataList = this.allDataList
} else {
this.isShowAll = false
this.showDataList = this.allDataList.slice(0, this.cutIndex)
}
}
// maxLine指布局需要保留的最大行数
calculateCutIndex(maxLine: number) {
let layer = 1;
let curLength = 0;
for (let i = 0; i < this.itemWidthList.length; i++) {
curLength += this.itemWidthList[i]
if (curLength > this.flexWidth) {
if (layer == maxLine) {
curLength -= this.itemWidthList[i]
this.cutIndex = i
break;
} else {
layer += 1
curLength = this.itemWidthList[i]
}
}
}
// 查看maxLine当前剩余的宽度是否足以容纳按钮,不能则掏空到能为止
let buttonWidth = 40
let empty = this.flexWidth - curLength
while (empty <= buttonWidth) {
empty += this.itemWidthList[this.cutIndex]
this.cutIndex--
}
}
build() {
Column() {
Flex({wrap: FlexWrap.Wrap, alignItems: ItemAlign.Center}) {
ForEach(this.showDataList, (item: string, index: number) => {
Text(item)
.fontSize(20)
.fontWeight(FontWeight.Bold)
.fontColor(Color.Black)
.backgroundColor(Color.Gray)
.borderRadius(10)
.padding(8)
.margin(5)
.id(`item_${index}`)
.onAreaChange((oldValue: Area, newValue: Area) => {
if (!this.isInit && newValue.width != 0) {
this.itemWidthList[index] = Number(newValue.width) + 10 // item宽度加上两边margin
this.isDrawFinish()
}
})
})
Button() {
Text(this.isShowAll ? '^' : 'v')
.padding(10)
.fontColor(Color.White)
.borderRadius(5)
}
.width(40)
.onClick(() => {
this.transformExpendList(!this.isShowAll)
})
}
.onAreaChange((oldValue: Area, newValue: Area) => {
if (!this.isInit && newValue.width != 0) {
this.flexWidth = Number(newValue.width)
this.isDrawFinish()
}
})
.backgroundColor("#f1f3f5")
.id("flex")
.width("50%")
}
.height('100%')
.width('100%')
.justifyContent(FlexAlign.Start)
}
}
展开全部 
1楼回复于2026-04-02 11:25 未知归属地
写回答
所见即所得编辑器, editor1
工具栏 撤消 快捷键 Ctrl+Z 重做 快捷键 Ctrl+Y 格式刷 快捷键 Ctrl+Shift+C 清除格式 加粗 快捷键 Ctrl+B 倾斜 快捷键 Ctrl+I 下划线 快捷键 Ctrl+U 删除线格式调整字号 文本颜色 背景颜色 增加缩进量 减少缩进量 左对齐 居中 右对齐 两端对齐段落间距行间距 项目列表 编号列表 插入水平线 块引用 插入/编辑超链接 快捷键 Ctrl+K 取消超链接 图片 表格 上传附件 导入Word 插入代码 MD编辑器 全屏模式
0/50000
新增插入模板功能
一键使用模板,快速填写内容,轻松发帖~
知道了
- 为了保障您的信息安全,请勿上传您的敏感个人信息(如您的密码等信息)和您的敏感资产信息(如关键源代码、签名私钥、调试安装包、业务日志等信息),且您需自行承担由此产生的信息泄露等安全风险。
- 如您发布的内容为转载内容,请注明内容来源。
我要提问题
相关推荐
换一批
1
HarmonyOS SaveButton 保存图片设置背景色范围是否有限制?范围是多少?(API12+)
2
HarmonyOS Flex无法限制高度(API12+)
3
HarmonyOS AppStorage存储有大小限制吗?(API12+)
4
HarmonyOS 元服务对拉起三方App有什么限制?(API12+)
5
HarmonyOS textArea是否支持设置行数占位?(API12+)
6
HarmonyOS TextInput如何限制输入数字范围?(API12+)
7
HarmonyOS TextInput如何限制输入两位小数?(API12+)
8
HarmonyOS 怎么控制组件的显示和隐藏?(API12+)
了解社区公约,与您携手共创和谐专业的开发者社区。




































苏公安网备 32011402010933号