android Jetpack组合,可组合布局问题

hsvhsicv  于 2023-06-28  发布在  Android
关注(0)|答案(1)|浏览(136)

我有两个Text composable,我想把它们水平放置,我想确保第二个孩子得到它所需的宽度( Package 内容),然后把第一个孩子这样:如果第一个孩子的宽度+第二个孩子的宽度>父宽度,那么首先放置第二个孩子,然后将剩余的宽度给第一个孩子。
如果第一个子节点具有+第二子节点宽度<父节点宽度;这意味着在放置www.example.com之后有额外的宽度可用,在这种情况下,通过跳过额外的空间,将第二子放置在第一子的末尾。them.in that case place the 2nd child at the end of the 1st child,by skipping the extra space.
例如:在较大屏幕中:AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA AAAAAAAAA... 2ndTextComposable
我该如何做到这一点?
我尝试使用自定义布局,但一旦测量了孩子,我就无法更改孩子的宽度。

xzv2uavs

xzv2uavs1#

您必须在第一个文本上使用weight,并确保将fill参数设置为false

@Preview(widthDp = 420, showBackground = true)
@Composable
fun TwoText() {
    Column(
        modifier = Modifier.fillMaxWidth()
    ) {
        Row(
            modifier = Modifier.fillMaxWidth()
        ) {
            Text(
                text = "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Duis semper mauris mi, vel egestas tortor auctor quis. Proin lectus felis, elementum vel volutpat et, faucibus ut urna. ",
                maxLines = 1,
                modifier = Modifier.weight(1f, fill = false)
            )
            Text(
                text = "Second Text",
                color = Color.Red,
            )
        }
        Spacer(modifier = Modifier.height(16.dp))
        Row(
            modifier = Modifier.fillMaxWidth()
        ) {
            Text(
                text = "Short first text",
                maxLines = 1,
                modifier = Modifier.weight(1f, fill = false)
            )
            Text(
                text = "Second Text",
                color = Color.Red,
            )
        }
    }
}

相关问题