Jetpack Compose:检查文本最大行数

问题描述 投票:0回答:2

我有一个场景,如果文本最大行数超过 9,我需要显示一个按钮,否则该按钮不应该出现。

我尝试查看 Android 开发者指南,但找不到任何解决方案。

下面是我的代码:

                    Text(
                        //text = item.content,
                        text = "Lorem Ipsum is simply dummy text of the printing and typesetting industry. " +
                                "Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, " +
                                "when an unknown printer took a galley of type and scrambled it to make a type specimen book. " +
                                "It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software " +
                                "like Aldus PageMaker including versions of Lorem Ipsum.",

                        modifier = Modifier
                            .padding(bottom = 4.dp),
                        fontSize = 16.sp,
                        //maxLines = 6,
                        textAlign = TextAlign.Start,
                        style = MaterialTheme.typography.body1,
                    )

                
android android-jetpack-compose
2个回答
6
投票

以下对我有用:

Column {
    var textOverflow by remember { mutableStateOf(false) }
    Text(
        modifier = Modifier.size(50.dp),
        text = "1234567891",
        fontSize = 16.sp,
        maxLines = 2,
        onTextLayout = { textLayoutResult ->
            textOverflow = textLayoutResult.hasVisualOverflow
        },
        textAlign = TextAlign.Start,
        style = MaterialTheme.typography.body1,
    )
    if(textOverflow) {
        Button(onClick = {}) {
            Text("Hello")
        }
    }
}

0
投票

查看这篇媒体文章是否有相同的内容:

Jetpack Compose:根据文本行有条件地显示“阅读更多”按钮

https://medium.com/@manuyadav644/jetpack-compose-conditionally-display-read-more-button-based-on-text-lines-c9c0c5f65556

© www.soinside.com 2019 - 2024. All rights reserved.