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
相关产品推荐
相关产品推荐

