用户3096723
用户3096723
  • 发布:2026-07-13 12:29
  • 更新:2026-07-13 12:29
  • 阅读:17

解决Row容器空间不足时子组件消失的问题

分类:uni-app

问题现象
在Row组件中放置两个Text组件,左侧Text(动态标题)需自适应宽度,空间不足时末尾省略显示(TextOverflow.Ellipsis),右侧Text(如(99))需始终完整显示,但实际效果中,空间不足时左侧Text直接消失,而非显示省略号。

问题代码示例参考如下:

@Entry
@Component
struct Index {
@State title: string = '长标题文本长标题文本长标题文本';

build() {
Column() {
Row() {
Text(this.title)
.fontSize(17)
.fontWeight(500)
.maxLines(1)
.textOverflow({ overflow: TextOverflow.Ellipsis })
.textAlign(TextAlign.Center)
.onClick(() => {
this.title += '加加';
})

    Text('(99)')  
      .fontSize(17)  
      .fontWeight(500)  
      .maxLines(1)  
      .textOverflow({ overflow: TextOverflow.Ellipsis })  
      .textAlign(TextAlign.Start)  
      .displayPriority(2)  
  }  
  .justifyContent(FlexAlign.Center)  
  .width('100%')  
  .margin({ top: 280 })  
}  

}
}
问题效果预览:

点击放大

效果预览
点击放大

背景知识
displayPriority属性机制:当父容器空间不足时,系统按优先级隐藏子组件(值越小优先级越高),若某一优先级组件被隐藏,更低优先级的组件会全部被隐藏(即使空间足够),右侧Text设置displayPriority(低优先级),左侧未设置(默认0,高优先级),但隐藏逻辑导致左侧异常消失。
弹性布局压缩规则:Row基于Flex布局,子组件默认flexShrink:0(禁止压缩),文本省略需同时满足:设置maxLines和textOverflow,组件flexShrink:1(允许压缩)且有明确宽度约束。
问题定位
隐藏机制冲突:右侧displayPriority激活了隐藏逻辑,空间不足时触发低优先级组件隐藏链,导致左侧被连带隐藏,左侧虽设置省略样式,但flexShrink默认为0,未触发压缩流程,直接跳过省略进入隐藏。
布局约束缺失:左侧Text未明确允许压缩,右侧未禁止压缩,两者在空间争夺中行为未定义,justifyContent(FlexAlign.Center)强制居中分配空间,加剧宽度计算冲突。
分析结论
根本矛盾在于:displayPriority的组件级隐藏机制与textOverflow的文本级压缩机制互斥,当空间不足时,系统优先触发displayPriority的隐藏逻辑,而非文本压缩。

修改建议
核心方案:弃用displayPriority,改用弹性压缩控制。

@Entry
@Component
struct LongText {
@State title: string = '长标题文本长标题文本长标题文本';

build() {
Column() {
// 关键修改1:使用Flex替代Row,明确弹性规则
Flex({
direction: FlexDirection.Row,
alignItems: ItemAlign.Center,
justifyContent: FlexAlign.Start // 左对齐
}) {
Text(this.title)
.fontSize(17)
.fontWeight(500)
.maxLines(1)
.textOverflow({ overflow: TextOverflow.Ellipsis })
.flexShrink(1) // 关键修改2:允许压缩
.onClick(() => {
this.title += '追加文本';
})

    Text('(99)')  
      .fontSize(17)  
      .fontWeight(500)  
      .flexShrink(0) // 关键修改3:禁止压缩  
  }  
  .width('100%')  
  .margin({ top: 280 })  
}  

}
}
https://pastebin.com/zVxDU61E
https://pastebin.com/nzLueSAz
https://pastebin.com/ezBMAig0
https://pastebin.com/YnzFdFKL
https://pastebin.com/7NqfmRSc
https://pastebin.com/XnhHvhjC
https://pastebin.com/5vt1ShF0
https://pastebin.com/jimpxmtQ
https://pastebin.com/0MKBBtSb
https://pastebin.com/hYPcWX3A
https://pastebin.com/JGZi6dqX
https://pastebin.com/Eg0QkGxK
https://pastebin.com/avBQHdmv
https://pastebin.com/9K1kr69e
https://pastebin.com/Ft3Qmf4p
https://pastebin.com/ECQydf9d
https://pastebin.com/w6AjGy7B
https://pastebin.com/pUAfNajs
https://pastebin.com/PdVL3eGz
https://pastebin.com/mTMqjn2v
https://pastebin.com/3h4gwTdv
https://pastebin.com/gjNjuKYf
https://pastebin.com/KFucmjDt
https://pastebin.com/6bWkRuLA
https://pastebin.com/7NibmTdB
https://pastebin.com/YfQPgbwm
https://pastebin.com/LJSs1Hdj
https://pastebin.com/PSvAdQ53
https://pastebin.com/KgSmzqhM
https://pastebin.com/BnDxzKWh
https://pastebin.com/aF99ZhZq
https://pastebin.com/bqhqtcei
https://pastebin.com/sXTWrCAD
https://pastebin.com/7yCNwek9
https://pastebin.com/naX7WgBh
https://pastebin.com/DkhB099s
https://pastebin.com/zkQ8Pr8B
https://pastebin.com/N3DaSPpi
https://pastebin.com/uTy41bXJ
https://pastebin.com/7YJE2nuP
https://pastebin.com/nz8wUJUM
https://pastebin.com/2REs5Q33
https://pastebin.com/tQ28rQXE
https://pastebin.com/DMCWY9XS
https://pastebin.com/nRcXfz4e
https://pastebin.com/S4NzD1sa
https://pastebin.com/QgVJE8JN
https://pastebin.com/EvAFi0b5
https://pastebin.com/Nz6N6kSC
https://pastebin.com/n5xFt0XA
https://pastebin.com/AFNPdqRC
https://pastebin.com/wC1fQXzb
https://pastebin.com/ttzSHyy5
https://pastebin.com/6y9J8aZd
https://pastebin.com/gP7ZHPhR
https://pastebin.com/xhVy1Dr7
https://pastebin.com/7tMFe3Z8
https://pastebin.com/aFthfCSF
https://pastebin.com/26QsabND
https://pastebin.com/hbZqkTfN
https://pastebin.com/M0ZMtszR
https://pastebin.com/F62d211T
https://pastebin.com/LH6eA95e
https://pastebin.com/6mP60iDt
https://pastebin.com/gRhsLLQt
https://pastebin.com/a7rmhfm0
https://pastebin.com/sj10sst6
https://pastebin.com/dty8sxD5
https://pastebin.com/dHEXCxFt
https://pastebin.com/pE6ePUAY
https://pastebin.com/2U1LgwWz
https://pastebin.com/WQpk7jSa
https://pastebin.com/G8LPQVtP
https://pastebin.com/VRpjsRTK
https://pastebin.com/aCy0qhBi
https://pastebin.com/NFQMuiXH
https://pastebin.com/S064raxa
https://pastebin.com/4s7w0BBW
https://pastebin.com/m7UDFUJ6
https://pastebin.com/UBeiNw6e
https://pastebin.com/kms3qfYM
https://pastebin.com/dYjjH5yP
https://pastebin.com/6cvgDvgL
https://pastebin.com/KPmn6RCf
https://pastebin.com/DiPdkuXM
https://pastebin.com/53nPeHWZ
https://pastebin.com/WNdjQBtb
https://pastebin.com/gD3BbuEt
https://pastebin.com/K1djmuru
https://pastebin.com/uqMvn9Uq
https://pastebin.com/paibxsw8
https://pastebin.com/Una68Ba5
https://pastebin.com/P7CrcBfR
https://pastebin.com/UiQ5wXCw
https://pastebin.com/LYHMErrw
https://pastebin.com/T3Fjh1Er
https://pastebin.com/m7Zx61yT
https://pastebin.com/dRmeRwsB
https://pastebin.com/vN5zyV7U
https://pastebin.com/hqms8PnB
https://pastebin.com/VJJ9tacg
https://pastebin.com/gnHE24Vx
https://pastebin.com/2DN8jUYE
https://pastebin.com/NYYLtSrA
https://pastebin.com/FJcvCjYv
https://pastebin.com/C50vS8E5
https://pastebin.com/x8G6uZzB
https://pastebin.com/xwqDW20Q
https://pastebin.com/bbrPiHh4
https://pastebin.com/KwcxyrV3
https://pastebin.com/8Yq1Mwpv
https://pastebin.com/rdXkVZnR
https://pastebin.com/X1QpPxj6
https://pastebin.com/rk4jhKwM
https://pastebin.com/AYH757Hd
https://pastebin.com/arrJtdPC
https://pastebin.com/x1a0QkrE
https://pastebin.com/ZPzMV6KV
https://pastebin.com/qZPAArab
https://pastebin.com/dDiiWq7r
https://pastebin.com/wH1CZA3w
https://pastebin.com/9CHyNi7a
https://pastebin.com/Wkzwp2vS
https://pastebin.com/MVpzkpRy
https://pastebin.com/4AuW9a4S
https://pastebin.com/42tkYqcy
https://pastebin.com/XeksS1ku
https://pastebin.com/L110ndKg
https://pastebin.com/4Axgsuj0
https://pastebin.com/eAA2mxp4
https://pastebin.com/aU4WMaqg
https://pastebin.com/f1hun2kM
https://pastebin.com/wiqMJgk3
https://pastebin.com/uEN8vUKX
https://pastebin.com/D5qiKNFB
https://pastebin.com/F65rvR1J
https://pastebin.com/rasLrbYn
https://pastebin.com/21n3MQq5
https://pastebin.com/bXDj2phg
https://pastebin.com/0PVahXVh
https://pastebin.com/7JtdfNGH
https://pastebin.com/0986iXre
https://pastebin.com/NUb3eRVe
https://pastebin.com/gvz7gKTZ
https://pastebin.com/UusP3Pyp
https://pastebin.com/QSMmWQi9
https://pastebin.com/jLtF7m6h
https://pastebin.com/csKNzJ6w
https://pastebin.com/wBeZafkw
https://pastebin.com/KESDZmpi
https://pastebin.com/mppjXxUF
https://pastebin.com/MepfrkCE
https://pastebin.com/ibwAybQc
https://pastebin.com/WuELbpHn
https://pastebin.com/hTM9NQ5N
https://pastebin.com/jj0Cp5Ux
https://pastebin.com/LzZFQwaZ
https://pastebin.com/tY8wgRRh
https://pastebin.com/XhgJKNVx
https://pastebin.com/zmytyHRD
https://pastebin.com/ps3FBe7w
https://pastebin.com/hKEiN00Z
https://pastebin.com/0krAGqa8
https://pastebin.com/bmN1vmY5
https://pastebin.com/tXEzETbZ
https://pastebin.com/sxvMRT7d
https://pastebin.com/PBYx1zjX
https://pastebin.com/YL3YtvW4
https://pastebin.com/wYkL6tyV
https://pastebin.com/G9yg2jbz
https://pastebin.com/YS9qiTPE
https://pastebin.com/zRzdakmk
https://pastebin.com/cPbmqAwR
https://pastebin.com/k6VnHXDj
https://pastebin.com/xaU4Lde8
https://pastebin.com/T3qhtZbi
https://pastebin.com/ti5Gv5Uy
https://pastebin.com/p9B6cLzp
https://pastebin.com/0McP0kzG
https://pastebin.com/QRsCLz7H
https://pastebin.com/rTnmVGim
https://pastebin.com/dirbSPyj
https://pastebin.com/QJ1gdh7k
https://pastebin.com/qgCPAp80
https://pastebin.com/H6YUkDVV
https://pastebin.com/YzEYse2d
https://pastebin.com/kUbmfLr0
https://pastebin.com/DmfhXUWK
https://pastebin.com/rxfuCib1
https://pastebin.com/wCR9BHRT
https://pastebin.com/rfJqDQph
https://pastebin.com/UVXE7g9P
https://pastebin.com/vB8FW842
https://pastebin.com/93JiaU8B
https://pastebin.com/40fKbQF1
https://pastebin.com/UpbtX9Vs
https://pastebin.com/GwhrB2rt
https://pastebin.com/CMt33cpf
https://pastebin.com/qPsRkEzz
https://pastebin.com/RR7a27Wk
https://pastebin.com/zU5CC8qs
https://pastebin.com/D3aJGYQk
https://pastebin.com/hx9ya1cr
https://pastebin.com/2GgcbCuP
https://pastebin.com/gYczTsfK
https://pastebin.com/VXsw9xTt
https://pastebin.com/R7DhK5Tq
https://pastebin.com/hHKGpdvA
https://pastebin.com/xREcgGy2

0 关注 分享

要回复文章请先登录注册