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

HarmonyOS
2024-12-18 16:05:43
浏览
收藏 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)
  }
}
分享
微博
QQ
微信
回复
2024-12-18 17:20:46
相关问题
HarmonyOS如何测量Text组件宽度
1213浏览 • 1回复 待解决
HarmonyOS Text如何设置最大宽度maxWidth
1855浏览 • 1回复 待解决
获取文本Text组件宽度
1516浏览 • 1回复 待解决
HarmonyOS Text内部Span宽度设置无效
941浏览 • 1回复 待解决
如何获取Text组件中文字宽度
3412浏览 • 1回复 待解决
Text文本过长如何实现上下滚动?
1916浏览 • 1回复 待解决
HarmonyOS Text获取每行数据宽度
764浏览 • 1回复 待解决
Text如何实现删除线功能?
3477浏览 • 1回复 待解决
HarmonyOS Text加载藏文,显示异常
765浏览 • 1回复 待解决
HarmonyOS text组件如何实现部分圆角
998浏览 • 1回复 待解决
ArkTS实现Text文本【...展开】
2847浏览 • 3回复 待解决
HarmonyOS Text.textOverflow超长显示异常
1339浏览 • 1回复 待解决
HarmonyOS Text文本描边实现
995浏览 • 1回复 待解决