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
  }
}
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
  }
}
分享
微博
QQ
微信
回复
2024-12-25 15:35:55
相关问题
HarmonyOS RelativeContainer无法自适应高度
608浏览 • 1回复 待解决
WebView加载网页无法自适应
609浏览 • 1回复 待解决
HarmonyOS RichText自适应高度
183浏览 • 1回复 待解决
HarmonyOS 高度自适应
429浏览 • 1回复 待解决
HarmonyOS webview高度不能自适应
98浏览 • 1回复 待解决
HarmonyOS RichText能否自适应高度
190浏览 • 1回复 待解决
HarmonyOS Web高度自适应问题
978浏览 • 1回复 待解决
HarmonyOS popup宽度自适应
111浏览 • 1回复 待解决
HarmonyOS webview自适应屏幕
175浏览 • 1回复 待解决
HarmonyOS ui自适应问题
138浏览 • 1回复 待解决
HarmonyOS List高度根据内容自适应
338浏览 • 1回复 待解决
HarmonyOS RelativeContainer宽高自适应问题
935浏览 • 1回复 待解决
HarmonyOS Grid高度根据内容自适应
264浏览 • 1回复 待解决
HarmonyOS 高度自适应的问题
529浏览 • 1回复 待解决
HarmonyOS GridItem自适应高度问题
562浏览 • 1回复 待解决
HarmonyOS RelativeContainer自适应高度相关
228浏览 • 1回复 待解决
HarmonyOS grid如何自适应宽度
287浏览 • 1回复 待解决