Jetpack Compose BottomSheet在平板上无法铺满全屏问题求助
如何让Material3 BottomSheet铺满屏幕边缘?
要让BottomSheet完全铺满屏幕边缘,只需修改BottomSheetScaffold的关键参数并调整内容布局:
- 移除BottomSheet默认圆角:设置
sheetShape为RectangleShape - 让内容填充整个BottomSheet容器:给sheetContent内的组件添加
Modifier.fillMaxSize()
修改后的完整代码如下:
@OptIn(ExperimentalMaterial3Api::class) class MainActivity : ComponentActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContent { M3BottomSheetTheme { Surface( modifier = Modifier.fillMaxSize(), color = MaterialTheme.colorScheme.background ) { val scaffoldState = rememberBottomSheetScaffoldState() val scope = rememberCoroutineScope() BottomSheetScaffold( scaffoldState = scaffoldState, sheetContent = { Image( painter = painterResource(id = R.drawable.kermit), contentDescription = null, modifier = Modifier.fillMaxSize(), contentScale = ContentScale.Crop // 可选,避免图片拉伸变形 ) }, sheetPeekHeight = 0.dp, sheetShape = RectangleShape, // 去除默认圆角,让边缘贴合屏幕 sheetContainerColor = MaterialTheme.colorScheme.background // 可选,统一背景色避免突兀 ) { Box( modifier = Modifier.fillMaxSize(), contentAlignment = Alignment.Center ) { Button(onClick = { scope.launch { scaffoldState.bottomSheetState.expand() } }) { Text(text = "Open sheet") } } } } } } } }
说明
默认的BottomSheetScaffold自带圆角形状和容器内边距,通过设置RectangleShape可以消除圆角,让BottomSheet边缘与屏幕完全贴合;给内容组件添加fillMaxSize则能让内容填满整个BottomSheet区域。
内容的提问来源于stack exchange,提问作者truthsayer
相关产品推荐
相关产品推荐

