Jetpack Compose 中的应用程序/脚手架白色背景更改问题

Raw*_*san 7 android kotlin android-jetpack-compose

谁能告诉我如何更改应用程序的白色背景颜色?在 Surface 上设置颜色或背景颜色没有任何影响。我为支架内的内容设置了青色背景只是为了调试问题。

class MainActivity : ComponentActivity() {
        ...
        setContent {
            ChakkarTheme {
                Surface(
                    color = Color.Red, modifier = Modifier
                        .fillMaxSize()
                        .background(Color.DarkGray)
                ) {
                    ChakkarApp()
                }
            ...
Run Code Online (Sandbox Code Playgroud)
@Composable
fun ChakkarApp() {
    Scaffold(
        topBar = { TopAppBar(title = { Text(appTitle) }, navigationIcon = navigationIcon) },
        floatingActionButtonPosition = FabPosition.End,
        floatingActionButton = {
            if (showFab) {
                FloatingActionButton(onClick = { /*TODO*/ }) {
                    Icon(imageVector = Icons.Filled.Add, contentDescription = null)
                }
            }
        },
        bottomBar = {
            BottomNavigation() {
                val navBackStackEntry by navController.currentBackStackEntryAsState()
                val currentDestination = navBackStackEntry?.destination
                bottomNavItems.forEach { screen ->
                    BottomNavigationItem(
                        icon = { Icon(imageVector = screen.icon, contentDescription = null) },
                        label = { Text(stringResource(id = screen.resourceId)) },
                        selected = currentDestination?.hierarchy?.any { it.route == screen.route } == true,
                        onClick = {
                            navController.navigate(screen.route) {
                                popUpTo(navController.graph.findStartDestination().id) {
                                    saveState = true
                                }
                                launchSingleTop = true
                                restoreState = true
                            }
                        }
                    )
                }
            }
        }
    ) { paddingValues ->

        Column(
            modifier = Modifier
                .verticalScroll(rememberScrollState())
                .background(Color.Cyan)
                .padding(paddingValues)
                .padding(16.dp)
        ) {
            NavHost(
                navController = navController,
                startDestination = Screen.Running.route
            ) {
...
Run Code Online (Sandbox Code Playgroud)

感谢您的帮助!

Phi*_*hov 17

默认背景颜色ScaffoldMaterialTheme.colors.background

您可以指定自定义值:

Scaffold(
    ...
    backgroundColor = Color.DarkGray,
)
Run Code Online (Sandbox Code Playgroud)

对于 M3 来说containerColor

Scaffold(
    ...
    containerColor = Color.DarkGray,
)
Run Code Online (Sandbox Code Playgroud)

  • @Aakash我不会说“现在”,因为material2没有被弃用=)但是是的,在M3中它有一个不同的名称,将其添加到我的答案中,谢谢 (2认同)