kotlin 在jetpack中将一列分为2行

omqzjyyz  于 2023-02-05  发布在  Kotlin
关注(0)|答案(3)|浏览(166)

我想把一列长文本分成两个相同高度和宽度的行。这是为了防止文本太长。有没有可能把一列长文本分成两行?
我来不了,我不知道。

ifmq2ha2

ifmq2ha21#

使用重量

Column() {
        Row(
            Modifier.weight(1f).background(Blue)
        ){
            Text(text = "Weight = 1", color = Color.White)
        }
        Row(
            Modifier.weight(1f).background(Yellow)
        ) {
            Text(text = "Weight = 1")
        }

}
xienkqul

xienkqul2#

可以使用row并设置修改器weight: 1f

Row {
        TextField(
            value = "Test Test Test Test Test Test Test Test Test Test Test Test" +
                    " Test Test Test Test Test Test Test Test ",
            onValueChange = {},
            modifier = Modifier.weight(1f)
        )
        TextField(
            value = "Test Test Test Test Test Test TesTest Test Test Test " +
                    "Test Test Test Test Test Test Test Test Test ",
            onValueChange = {},
            modifier = Modifier.weight(1f)
        )
    }

如果您希望在下一行中有第二个TextField(如果它正在换行),则可以使用FlowRow
https://google.github.io/accompanist/flowlayout

nuypyhwy

nuypyhwy3#

看看这个,这可以通过使用box来完成。

Box(modifier = Modifier.fillMaxWidth()) {
            var size by remember { mutableStateOf(Size.Zero) }
            val height: @Composable () -> Dp = { with(LocalDensity.current) { size.height.toDp() } }
            Text(
                text = longText,
                modifier = Modifier
                    .padding(8.dp)
                    .fillMaxWidth(.45F)
                    // if second text is larger than first text call it on second text
                    .onGloballyPositioned { size = it.size.toSize() },
                textAlign = TextAlign.Justify
            )
    
            Divider(
                modifier = Modifier
                    .width(1.dp)
                    .height(height())
                    .align(Alignment.Center),
                color = Color.Black
            )
    
            Text(
                text = longText,
                modifier = Modifier
                    .padding(8.dp)
                    .align(Alignment.TopEnd)
                    .fillMaxWidth(.45F),
                textAlign = TextAlign.Justify
            )
}

相关问题