HarmonyOS UI布局子控件超出父控件宽度?

UI布局子控件超出父控件宽度,不知道是什么原因,想问下为什么子控件会超出父控件的宽度

.clip这个参数是把子控件的内容裁掉了,会显示半个字的情况,我想要的是文本到达右侧宽度后自动换行的效果

阅读 457
1 个回答

想要文本到达右侧宽度后自动换行可以使用layoutWeight,仅在Row/Column/Flex布局中生效。

https://developer.huawei.com/consumer/cn/doc/harmonyos-references-V5/ts-universal-attributes-size-V5\#layoutweight

demo:

Text('我的宽度超出了父容器宽度,父容器是红色背景我的宽度超出了父容器宽度,父容器是红色背景我的宽度超出了父容器宽度,父容器是红色背景我的宽度超出了父容器宽度,父容器是红色背景')
  .fontSize(14)
  .fontColor(Color.Black)
  .margin({ left: 5 })
  .width('100%')
  .layoutWeight(1)

@Builder
createItemView(title: string, content: string) {
  Row() {
    Text(title).fontSize(14).fontColor($r('app.color.start_window_background'))
    Text(content).fontSize(14).fontColor(Color.Black).margin({ left: 5 }).layoutWeight(1)
  }.width('100%').padding({ top: 10, bottom: 10 }).backgroundColor(Color.Red)
}
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进