HarmonyOS 在Flex中有多个Text时,如何实现压缩某个Text的宽度

HarmonyOS
2024-12-18 16:05:43
815浏览
收藏 0
回答 1
回答 1
按赞同
/
按时间
fox280

可以使用flexShrink属性,配合textOverflow、wordBreak、maxLines,实现压缩Text的宽度和内容:

@Entry
@Component
struct AdaptiveText {
  build() {
    Flex({ direction: FlexDirection.Row }) {
      Text('flexShrink(2)flexShrink(2)flexShrink(2)')
        .flexShrink(2)
        .width(200)
        .height(100)
        .backgroundColor(0xF5DEB3)
        .textOverflow({ overflow: TextOverflow.Ellipsis })
        .wordBreak(WordBreak.BREAK_WORD)
        .maxLines(2)

      Text('no flexShrink')
        .width(200)
        .height(100)
        .backgroundColor(0xD2B48C)
        .textOverflow({ overflow: TextOverflow.Ellipsis })
        .wordBreak(WordBreak.BREAK_WORD)
        .maxLines(2)

      Text('flexShrink(6)flexShrink(6)flexShrink(6)')
        .flexShrink(6)
        .width(200)
        .height(100)
        .backgroundColor(0xF5DEB3)
        .textOverflow({ overflow: TextOverflow.Ellipsis })
        .wordBreak(WordBreak.BREAK_WORD)
        .maxLines(2)
    }.width("100%").height(120).padding(10).backgroundColor(0xAFEEEE)
  }
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
分享
微博
QQ
微信
回复
2024-12-18 17:20:46


相关问题
HarmonyOS如何测量Text组件宽度
962浏览 • 1回复 待解决
HarmonyOS Text如何设置最大宽度maxWidth
1229浏览 • 1回复 待解决
获取文本Text组件宽度
1235浏览 • 1回复 待解决
HarmonyOS Text内部Span宽度设置无效
700浏览 • 1回复 待解决
如何获取Text组件中文字宽度
3130浏览 • 1回复 待解决
Text文本过长如何实现上下滚动?
1598浏览 • 1回复 待解决
HarmonyOS Text获取每行数据宽度
546浏览 • 1回复 待解决
Text如何实现删除线功能?
2236浏览 • 1回复 待解决
HarmonyOS text组件如何实现部分圆角
730浏览 • 1回复 待解决
HarmonyOS Text加载藏文,显示异常
606浏览 • 1回复 待解决
ArkTS实现Text文本【...展开】
2556浏览 • 3回复 待解决
HarmonyOS Text文本描边实现
777浏览 • 1回复 待解决
HarmonyOS Flex组件宽度问题
1019浏览 • 1回复 待解决