android - How to force a second line in two texts that are displayed in the left and right margin when the first text is too lar

Row {Text(modifier = Modifier.weight(1f).padding(top = 16.dp, bottom = 8.dp),text = first.toAnnotatedSt

Row {
        Text(
            modifier = Modifier
                .weight(1f)
                .padding(top = 16.dp, bottom = 8.dp),
            text = first.toAnnotatedString(),
            style = style
        )
        Text(
            modifier = Modifier.padding(top = 16.dp, bottom = 8.dp),
            text = second.toAnnotatedString(),
            style = style
        )
    }

I am using the following code to make two texts display, one aligned to the right margin and the other aligned to the left margin:

When the font size is small, the content is displayed as expected, as seen in the image (green box):

But when you change the font size, the text on the left looks horrible:

How could I make it so that when the text is too large, the content on the left is displayed on a single line and the content on the right is displayed on another line?

Row {
        Text(
            modifier = Modifier
                .weight(1f)
                .padding(top = 16.dp, bottom = 8.dp),
            text = first.toAnnotatedString(),
            style = style
        )
        Text(
            modifier = Modifier.padding(top = 16.dp, bottom = 8.dp),
            text = second.toAnnotatedString(),
            style = style
        )
    }

I am using the following code to make two texts display, one aligned to the right margin and the other aligned to the left margin:

When the font size is small, the content is displayed as expected, as seen in the image (green box):

But when you change the font size, the text on the left looks horrible:

How could I make it so that when the text is too large, the content on the left is displayed on a single line and the content on the right is displayed on another line?

Share Improve this question asked Mar 13 at 10:43 A. CedanoA. Cedano 1,00117 silver badges50 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 2

A row is expanding only on the horizontal axis, so if you want to put more than one line in a single row, it can't succeed. You can use a FlowRow composable that helps to have more than one line, like this:

@OptIn(ExperimentalLayoutApi::class)
@Composable
fun ExpandableText(modifier: Modifier = Modifier) {
    FlowRow(
        modifier = modifier.fillMaxWidth(),
        horizontalArrangement = Arrangement.SpaceBetween
    ) {
        Text(
            modifier = Modifier.padding(top = 16.dp, bottom = 8.dp),
            text = "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed non risus.",
            style = MaterialTheme.typography.bodyLarge,
            overflow = TextOverflow.Ellipsis,
            maxLines = 1
        )

        Text(
            modifier = Modifier.padding(top = 16.dp, bottom = 8.dp),
            text = "Show more",
            style = MaterialTheme.typography.bodyLarge,
        )
    }
}

Result Image for multiple lines:

@OptIn(ExperimentalLayoutApi::class)
@Composable
fun ExpandableText(modifier: Modifier = Modifier) {
    FlowRow(
        modifier = modifier.fillMaxWidth(),
        horizontalArrangement = Arrangement.SpaceBetween
    ) {
        Text(
            modifier = Modifier
                .padding(top = 16.dp, bottom = 8.dp),
            text = "Lorem ipsum ",
            style = MaterialTheme.typography.bodyLarge,
            overflow = TextOverflow.Ellipsis,
            maxLines = 1
        )

        Text(
            modifier = Modifier

                .padding(top = 16.dp, bottom = 8.dp),
            text = "Show more",
            style = MaterialTheme.typography.bodyLarge,
        )
    }
}

Result:

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744706885a4589105.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信