【问题标题】:Set height of child elements equal to highest other element that are contained by Row设置子元素的高度等于 Row 包含的其他最高元素
【发布时间】:2022-02-04 19:47:01
【问题描述】:

当孩子的高度不固定时,是否可以强制 Row 重新绘制其孩子的高度以具有最大孩子的高度?

Row {
    Box {

    }

    Box {
        // second child has larger height, recompose first child to match height of the second child? 
    }
}

我最终得到类似:

我认为这是因为 Row 单独测量每个孩子,但我希望将第一个孩子重新组合以匹配第二个孩子的高度,一旦将第二个孩子添加到 Row 中。这可能吗?

【问题讨论】:

标签: android android-jetpack-compose


【解决方案1】:

您可以将Modifier.fillMaxHeight 添加到所有子视图,并通过将Modifier.height(IntrinsicSize.Max) 添加到父视图来将它们的高度限制为最大:

Row(
    Modifier
        .padding(10.dp)
        .height(IntrinsicSize.Max)
) {
    Text(
        "First",
        fontSize = 20.sp,
        modifier = Modifier
            .fillMaxHeight()
            .background(Color.Gray)
    )
    Text(
        "Second",
        fontSize = 30.sp,
        modifier = Modifier
            .fillMaxHeight()
            .background(Color.LightGray)
    )
}

结果:

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-03-23
    • 1970-01-01
    • 2019-11-04
    • 2013-09-17
    相关资源
    最近更新 更多