HarmonyOS RelativeContainer容器如何根据子组件的高度进行自动设置高度?

RelativeContainer容器内容有一个text组件,组件内容可不固定,所有高度也不固定,默认RelativeContainer组件的高度是屏幕高度,这个可以设置为自动高度吗?

HarmonyOS
2024-12-24 15:17:30
7072浏览
收藏 0
回答 1
回答 1
按赞同
/
按时间
aquaa

从API Version 11开始,RelativeContainer组件的width、height设置auto表示自适应子组件的宽、高;但需要注意的是当width设置auto时,如果水平方向上子组件以容器作为锚点,则auto不生效,垂直方向上同理。

简单示例代码如下:

@Entry
@Component
struct Index3 {
  @State message: string = 'Hello World';

  build() {
    RelativeContainer() {
      Text(this.message)
        .id('Index3HelloWorld')
        .fontSize(50)
        .fontWeight(FontWeight.Bold)
        .alignRules({
          center: { anchor: '__container__', align: VerticalAlign.Center },
          //当width设置auto时,如果水平方向上子组件以容器作为锚点,则auto不生效
          // middle: { anchor: '__container__', align: HorizontalAlign.Center }
        })
        .width('90%')
    }
    .height('100%')
    .width('auto')
    .backgroundColor(Color.Orange)
  }
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.

relativecontainer的规则说明:https://developer.huawei.com/consumer/cn/doc/harmonyos-references-V5/ts-container-relativecontainer-V5#规则说明

分享
微博
QQ
微信
回复
2024-12-24 17:51:38
相关问题
HarmonyOS RelativeContainer自适应高度相关
744浏览 • 1回复 待解决
HarmonyOS 设置list组件高度
572浏览 • 1回复 待解决
HarmonyOS RelativeContainer无法自适应高度
1257浏览 • 1回复 待解决