最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

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

programmeradmin0浏览0评论
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:

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论