HarmonyOS Text无法自适应

@Entry
@Component
struct TestPage {
  commentsList: CommentList[] = []

  build() {
    List() {
      ForEach(this.commentsList, (item: CommentList, index: number) => {
        ListItem() {
          Row() {
            Image($r('app.media.head'))
              .alt($r('app.media.head'))
              .height(20).width(20)
              .margin(8)
            Text() {
              Span(item.name + ':')
                .fontColor($r('app.color.AAAAAA'))
                .fontSize('14fp')
              Span(item.content)
                .fontColor($r('app.color.333333'))
                .fontSize('14fp')
            }
            .margin({ top: 10, bottom: 10, right: 12 })

          }
          .margin({
            top: 5,
            bottom: 5,
            left: 20,
            right: 20
          })
          .borderRadius(4)
          .backgroundColor($r('app.color.F5F5F5'))
        }
      })
    }
    .scrollBar(BarState.Off)
    .width('100%')
    .height('100%')
    .margin({ top: 50 })
  }

  aboutToAppear(): void {
    this.commentsList.push(new CommentList('111', 'xxx'))
    this.commentsList.push(new CommentList('222', 'xxx'))
    this.commentsList.push(new CommentList('333', 'xxx'))
    this.commentsList.push(new CommentList('555', 'xxx'))
  }
}

@Observed
export class CommentList {
  name: string
  content: string

  constructor(name: string, content: string) {
    this.name = name
    this.content = content
  }
}
  • 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.
  • 34.
  • 35.
  • 36.
  • 37.
  • 38.
  • 39.
  • 40.
  • 41.
  • 42.
  • 43.
  • 44.
  • 45.
  • 46.
  • 47.
  • 48.
  • 49.
  • 50.
  • 51.
  • 52.
  • 53.
  • 54.
  • 55.
  • 56.
  • 57.
  • 58.
  • 59.
  • 60.
HarmonyOS
2024-12-25 14:20:29
浏览
收藏 0
回答 1
回答 1
按赞同
/
按时间
put_get

Row布局时若子组件不设置flexShrink则默认不会压缩子组件,即所有子组件主轴大小累加可超过容器主轴。可以对子组件设置layoutWeight(1)进行解决或者设置flexShrink来解决row中子组件超过row容器的情况。但是让子组件重新布局这种情况就会出现红框没有去除的问题,无法避免。目前想到解决方案是去除左右间距来实现想要的效果,参考示例:

@Entry
@Component
struct TestPage {
  commentsList: CommentList[] = []

  build() {
    List() {
      ForEach(this.commentsList, (item: CommentList, index: number) => {
        ListItem() {
          Row() {
            Image($r('app.media.sun'))
              .alt($r('app.media.startIcon'))
              .height(20).width(20)// .margin(8)
              .margin({
                top: 8,
                bottom: 8,
                // left: 8
              })
            Text() {
              Span(item.name + ':')
                .fontColor(Color.Red)
                .fontSize('14fp')
              Span(item.content)
                .fontColor(Color.Yellow)
                .fontSize('14fp')
            }
            .margin({
              top: 10,
              bottom: 10,
              right: 12
            })

          }
          .margin({
            top: 5,
            bottom: 5,
            // left: 20,
            right: 20
          })
          .borderRadius(4)
          .backgroundColor(Color.Black)
        }
      })
    }
    .scrollBar(BarState.Off)
    .width('100%')
    .height('100%')
    .margin({ top: 50 })
  }

  aboutToAppear(): void {
    this.commentsList.push(new CommentList('111', 'xxx'))
    this.commentsList.push(new CommentList('222', 'xxx'))
    this.commentsList.push(new CommentList('333',
      'xxx'))
    this.commentsList.push(new CommentList('555',
      'xxx'))
  }
}

@Observed
export class CommentList {
  name: string
  content: string

  constructor(name: string, content: string) {
    this.name = name
    this.content = content
  }
}
  • 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.
  • 34.
  • 35.
  • 36.
  • 37.
  • 38.
  • 39.
  • 40.
  • 41.
  • 42.
  • 43.
  • 44.
  • 45.
  • 46.
  • 47.
  • 48.
  • 49.
  • 50.
  • 51.
  • 52.
  • 53.
  • 54.
  • 55.
  • 56.
  • 57.
  • 58.
  • 59.
  • 60.
  • 61.
  • 62.
  • 63.
  • 64.
  • 65.
  • 66.
  • 67.
  • 68.
  • 69.
  • 70.
分享
微博
QQ
微信
回复
2024-12-25 15:35:55
相关问题
HarmonyOS RelativeContainer无法自适应高度
1271浏览 • 1回复 待解决
WebView加载网页无法自适应
1267浏览 • 1回复 待解决
HarmonyOS RichText自适应高度
758浏览 • 1回复 待解决
HarmonyOS 高度自适应
1053浏览 • 1回复 待解决
HarmonyOS RichText能否自适应高度
652浏览 • 1回复 待解决
HarmonyOS webview高度不能自适应
676浏览 • 1回复 待解决
HarmonyOS Web高度自适应问题
1606浏览 • 1回复 待解决
HarmonyOS popup宽度自适应
679浏览 • 1回复 待解决
HarmonyOS ui自适应问题
694浏览 • 1回复 待解决
HarmonyOS webview自适应屏幕
827浏览 • 1回复 待解决
HarmonyOS RelativeContainer 不能自适应宽高
1060浏览 • 1回复 待解决
HarmonyOS RelativeContainer宽高自适应问题
1688浏览 • 1回复 待解决
HarmonyOS Grid高度根据内容自适应
847浏览 • 1回复 待解决
HarmonyOS List高度根据内容自适应
1031浏览 • 1回复 待解决
HarmonyOS RelativeContainer自适应高度相关
766浏览 • 1回复 待解决
HarmonyOS GridItem自适应高度问题
1083浏览 • 1回复 待解决
HarmonyOS webview如何设置自适应
1428浏览 • 1回复 待解决
HarmonyOS 高度自适应的问题
1054浏览 • 1回复 待解决
HarmonyOS grid如何自适应宽度
857浏览 • 1回复 待解决