问题的根本原因是由于LargeTopAppBar的容器默认情况下会覆盖containerColor的色值,从而导致设置的颜色无法生效。解决方法是需要使用Modifier来指定样式并将backgroundColor属性设置为containerColor,从而将指定的颜色应用到容器中。代码示例如下:
@Composable
fun CustomTopAppBar(title: String) {
val appBarColor = MaterialTheme.colors.primary
TopAppBar(
title = { Text(text = title, color = Color.White) },
navigationIcon = {
IconButton(onClick = { /* doSomething() */ }) {
Icon(Icons.Default.Menu, contentDescription = null, tint = Color.White)
}
},
modifier = Modifier.background(color = appBarColor),
)
}
@Composable
fun LargeTopAppBar() {
val containerColor = Color.Blue
Column(
modifier = Modifier.fillMaxSize(),
verticalArrangement = Arrangement.Top,
horizontalAlignment = Alignment.CenterHorizontally,
) {
CustomTopAppBar(title = "Large TopAppBar")
Text(text = "This is the body content", modifier = Modifier.background(
color = containerColor).fillMaxSize(),
)
}
}
上一篇:AndroidComposeKtor报错Lio/ktor/utils/io/NativeUtilsJvmKt
下一篇:AndroidComposeLazyColumnIllegalArgumentException:Keywasalreadyused