HarmonyOS RelativeContainer高度怎么设置auto,自适应子控件的高

一个item的高无法控制,想要实现跟随子控件的高一样,怎么实现。

HarmonyOS
2024-12-20 16:58:42
浏览
收藏 0
回答 1
回答 1
按赞同
/
按时间
put_get

当width设置auto时,如果水平方向上子组件以容器作为锚点,则auto不生效,垂直方向上同理。 这边代码不能以__container__为锚点。可以参考文档规则说明:https://developer.huawei.com/consumer/cn/doc/harmonyos-references-V5/ts-container-relativecontainer-V5

参考demo示例:

@Component
export struct CommentItem {
  index: number = 0

  build() {
    Column() {
      RelativeContainer() {
        Image($r("app.media.xxx")).width(30).height(30).id("ivHead1" + this.index)

        Image($r("app.media.xxx")).width(30).height(30).id("ivHead2" + this.index).alignRules({
          top: { anchor: "ivHead1" + this.index, align: VerticalAlign.Top },
          left: { anchor: "ivHead1" + this.index, align: HorizontalAlign.End }
        })

        Image($r("app.media.xxx")).width(30).height(30).id("ivHead3" + this.index).alignRules({
          top: { anchor: "ivHead1" + this.index, align: VerticalAlign.Bottom },
          left: { anchor: "ivHead1" + this.index, align: HorizontalAlign.Start }
        })
        Image($r("app.media.xxx")).width(30).height(30).id("ivHead4" + this.index).alignRules({
          top: { anchor: "ivHead2" + this.index, align: VerticalAlign.Bottom },
          left: { anchor: "ivHead3" + this.index, align: HorizontalAlign.End }
        })

      }.width("auto").height("auto").padding(13)
    }.width('100%')

  }
}


@Entry
@Component
struct Index3 {

  build() {
    Column() {
      List() {
        ForEach([1, 2, 3], (item: number, index: number) => {
          ListItem() {
            CommentItem({ index: index })
          }
        })
      }
    }.height("100%")
  }
}
  • 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.
分享
微博
QQ
微信
回复
2024-12-20 18:23:20


相关问题
HarmonyOS RelativeContainer无法自适应高度
1267浏览 • 1回复 待解决
HarmonyOS RelativeContainer 不能自适应
1057浏览 • 1回复 待解决
HarmonyOS RelativeContainer自适应问题
1676浏览 • 1回复 待解决
HarmonyOS RelativeContainer自适应高度相关
766浏览 • 1回复 待解决
HarmonyOS web组件怎么自适应高度
754浏览 • 1回复 待解决
HarmonyOS List是否可以设置自适应高度
640浏览 • 1回复 待解决
HarmonyOS 高度自适应
1043浏览 • 1回复 待解决
HarmonyOS RichText自适应高度
748浏览 • 1回复 待解决
HarmonyOS web高度自适应内容高度
1170浏览 • 1回复 待解决
HarmonyOS 高度自适应问题
1036浏览 • 1回复 待解决