文档开发者中心
我的
|

UI

HarmonyOS 5

RelativeContainer如何自适应高度

已解决
收起
自动换行
深色代码主题
复制
List({ scroller: this.scroller }) { // 设置列表项间距[2](@ref)
  ForEach(this.processList, (item: AlarmProcessLog, index: number) => {
    ListItem() {
      RelativeContainer() {
        Column() {
          Divider().width(2).backgroundColor("#6CB8F9").vertical(true).height(20)
          Image($r("app.media.alarm_process")).fitOriginalSize(true).width(40).height(40)
          Divider().width(2).backgroundColor("#6CB8F9").vertical(true)
        }.id("left").margin({ left: 20, right: 20 }).alignRules({
          // 'top': { 'anchor': '__container__', 'align': VerticalAlign.Top },
          // 'bottom': { 'anchor': 'right', 'align': VerticalAlign.Bottom },
          'left': { 'anchor': '__container__', 'align': HorizontalAlign.Start },
        })

        Column() {
          Row() {
            Text(item.createTime).fontColor("#8C8C8C").fontSize(14)
            Text(item.actionName).fontColor($r("app.color.btn_color_blue")).fontSize(14).margin({ left: 8 })
          }

          Text(item.deptName + "|" + item.createUserName).fontColor(Color.Black).fontSize(15).margin({ top: 10 })
          Text(item.content.replace("<br/>", "\n"))
            .fontColor("#595959")
            .fontSize(15)
            .padding(7)
            .margin({ top: 10 })
            .backgroundColor("#F5F5F5")
            .onClick(() => {
              if (item.action == "2207" || item.action == "2209") {
                router.pushUrl({ url: RouterContent.AlarmDetailPage, params: { "alarmId": item.id } })
              }
            })
        }.id("right").alignItems(HorizontalAlign.Start).margin({ top: 25, left: 20 }).alignRules({
          // 'top': { 'anchor': '__container__', 'align': VerticalAlign.Top },
          'right': { 'anchor': '__container__', 'align': HorizontalAlign.End },
          'left': { 'anchor': "left", 'align': HorizontalAlign.End },
        }).height('auto')
      }.height('auto')
    }.height('auto')
  })
}
.scrollBar(BarState.Off) //滚动条配置
.listDirection(Axis.Vertical)
.width('auto')
.height('auto')
.layoutWeight(1)

我需要RelativeContainer能根据内容高度自适应,该如何实现,

cke_2190.png

如图。左边的高度要等于右边的高度,RelativeContainer要能自适应右边的高度。现在一个listitem就是一整个屏幕的高度

点赞
收藏
1
回复
1
分享
举报
浏览504 发布于2025-04-28 07:40江苏
全部评论
最多点赞
最新发布
最早发布
|
采纳答复

您好,可以采取以下方式解决:

【背景知识】
RelativeContainer为相对布局组件,用于元素对齐的布局。其支持容器内部的子组件设置相对位置关系,对多个子组件进行对齐和排列。子组件可以指定兄弟组件或父容器作为锚点,基于锚点进行相对位置布局。

【解决方案】

子组件不设置高度时,默认撑满父组件布局,导致子组件全屏问题。可以通过onAreaChange事件动态获取并设置对应组件宽高属性,然后实时渲染,达到预期布局效果。
示例代码如下:

收起
自动换行
深色代码主题
复制
struct Index {
  @State knowLedgeList: string[] = ['1', '2', '3', '4', '5', '6', '7', '8']
  @State heightArr: number[] = []

  build() {
    Column() {
      List() {
        ForEach(this.knowLedgeList, (model: string, index: number) => {
          ListItem() {
            RelativeContainer() {
              Line()
                .width(0.5)
                .height(this.heightArr[index] + 2)
                .backgroundColor(Color.Black)
                .id(`line${index}`)
                .margin({ left: 20, top: 3 })
              Text(`mode${index}:RelativeContainer自适应宽高示例`)
                .fontSize(25)
                .offset({ x: 20, y: 0 })
                .margin({
                  top: '0.00px',
                  right: '50.00vp',
                  bottom: '0.00px',
                  left: '0.00px'
                })
                .alignRules({
                  top: { anchor: `line${index}`, align: VerticalAlign.Top },
                  left: { anchor: `line${index}`, align: HorizontalAlign.End },
                })
                .onAreaChange((oldValue: Area, newValue: Area) => {
                  this.heightArr[index] = Number(newValue.height)
                })
              Text('')
                .offset({ x: -10, y: 0 })
                .backgroundColor(Color.Green)
                .width('20.00vp')
                .height('20.00vp')
                .borderRadius(40)
                .alignRules({
                  top: { anchor: `line${index}`, align: VerticalAlign.Top },
                  left: { anchor: `line${index}`, align: HorizontalAlign.Start },
                })
            }.height('auto').width('auto').margin({ left: 10, right: 10 })
          }
        })
      }.width('100%').height('100%').margin({ top: 10, left: 10 })
    }
  }
}
展开全部
2楼编辑于2025-07-07 03:31 来自广东
写回答
新增插入模板功能
一键使用模板,快速填写内容,轻松发帖~
知道了
  • 为了保障您的信息安全,请勿上传您的敏感个人信息(如您的密码等信息)和您的敏感资产信息(如关键源代码、签名私钥、调试安装包、业务日志等信息),且您需自行承担由此产生的信息泄露等安全风险。
  • 如您发布的内容为转载内容,请注明内容来源。

我要提问题

了解社区公约,与您携手共创和谐专业的开发者社区。