[英]Compose TopAppBar has no background color
I want to add a TopAppBar
to my Compose app, so I did the following:我想将
TopAppBar
添加到我的 Compose 应用程序中,因此我执行了以下操作:
@OptIn(ExperimentalMaterial3Api::class)
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContent {
AlternoTubeTheme {
// A surface container using the 'background' color from the theme
Surface(
modifier = Modifier.fillMaxSize(),
color = MaterialTheme.colorScheme.background
) {
Scaffold(
topBar = {
TopAppBar(
title = {
Text(
stringResource(id = R.string.app_name),
maxLines = 1,
overflow = TextOverflow.Ellipsis
)
},
)
},
content = { innerPadding ->
MyAppTheme(modifier = Modifier.padding(innerPadding))
}
)
}
}
}
}
The issue is, when I run the app, there is no color to my TopAppBar
:问题是,当我运行该应用程序时,我的
TopAppBar
没有颜色:
Whereas on the preview images, the app bar does have colors:而在预览图像上,应用栏确实有 colors:
What can I try next to get the right colors?接下来我可以尝试什么来获得正确的 colors?
With M3 the default value of the background color in the TopAppBar
is defined in the TopAppBarDefaults.smallTopAppBarColors()
with the containerColor
attribute.对于M3 ,
TopAppBar
中背景颜色的默认值是在TopAppBarDefaults.smallTopAppBarColors()
中使用containerColor
属性定义的。 The default value is the surface
color defined in your theme.默认值是主题中定义的
surface
颜色。
Check your theme or you can override it using something like:检查您的主题,或者您可以使用以下内容覆盖它:
TopAppBar(
title = {
Text(
stringResource(id = R.string.app_name),
maxLines = 1,
overflow = TextOverflow.Ellipsis
)
},
colors = TopAppBarDefaults.smallTopAppBarColors(containerColor = Yellow)
)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.