繁体   English   中英

Compose TopAppBar 没有背景色

[英]Compose TopAppBar has no background color

我想将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))
                        }
                    )
                }
            }
        }
    }

问题是,当我运行该应用程序时,我的TopAppBar没有颜色:

在此处输入图像描述

而在预览图像上,应用栏确实有 colors:

在此处输入图像描述

接下来我可以尝试什么来获得正确的 colors?

对于M3TopAppBar中背景颜色的默认值是在TopAppBarDefaults.smallTopAppBarColors()中使用containerColor属性定义的。 默认值是主题中定义的surface颜色。

检查您的主题,或者您可以使用以下内容覆盖它:

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM