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
相关产品推荐
相关产品推荐

