状态栏图标未显示为深色
我最近对边到边的实现做了一点尝试,据官方Android文档显示,可以通过使用...来强制将状态栏图标显示为深色
WindowCompat.getInsetsController(window, window.decorView)
.isAppearanceLightStatusBars = true
于是我在应用里尝试了一下,但状态栏图标仍然显示为亮色,这使得图标在我的应用中有点看不清,因为我使用的是带Scaffold的表面颜色。我确实希望应用颜色能延伸到状态栏后面,但图标需要是黑色,或者至少是深色。
一个很简单的例子来说明我的意思是...
class MainActivity: FragmentActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
enableEdgeToEdge()
WindowCompat.getInsetsController(window, window.decorView)
.isAppearanceLightStatusBars = true
setContent {
StatusBarTestTheme {
Surface(color = MaterialTheme.colorScheme.surface) {
Scaffold(...) { padding ->
HomeView(padding)
}
}
}
}
}
@Composable
fun HomeView(padding: PaddingValues) {
Box(modifier = Modifier.padding(padding) {
Column(
modifier = Modifier.fillMaxSize(),
horizontalAlignment = Alignment.CenterHorizontally,
verticalArrangement = Arrangement.Center
) {
Text("Hello There")
}
}
}
When you do that, if the device theme is in light mode the status bar icons are white, it doesn't seem to take the `isAppearanceLightStatusBars = true` into effect at all. If I switch the device to dark mode, then the icons are black, but I need them to always be black.
我参考的文档在官方Android开发者页面,这里是...
https://developer.android.com/develop/ui/compose/system/setup-e2e
解决方案
最有效的解决方案是在你的
StatusBarTestTheme
中设置
isAppearanceLightStatusBars = !darkTheme
isAppearanceLightNavigationBars = !darkTheme
这是我在 我的应用模板 中解决的方法:
@Composable
fun AppTheme(
darkTheme: Boolean = true,
content: @Composable () -> Unit
) {
val colorScheme = when {
darkTheme -> DarkColorScheme
else -> LightColorScheme
}
val view = LocalView.current
if (!view.isInEditMode) {
SideEffect {
val window = (view.context as Activity).window
val windowInsetsController =
WindowCompat.getInsetsController(window, view)
windowInsetsController.apply {
isAppearanceLightStatusBars = !darkTheme
isAppearanceLightNavigationBars = !darkTheme
}
}
}
MaterialTheme(
colorScheme = colorScheme,
typography = Typography
) {
Box(
modifier = Modifier
.fillMaxSize()
.background(color = MaterialTheme.colorScheme.surfaceContainer)
) {
content()
}
}
}
并在themes.xml中将状态栏设为透明
<resources>
<style name="Theme.AppTemplateCompose" parent="android:Theme.Material.Light.NoActionBar">
<item name="android:fitsSystemWindows">false</item>
<item name="android:statusBarColor">@android:color/transparent</item>
<item name="android:navigationBarColor">@android:color/transparent</item>
<!-- This allows to set the status bar fully transparent. It was removed in androidx.core:core-splashscreen 1.2.0 -->
<item name="android:enforceNavigationBarContrast">false</item>
<item name="android:enforceStatusBarContrast">false</item>
</style>
<style name="Theme.AppTemplateCompose.Splash" parent="Theme.SplashScreen">
<item name="windowSplashScreenBackground">@color/launcher_background</item>
<item name="windowSplashScreenAnimatedIcon">@drawable/ic_splash</item>
<item name="postSplashScreenTheme">@style/Theme.AppTemplateCompose</item>
</style>
</resources>
站内所有文章版权归属LeftHeroAI导航站,无授权禁止任何主体转载、抄袭、复制内容,亦不得私自架设镜像站点。一经侵权,本站将通过法律途径追责。