RelativeContainer无法自适应子组件?

阅读 707
1 个回答

在RelativeContainer组件中,width、height设置auto表示自适应子组件。当width设置auto时,如果水平方向上子组件以容器作为锚点,则auto不生效,垂直方向上同理。https://developer.huawei.com/consumer/cn/doc/harmonyos-references/ts-container-relativecontainer-0000001820880933

可以设置以兄弟组件为锚点,demo如下:

@Entry
@Component
struct Index {
  build() {
    Column(){
      RelativeContainer() {
        Row().width(100).height(100)
          .backgroundColor("#FF3333")
          .id("row1")

        Row().width(100).height(100)
          .backgroundColor("#FFCC00")
          .alignRules({
            top: {anchor: "__container__", align: VerticalAlign.Top},
            left: {anchor: "row1", align: HorizontalAlign.End}
          })
          .id("row2")
      }
      .width('auto').height('auto')
      .margin({left: 50})
      .border({width:2, color: "#6699FF"})
    }.width('100%')
    .height('100%')
  }
}
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进