如何自定义IconButton的颜色

ccd*_*ccd 12 android android-jetpack-compose android-compose-button

我想自定义 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)
                    }
                }
            )
        }
    )
Run Code Online (Sandbox Code Playgroud)

Gab*_*tti 15

tint您可以在可组合项中使用该参数Icon

actions = {
    IconButton(onClick = { /* doSomething() */ }) {
        Icon(Icons.Filled.Add,
            "contentDescription",
            tint = Color.Red)
    }
}
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

  • @acmpo6ou 在这种情况下,您必须使用“modifier = Modifier.background(Yellow,CircleShape)”来剪辑背景。 (2认同)