You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Android Material3 Kotlin:TopAppBar无法设置背景色

解决Material3 TopAppBar无backgroundColor字段的问题

在Material3的TopAppBar组件中,没有直接的backgroundColor参数,背景色需要通过colors参数配置,具体使用TopAppBarDefaults.topAppBarColors()的containerColor属性来设置。

修改后的代码如下:

fun topbar() {
    TopAppBar(
        title = {
            Text(
                "TopBar",
                maxLines = 1,
                overflow = TextOverflow.Ellipsis
            )
        },
        navigationIcon = {
            IconButton(onClick = { /* doSomething() */ }) {
                Icon(
                    imageVector = Icons.Filled.Menu,
                    contentDescription = "Localized description"
                )
            }
        },
        actions = {
            IconButton(onClick = { /* doSomething() */ }) {
                Icon(
                    imageVector = Icons.Filled.Search,
                    contentDescription = "Localized description"
                )
            }
            IconButton(onClick = { /* doSomething() */ }) {
                Icon(
                    imageVector = Icons.Filled.Menu,
                    contentDescription = "Localized description"
                )
            }
        },
        // 配置背景色
        colors = TopAppBarDefaults.topAppBarColors(
            containerColor = Color.Red // 替换为你需要的颜色
        )
    )
}

如果需要使用主题内置颜色,也可以直接引用主题色:

containerColor = MaterialTheme.colorScheme.primary

内容的提问来源于stack exchange,提问作者Santosh b

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.02 18:35:49