如何移除Jetpack Compose中Bottom Sheet的底部内边距
解决Material 2 BottomSheetScaffold底部莫名出现内边距的问题
Material 2的BottomSheetScaffold默认会给sheetContent添加底部内边距,用来适配系统导航栏(虚拟按键、底部导航栏等),这是组件的默认行为,并非你代码里手动设置的。
你可以通过以下两种方式解决:
1. 直接移除所有底部内边距
在BottomSheetScaffold中添加sheetPadding = PaddingValues(0.dp)参数,覆盖默认的内边距设置:
BottomSheetScaffold( scaffoldState = scaffoldState, modifier = Modifier.fillMaxSize(), sheetBackgroundColor = UiColors.MeditationBottomSheetColor, sheetContentColor = Color.Black, sheetGesturesEnabled = currentBottomSheet == 1, sheetPeekHeight = 0.dp, sheetShape = ShapeDefaults.Medium, sheetPadding = PaddingValues(0.dp), // 新增这一行移除默认内边距 sheetContent = { if (currentBottomSheet == 0) { GuidedMeditationStatisticsBottomSheetContent( user = currentUser, totalDurationInMs = currentPosition, onDone = onDone ) } else { InstrumentBottomSheet( currentInstrument = currentInstrument, instruments = instruments, currentVolume = currentInstrumentVolume, onVolumeChange = onVolumeChange, onSelectedInstrument = onSelectedInstrument, onCancel = onCancelInstrument ) } }, content = { //Content })
2. 检查子组件是否自带内边距
如果添加sheetPadding后问题仍存在,去检查GuidedMeditationStatisticsBottomSheetContent和InstrumentBottomSheet的内部代码,看是否在根布局中设置了底部padding——很多时候额外内边距是子组件自身添加的。
内容的提问来源于stack exchange,提问作者Harsh Vardhan Tewari
相关产品推荐
相关产品推荐

