如何自定义 IconButton 的颜色

10

我想自定义 IconButton 的颜色,而不是使用 TopAppBar 上设置的默认值,但在 android.compose.material 中没有插槽API可以更改它。

    Scaffold(
        topBar = {
            TopAppBar(
                title = {
                    Text(text = "LayoutsCodelab")
                },
                actions = {
                    IconButton(onClick = { /* doSomething() */ }) {  // <- why it has the theme color and how to custom it.
                        Icon(Icons.Filled.Favorite)
                    }
                }
            )
        }
    )
1个回答

14
你可以在 Icon 组合中使用 tint 参数:
actions = {
    IconButton(onClick = { /* doSomething() */ }) {
        Icon(Icons.Filled.Add,
            "contentDescription",
            tint = Color.Red)
    }
}

输入图像描述此处


背景颜色怎么样? - acmpo6ou
使用background修饰符。例如:modifier = Modifier.background(Yellow) - Gabriele Mariotti
谢谢,我试过了。但是我的图标是圆形的,而background修饰符会产生一个正方形的背景。由于IconButton没有shape参数,所以我无法将其变成圆形。 - acmpo6ou
2
在这种情况下,您需要使用以下代码来裁剪背景:modifier = Modifier.background(Yellow,CircleShape) - Gabriele Mariotti

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接