android - 等价于约束布局 0dp

标签 android android-jetpack-compose

我一直在尝试使用 compose 实现以下布局:
enter image description here
为此,我创建了可组合:

@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)
                }
        )
    }
}
但是,可组合的文本不会填满整个空间,并且 UI 看起来像:
enter image description here
我尝试向 Text 添加修饰符可组合,如 Modifier..fillMaxWidth() ,但这会导致:
enter image description here
我也尝试过使用带有水平链的约束集,但无济于事。我知道删除 end.linkTo(icon.start)看起来这是可以实现的,但是当文本很长时,它会与删除图标重叠。
我在这里想念什么?当我们说 TextView 时,如何获得与 View 系统中相同的结果的宽度为 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
    },
)

关于android - 等价于约束布局 0dp,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64313409/

相关文章:

android - Jetpack Compose LazyColumn 使用 remember() 重构

android-jetpack-compose - 如何仅显示几秒钟的可组合项?

android - 如何在 Jetpack 撰写的 Horizo​​ntalPager 中检测滑动?

android - 在哪里可以找到/浏览应用程序数据文件夹中的 cookie?

android - IllegalAccessException : can not access a member of class com. android.build.gradle.tasks.ManifestProcessorTask

android - 我在 Android Q 中获取的 IMEI 为空?

android - 如何在 GPS LocationListener 中将 Android 文本转语音?

android - 将数据传递给 Android Compose 中的前一个可组合对象

android - Android Jetpack Compose 是否支持工具栏小部件?

android - 如何在 Android 中制作类似 "Notification"类的类?