使用 Jetpack Compose 将文本换行到下一行

Wrap text to next line using Jetpack Compose

我创建了一个约束布局

 ConstraintLayout(
        Modifier
            .fillMaxWidth()
            .wrapContentHeight()
    ) {

        Image(
            painter = rememberImagePainter(it.photoUrl.toString()),
            modifier = Modifier
                .constrainAs(photo) {
                    start.linkTo(parent.start, margin = 30.dp)
                    top.linkTo(parent.top, margin = 30.dp)
                }
                .width(130.dp)
        )

        Column(
            modifier = Modifier
                .constrainAs(holder) {
                    start.linkTo(photo.end, margin = 30.dp)
                    top.linkTo(photo.top)
                    bottom.linkTo(photo.bottom)
                    end.linkTo(parent.end, margin = 30.dp)
                }
                .fillMaxWidth()
                .wrapContentHeight(),
            horizontalAlignment = Alignment.CenterHorizontally
        ) {
            Text(
                modifier = Modifier
                    .wrapContentWidth()
                    .wrapContentHeight(),
                text = "Lorem Ipsu auto text  ator..."
            )
}

如您所见,长文本不适合布局。如何将文本换行到下一行?我试图将 wrapContentWidht() 更改为 fillMaxParentWidth 但 id 没有帮助。

您可以使用 width = Dimension.fillToConstraints 将宽度限制为可用 space。

constrainAs块内

在你的例子中:

.constrainAs(holder) {
   start.linkTo(photo.end, margin = 30.dp)
   top.linkTo(photo.top)
   bottom.linkTo(photo.bottom)
   end.linkTo(parent.end, margin = 30.dp)
   width = Dimension.fillToConstraints
}