相当于约束布局0dp

Equivalent of constraint layout 0dp

我一直在尝试使用 compose 实现以下布局:

为此,我创建了可组合项:

@Preview(showBackground = true)
@Composable
fun element() {
    ConstraintLayout(
        modifier = Modifier.fillMaxWidth()
    ) {
        val (checkbox, title, icon) = createRefs()

        Text(
            text = "This would be some text",
            style = TextStyle(
                color = Color.Black,
                fontSize = 18.sp,
            ),
            modifier = Modifier.constrainAs(title) {
                top.linkTo(parent.top)
                bottom.linkTo(parent.bottom)
                start.linkTo(checkbox.end)
                end.linkTo(icon.start)
            },
        )

        Checkbox(
            checked = false,
            modifier = Modifier.constrainAs(checkbox) {
                top.linkTo(title.top)
                bottom.linkTo(title.bottom)
                start.linkTo(parent.start)
            },
            onCheckedChange = {},
        )

        Icon(
            asset = Icons.Filled.Close,
            modifier = Modifier
                .constrainAs(icon) {
                    top.linkTo(title.top)
                    bottom.linkTo(title.bottom)
                    end.linkTo(parent.end)
                }
        )
    }
}

但是,文本可组合项并未填满整个 space,UI 看起来像:

我试过向 Text 可组合项添加修饰符,例如 Modifier..fillMaxWidth(),但这会导致:

我也尝试过使用带水平链的约束集,但无济于事。我知道删除 end.linkTo(icon.start) 看起来是可以实现的,但是当文本很长时它会与删除图标重叠。

我在这里错过了什么?当我们说 TextView 的宽度是 0dp 时,如何获得与视图系统相同的结果?

使用Dimension.fillToConstraints:

A Dimension that spreads to match constraints. Links should be specified from both sides corresponding to this dimension, in order for this to work.

将此行添加到您的 Text 修饰符中:

width = Dimension.fillToConstraints

所以变成:

Text(
    text = "This would be some text",
    style = TextStyle(
        color = Color.Black,
        fontSize = 18.sp,
    ),
    modifier = Modifier.constrainAs(title) {
        top.linkTo(parent.top)
        bottom.linkTo(parent.bottom)
        start.linkTo(checkbox.end)
        end.linkTo(icon.start)
        width = Dimension.fillToConstraints
    },
)