You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Row中子项为ConstraintLayout时的高度填充及宽度占满问题

Row嵌套ConstraintLayout时的适配问题

我在实现Row中子项填充高度的功能时,参考了常规方案(给Row设置height(IntrinsicSize.Min)启用固有尺寸测量),但替换为ConstraintLayout后该方案失效。

测试代码

@Composable
fun Test() {
    Row(
        modifier = Modifier
            .fillMaxWidth()
            .padding(horizontal = 16.dp)
            .background(
                Color.White,
                RoundedCornerShape(16.dp)
            )
            .clip(RoundedCornerShape(16.dp))
            .height(IntrinsicSize.Min) // 固有尺寸测量
    ) {
        Box(
            contentAlignment = Alignment.Center,
            modifier = Modifier
                .background(Color.Green)
                .width(20.dp)
                .fillMaxHeight()  // 尝试填充最大高度
        ) {
            // 空内容
        }
        ConstraintLayout(modifier = Modifier.weight(1f).background(Color.Red)) {
            val (title, perHour) = createRefs()
            Text(
                text = "Title",
                color = Color.White,
                modifier = Modifier
                    .constrainAs(title) {
                        width = Dimension.preferredWrapContent
                        top.linkTo(parent.top, 16.dp)
                        linkTo(
                            start = parent.start,
                            end = perHour.start,
                            bias = 0f,
                            startMargin = 10.dp,
                            endMargin = 12.dp
                        )
                    }
            )
            Text(
                text = "£20.87/H",
                modifier = Modifier
                    .constrainAs(perHour) {
                        top.linkTo(parent.top, 16.dp)
                        end.linkTo(parent.end, 24.dp)
                    }
            )
        }
    }
}

实际效果

效果截图

问题表现

  • 设置了fillMaxHeight()的绿色Box高度超过了红色ConstraintLayout的高度(若将ConstraintLayout替换为Box/Row/Column则正常)
  • 即使给ConstraintLayout设置了weight=1f,它也无法占满Row的可用宽度

内容的提问来源于stack exchange,提问作者user924

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.25 05:55:11