升级compileSDK至35后Modal Bottom Sheet为何显示在屏幕顶部?
Compile SDK 35下ModalBottomSheet异常显示在顶部的修复方案
问题原因
Android 15(Compile SDK 35)中,Jetpack Compose的ModalBottomSheet组件对窗口内边距(WindowInsets)的处理逻辑进行了调整。你手动设置的contentWindowInsets = { WindowInsets.systemBars }会干扰组件默认的底部对齐计算,导致Sheet被错误定位到屏幕顶部。
修复方案
方案1:移除自定义Insets配置(推荐)
默认情况下,ModalBottomSheet已经会自动处理系统栏的内边距,无需手动指定WindowInsets.systemBars。移除该配置后,组件会恢复正常的底部对齐:
val sheetState = rememberModalBottomSheetState(skipPartiallyExpanded = true) ModalBottomSheet( sheetState = sheetState, modifier = Modifier.heightIn(min = 480.dp, max = 480.dp) ) { // sheet content here }
方案2:针对性调整Insets(若需保留部分自定义)
如果确实需要自定义Insets(比如忽略顶部状态栏),可以通过排除顶部Insets来保留底部对齐逻辑:
val sheetState = rememberModalBottomSheetState(skipPartiallyExpanded = true) ModalBottomSheet( sheetState = sheetState, modifier = Modifier.heightIn(min = 480.dp, max = 480.dp), contentWindowInsets = { WindowInsets.systemBars.exclude(WindowInsets.top) } ) { // sheet content here }
补充说明
Jetpack Compose适配Android 15时优化了组件的系统UI适配逻辑,手动设置全量systemBars Insets会覆盖组件默认的底部定位规则,从而引发显示异常。使用默认配置或精准调整Insets,即可恢复ModalBottomSheet的正常底部显示。
内容的提问来源于stack exchange,提问作者stackunderflow
相关产品推荐
相关产品推荐

