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

android - How to create dynamic, evenly sized containers in a row, depending on their contents in Compose? - Stack Overflow

programmeradmin0浏览0评论

I want to create a circular component which will contain a number (0..99), and will be displayed in a row of 5.

I need the circles to grow in size with their contents, depending on the number itself (1 takes up much less space than 88) and font size.

So far, I got the elements to "behave" when the font size is regular sized, but I can't get them to display properly when the font is larger. The number either "grows" outside of the circle container, or the container gets distorted entirely.

Current - Normal:

Current - Large (note the numbers sticking out of the container)

This is the code for the above:

@Composable
fun TestBadges(numbers: List<Int>, modifier: Modifier = Modifier) {
    Row(
        modifier = modifier.fillMaxWidth(),
        horizontalArrangement = Arrangement.SpaceEvenly,
        verticalAlignment = Alignment.CenterVertically,
    ) {
        numbers.forEachIndexed { index, number ->
            Badge(
                number = number, modifier = Modifier.weight(1f),
            )
            if (index != numbers.size - 1) {
                BlueDivider()
            }
        }
    }
}

@Composable
private fun Badge(
    number: Int,
    modifier: Modifier = Modifier,
) {
    Box(
        modifier = modifier
            .aspectRatio(1f)
            .background(color = Color.Gray, shape = CircleShape),
        contentAlignment = Alignment.Center,
    ) {
        Text(
            text = number.toString(),
            style = MaterialTheme.typography.h4,
        )
    }
}

How do I achieve dynamically sized circles?

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论