HarmonyOS RelativeContainer组件的使用?

在List组件中的item 使用RelativeContainer组件 的高度设置为 auto 还是一条占一屏高度,如何让高度根据子空间的高度自适应

阅读 507
1 个回答

可以参考此demo如下:

@Entry
@Component
struct Index {

  build() {
    List() {
      ListItem() {
        RelativeContainer() {
          Row()
            .width(100)
            .height(100)
            .backgroundColor('#FF3333')
            .alignRules({})

            .id('row1')

          Row()
            .width(100)
            .height(100)
            .backgroundColor('#FFCC00')
            .alignRules({
              top: {
                anchor: 'row1',
                align: VerticalAlign.Top
              },
              left: {
                anchor: 'row1',
                align: HorizontalAlign.End
              },
            })
            .id('row2')

          Row()
            .height(100)
            .width(100)
            .backgroundColor('#FF6633')
            .alignRules({
              left: {
                anchor: 'row1',
                align: HorizontalAlign.Start
              },
              top: {
                anchor: 'row1',
                align: VerticalAlign.Bottom
              }
            })
            .id('row3')
        }

        .height('auto')
        .margin({
          left: 50
        })
        .border({
          width: 2,
          color: '#6699FF'
        })
      }

      ListItem() {
        RelativeContainer() {
          Row()
            .width(100)
            .height(100)
            .backgroundColor('#FF3333')
            .alignRules({})

            .id('row1')

          Row()
            .width(100)
            .height(100)
            .backgroundColor('#FFCC00')
            .alignRules({
              top: {
                anchor: 'row1',
                align: VerticalAlign.Top
              },
              left: {
                anchor: 'row1',
                align: HorizontalAlign.End
              },
            })
            .id('row2')

          Row()
            .height(100)
            .width(100)
            .backgroundColor('#FF6633')
            .alignRules({
              left: {
                anchor: 'row1',
                align: HorizontalAlign.Start
              },
              top: {
                anchor: 'row1',
                align: VerticalAlign.Bottom
              }
            })
            .id('row3')
        }

        .height('auto')
        .margin({
          left: 50
        })
        .border({
          width: 2,
          color: '#6699FF'
        })
      }

      ListItem() {
        RelativeContainer() {
          Row()
            .width(100)
            .height(100)
            .backgroundColor('#FF3333')
            .alignRules({})

            .id('row1')

          Row()
            .width(100)
            .height(100)
            .backgroundColor('#FFCC00')
            .alignRules({
              top: {
                anchor: 'row1',
                align: VerticalAlign.Top
              },
              left: {
                anchor: 'row1',
                align: HorizontalAlign.End
              },
            })
            .id('row2')

          Row()
            .height(100)
            .width(100)
            .backgroundColor('#FF6633')
            .alignRules({
              left: {
                anchor: 'row1',
                align: HorizontalAlign.Start
              },
              top: {
                anchor: 'row1',
                align: VerticalAlign.Bottom
              }
            })
            .id('row3')
        }

        .height('auto')
        .margin({
          left: 50
        })
        .border({
          width: 2,
          color: '#6699FF'
        })
      }
    }
    .backgroundColor('#FFF1F3F5')
    .alignListItem(ListItemAlign.Center)
  }
}
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进