admin管理员组文章数量:1122832
I have the following composable theme:
@Composable
fun AppTheme(
themeOption: ThemeOption = ThemeOption.SYSTEM,
// Dynamic color is available on Android 12+
dynamicColor: Boolean = true,
content: @Composable () -> Unit,
) {
val isDarkTheme = isAppInDarkTheme(themeOption)
val colorScheme = when {
dynamicColor && Build.VERSION.SDK_INT >= Build.VERSION_CODES.S -> {
val context = LocalContext.current
if (isDarkTheme) dynamicDarkColorScheme(context) else dynamicLightColorScheme(context)
}
isDarkTheme -> darkScheme
else -> lightScheme
}
val view = LocalView.current
if (!view.isInEditMode) {
SideEffect {
(view.context as? Activity)?.window?.apply {
statusBarColor = colorScheme.background.toArgb()
navigationBarColor = colorScheme.surfaceContainer.toArgb()
}?.also {
WindowCompat.getInsetsController(it, view).apply {
isAppearanceLightStatusBars = !isDarkTheme
isAppearanceLightNavigationBars = !isDarkTheme
}
}
}
}
MaterialTheme(
colorScheme = colorScheme,
content = content
)
}
The piece of code that has the deprecation is:
WindowCompat.getInsetsController(it, view).apply {
isAppearanceLightStatusBars = !isDarkTheme
isAppearanceLightNavigationBars = !isDarkTheme
}
The deprecation messages are:
Deprecated
Draw proper background behind WindowInsets.Type.navigationBars() or WindowInsets. Type. tappableElement() instead.
Deprecated
Draw proper background behind WindowInsets.Type.statusBars()} instead.
I saw that is related to API 35. How can I solve it?
I have the following composable theme:
@Composable
fun AppTheme(
themeOption: ThemeOption = ThemeOption.SYSTEM,
// Dynamic color is available on Android 12+
dynamicColor: Boolean = true,
content: @Composable () -> Unit,
) {
val isDarkTheme = isAppInDarkTheme(themeOption)
val colorScheme = when {
dynamicColor && Build.VERSION.SDK_INT >= Build.VERSION_CODES.S -> {
val context = LocalContext.current
if (isDarkTheme) dynamicDarkColorScheme(context) else dynamicLightColorScheme(context)
}
isDarkTheme -> darkScheme
else -> lightScheme
}
val view = LocalView.current
if (!view.isInEditMode) {
SideEffect {
(view.context as? Activity)?.window?.apply {
statusBarColor = colorScheme.background.toArgb()
navigationBarColor = colorScheme.surfaceContainer.toArgb()
}?.also {
WindowCompat.getInsetsController(it, view).apply {
isAppearanceLightStatusBars = !isDarkTheme
isAppearanceLightNavigationBars = !isDarkTheme
}
}
}
}
MaterialTheme(
colorScheme = colorScheme,
content = content
)
}
The piece of code that has the deprecation is:
WindowCompat.getInsetsController(it, view).apply {
isAppearanceLightStatusBars = !isDarkTheme
isAppearanceLightNavigationBars = !isDarkTheme
}
The deprecation messages are:
Deprecated
Draw proper background behind WindowInsets.Type.navigationBars() or WindowInsets. Type. tappableElement() instead.
Deprecated
Draw proper background behind WindowInsets.Type.statusBars()} instead.
I saw that is related to API 35. How can I solve it?
Share Improve this question asked Nov 22, 2024 at 2:20 Pierre VieiraPierre Vieira 3,2508 gold badges28 silver badges52 bronze badges 3- Have you tried using the suggestions? – tomerpacific Commented Nov 22, 2024 at 8:49
- This question is similar to: Android API 35+ Status/Navigation bar color/height from Java. If you believe it’s different, please edit the question, make it clear how it’s different and/or how the answers on that question are not helpful for your problem. – tomerpacific Commented Nov 22, 2024 at 8:49
- @tomerparcifc it's not the same, I'm using compose and in this case if I use this passing my main activity as parameter I'll have problems with the app bottom bar height. – Pierre Vieira Commented Nov 22, 2024 at 19:34
1 Answer
Reset to default 1I solved this problem with the following:
- Remove the code:
val view = LocalView.current
if (!view.isInEditMode) {
SideEffect {
(view.context as? Activity)?.window?.apply {
statusBarColor = colorScheme.background.toArgb()
navigationBarColor = colorScheme.surfaceContainer.toArgb()
}?.also {
WindowCompat.getInsetsController(it, view).apply {
isAppearanceLightStatusBars = !isDarkTheme
isAppearanceLightNavigationBars = !isDarkTheme
}
}
}
}
- In your build gradle add the following dependency:
dependencies {
val activity_version = 1.9.3
// Java language implementation
implementation("androidx.activity:activity:$activity_version")
// Kotlin
implementation("androidx.activity:activity-ktx:$activity_version")
}
- In your onCreate of your single activity use the following:
override fun onCreate(savedInstanceState: Bundle?) {
// ...
enableEdgeToEdge(statusBarStyle = getStatusBarStyle())
}
@Composable
private fun getStatusBarStyle(): SystemBarStyle = SystemBarStyle.run {
val color = Color.Transparent.toArgb()
if (isAppInDarkTheme()) {
dark(color)
} else {
light(color, color)
}
}
版权声明:本文标题:How to solve deprecation sets for statusBarColor and navigationBarColor in Android api 35+? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736306051a1932812.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论