【HarmonyOS NEXT】使用RelativeContainer布局页面,如何实现height自适应

文章介绍了在HarmonyOS中,如何使用RelativeContainer组件的API11及以上版本实现页面布局的高度自适应,通过设置width和height为auto,并利用Anchor对齐规则来调整子组件布局。
摘要由CSDN通过智能技术生成

 【关键字】

RelativeContainer / height / 自适应 / API 11

【问题描述】

使用RelativeContainer布局页面,如何设置height才能使布局高度动态适应展示的内容?

【解决方案】

从API Version 11开始,在RelativeContainer组件中,width、height设置auto表示自适应子组件,具体请参考:https://developer.huawei.com/consumer/cn/doc/harmonyos-references/ts-container-relativecontainer-0000001820880933#ZH-CN_TOPIC_0000001820880933__规则说明

示例代码如下:

@Entry
@Component
struct Index {

build() {
Column() {

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},
//top:{anchor:"row1", align:VerticalAlign.Top}
})
.id("row2")

Row().height(100).width(100)
.backgroundColor("#FF6633")
.alignRules({
left: {anchor: "row1", align: HorizontalAlign.Start},
top:{anchor:"row1", align:VerticalAlign.Bottom}
})
.id("row3")
}
.width("auto").height("auto")
.margin({left: 50})
.border({width:2, color: "#6699FF"})
//.padding({left:10, top:10, right:30, bottom:30});
}
.height('100%')
//.padding({left:10, top:10, right:10, bottom:10});
}
}

  • 8
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值