HarmonyOS RelativeContainer容器的宽高如何根据子组件确定

RelativeContainer容器不指定宽高, 根据子组件的尺寸(也是根据内容确定的), 如何让RelativeContainer根据子组件的内容确定自己的宽高。

Stack(){  
  Stack(){  
    RelativeContainer() {  
      Text('123')  
        .id('tv')  
        .width(100)  
        .height(100)  
    }  
    .backgroundColor(Color.Blue)  
  }  
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
HarmonyOS
2024-09-24 12:14:33
989浏览
收藏 0
回答 1
回答 1
按赞同
/
按时间
put_get

RelativeContainer支持宽高自适应子组件,将其设置为auto即可,但限制是当width设置auto时,如果水平方向上子组件以容器作为锚点,则auto不生效,垂直方向上同理。

就像下面情况 Y方向定位不设置,才会自适应height。(水平,竖直方向如果都设置定位,那么父组件这时还是自适应状态等于没有)。

build() {  
  Stack(){  
    Stack(){  
      RelativeContainer() {  
        Text('123')  
          .id('tv')  
          .alignRules({  
            // top: {anchor: "__container__", align: VerticalAlign.Top},  
            left: {anchor: "__container__", align: HorizontalAlign.Start},  
          })  
          .height(100)  
          .width(200)  
      }  
      .width('auto')  
      .height('auto')  
      .backgroundColor(Color.Blue)  
    }  
  }  
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
分享
微博
QQ
微信
回复
2024-09-24 17:34:01
相关问题
RelativeContainer容器不支持自动
961浏览 • 1回复 待解决
HarmonyOS RelativeContainer设置问题
520浏览 • 1回复 待解决
HarmonyOS RelativeContainer 不能自适应
1060浏览 • 1回复 待解决
HarmonyOS RelativeContainer自适应问题
1688浏览 • 1回复 待解决
HarmonyOS 如何得到父容器尺寸
756浏览 • 1回复 待解决
ArkTs如何获取组件
5782浏览 • 1回复 待解决
HarmonyOS 如何获取组件大小,
1256浏览 • 1回复 待解决
HarmonyOS如何获取指定子组件
2081浏览 • 1回复 待解决
如何获取组件,你学会了吗?
3517浏览 • 1回复 待解决
求大佬告知如何获取组件
1039浏览 • 1回复 待解决