HarmonyOS Text自适应内容宽度和自动省略

Row中有两个Text,前面的Text宽度自适应内容,但是如果内容过长要省略显示,后面的text宽度固定,但是位置紧随前面的text,右侧又不超过父视图,效果如图,请问如何实现

例如在其他系统自动布局中,可以设置

[self.mainTextLabel setContentCompressionResistancePriority:UILayoutPriorityRequired forAxis:UILayoutConstraintAxisVertical];
[self.mainTextLabel setContentHuggingPriority:UILayoutPriorityRequired forAxis:UILayoutConstraintAxisVertical];
  • 1.
  • 2.

来实现一行控件的伸缩优先级

HarmonyOS Text自适应内容宽度和自动省略 -鸿蒙开发者社区

HarmonyOS
2025-01-09 14:59:57
浏览
收藏 0
回答 1
回答 1
按赞同
/
按时间
Excelsior_abit

请参考demo

@Entry
@Component
struct Index {
  @State message: string = 'Hello World';

  build() {
    Flex() {
      Text('Hello World Hello World Hello World Hello World')
        .maxLines(1)
        .textOverflow({overflow: TextOverflow.Ellipsis})
        .flexBasis('auto')
        .height(20)

      Text('全民')
        // .flexBasis(40)
        .width(40)
        .height(20)
        .backgroundColor(0xD2B48C)
    }.width('100%').padding(10)
  }
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
分享
微博
QQ
微信
回复
2025-01-09 18:12:48


相关问题
HarmonyOS Image如何设置内容宽度自适应
445浏览 • 1回复 待解决
HarmonyOS Text无法自适应
372浏览 • 1回复 待解决
HarmonyOS popup宽度自适应
362浏览 • 1回复 待解决
HarmonyOS grid如何自适应宽度
538浏览 • 1回复 待解决
HarmonyOS List高度根据内容自适应
620浏览 • 1回复 待解决
HarmonyOS Grid高度根据内容自适应
475浏览 • 1回复 待解决
HarmonyOS web的高度自适应内容的高度
740浏览 • 1回复 待解决
HarmonyOS Grid组件能否自适应内容高度?
366浏览 • 1回复 待解决
HarmonyOS Grid自适应高度拖拽问题
1009浏览 • 1回复 待解决
List水平布局如何根据内容自适应高度
1175浏览 • 1回复 待解决
自定义弹窗大小如何自适应内容
2842浏览 • 1回复 待解决