Jetpack Compose:行中的多行文本,具有重量和换行文本内容

ade*_*111 9 android android-jetpack-compose

我有一个Row布局,里面有两个可组合项:

  • Text当内容很长时,它应该包裹自己的宽度并占用尽可能多的空间
  • Image每次都必须出现在右侧Text

为了实现这两个目标,我目前正在使用weight(weight = 1f, fill = false)Composable Text。它对于单行内容效果很好。但是,它会产生换行和换行多行文本的问题。为了看得更清楚,我添加了background(Color.Yellow)一些长话。参见参考代码:

Row {
    Text(
        text = "This is the message with quite super-unprecedented words",
        modifier = Modifier
            .background(Color.Yellow)
            .weight(
                weight = 1f,
                fill = false
            )
    )

    Image(
        painter = painterResource(R.drawable.image),
        contentDescription = null
    )
}
Run Code Online (Sandbox Code Playgroud)

结果如下: 屏幕

是否可以删除末尾的填充,Text以便也Image可以紧密贴合Text多行情况?或者有其他想法如何实现这一目标?我已经查看了softWrapforTextrequireSizefor的参数Image,但运气不佳。

小智 0

现在,您可以通过应用换行属性并将文本样式的严格性设置为宽松来实现此目的。

 style = MaterialTheme.typography.bodySmall.copy(
                fontSize = 8.sp,
                lineBreak = LineBreak.Paragraph.copy(strictness = LineBreak.Strictness.Loose)
            )
Run Code Online (Sandbox Code Playgroud)