Jetpack Compose TopBar 和 BottomBar 默认高程内容未填充其容器

Jetpack Compose TopBar and BottomBar Default Elevation content doesn't fill its container

如何修复顶部栏和底部栏未填满其容器的问题。 topbar和bottombar分别使用默认的Elevation

您可以看到顶部栏没有填充最大宽度并且有阴影,而底部栏有自己的文本

Scaffold(
        topBar = {
            TopAppBar(
                title = { Text( text = "TEST" ) },
                actions = {
                    IconButton(
                        onClick = { },
                    ) {
                        Icon(
                            imageVector = Icons.Filled.AccountCircle,
                            contentDescription = null
                        )
                    }
                },
            )
        },
        bottomBar = {
           BottomNavigation
            {
                val navBackStackEntry by bottomAppBarNavController.currentBackStackEntryAsState()
                val currentDestination = navBackStackEntry?.destination

                bottomBarItems.forEach { mainRoute ->
                    BottomNavigationItem(
                        selected = currentDestination?.hierarchy?.any { it.route == mainRoute.route } == true,
                        icon = {
                            Icon(
                                imageVector = mainRoute.icon,
                                contentDescription = stringResource(id = mainRoute.resourceId),
                            )
                        },
                        label = { Text( text = stringResource(id = mainRoute.resourceId), ) },
                        onClick = { },
                        alwaysShowLabel = false // This hides the title for the unselected items
                    )

                }
            }
        }
    ){

    }

发生这种情况是因为 TopAppBarBottomNavigation 默认具有高度,并且因为您使用 半透明颜色 作为 primary 主题颜色。

您可以:

  • 移除高度:TopAppBar(elevation = 0.dp)
  • 使用纯色背景
  • 尝试将半透明颜色转换为非透明颜色,例如: TopAppBar(backgroundColor = Color(0xD9FFFFFF).compositeOver(Color.White))