【问题标题】:Jetpack compose. Center icon in ListItem喷气背包组成。 ListItem 中的中心图标
【发布时间】:2021-10-27 17:43:49
【问题描述】:

如何在 ListItem 中正确居中图标?

Column {
    ListItem(
        icon = {
            Icon(
                imageVector = Icons.Outlined.Delete,
                contentDescription = null
            )
        },
        secondaryText = { Text("secondaryText") },
        text = { Text("text") }
    )
    Divider()
}

给出这个结果

ListItem 实现使用显式 Box(contentAlignment = Alignment.TopStart) 来放置图标。嗯……为什么?

当然,我可以使用填充或创建自定义 ListItem,但必须有一个标准的解决方案。

【问题讨论】:

    标签: android android-jetpack android-jetpack-compose


    【解决方案1】:

    您可以将Modifier.size(40.dp)添加到Icon

    类似:

        ListItem(
            text = { Text("Two line list item with 40x40 icon") },
            secondaryText = { Text("Secondary text") },
            icon = {
                Icon(
                    Icons.Filled.Favorite,
                    contentDescription = null,
                    modifier = Modifier.size(40.dp)
                )
            }
        )
    

    ListItem 跟在material guidelines 后面:

    【讨论】:

    • 为什么是 40?这个神奇的数字是从哪里来的?
    • @vitidev 我已经更新了答案,添加了材料文档的链接。
    • 我明白了。尽管根据您的链接,在“内容类型”部分中有一个小图标居中的示例。事实证明 ListItem 不是很通用,因为它们本可以提供居中的机会
    • 您还可以添加 .padding(8.dp) 以实现 24dp 图标居中
    猜你喜欢
    • 2022-06-12
    • 2022-06-17
    • 1970-01-01
    • 1970-01-01
    • 2022-10-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-03-24
    相关资源
    最近更新 更多