沉浸式(侵入式)状态栏
效果图:
1、代码加入:WindowCompat.setDecorFitsSystemWindows(window, false)
ComposeTestTheme {
WindowCompat.setDecorFitsSystemWindows(window, false)
Greeting("Android")
}
2、沉浸式(侵入式)主题:
@Composable
fun ComposeTestTheme(
darkTheme: Boolean = isSystemInDarkTheme(),
// Dynamic color is available on Android 12+
dynamicColor: Boolean = true,
content: @Composable () -> Unit
) {
val colorScheme = when {
dynamicColor && Build.VERSION.SDK_INT >= Build.VERSION_CODES.S -> {
val context = LocalContext.current
if (darkTheme) dynamicDarkColorScheme(context) else dynamicLightColorScheme(context)
}
darkTheme -> DarkColorScheme
else -> LightColorScheme
}
val view = LocalView.current
if (!view.isInEditMode) {
SideEffect {
(view.context as Activity).window.statusBarColor = Color.Transparent.toArgb() //透明
ViewCompat.getWindowInsetsController(view)?.isAppearanceLightStatusBars = darkTheme
}
}
MaterialTheme(
colorScheme = colorScheme,
typography = Typography,
content = content
)
}
3、
@Composable
fun Greeting(name: String) {
Box(
modifier = Modifier
.size(200.dp)
.background(Color.Red),
contentAlignment = Alignment.Center
) {
Text(text = "Hello from Compose", color = Color.White, fontSize = 20.sp)
}
}