You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Jetpack Compose BottomSheet在平板上无法铺满全屏问题求助

如何让Material3 BottomSheet铺满屏幕边缘?

要让BottomSheet完全铺满屏幕边缘,只需修改BottomSheetScaffold的关键参数并调整内容布局:

  1. 移除BottomSheet默认圆角:设置sheetShape为RectangleShape
  2. 让内容填充整个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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.12 22:42:45