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

Jetpack Compose:如何将ModalBottomSheetLayout半展开状态设为屏幕70%高度

调整ModalBottomSheetLayout半展开状态高度到70%的实现方法

直接通过rememberModalBottomSheetState的halfExpandedRatio参数就能修改半展开状态的高度比例,默认值是0.5,改成0.7即可让弹窗占屏幕高度的70%。

修改后的代码如下:

val sheetState = rememberModalBottomSheetState(
    initialValue = if (isFullScreen) {
        ModalBottomSheetValue.Expanded
    } else {
        ModalBottomSheetValue.HalfExpanded
    },
    // 设置半展开状态的高度比例为0.7
    halfExpandedRatio = 0.7f
)

ModalBottomSheetLayout(
    sheetState = sheetState,
    sheetContent = {
        Spacer(modifier = Modifier.height(1.dp))
        content()
    },
    modifier = modifier,
    content = {}
)

如果需要基于屏幕尺寸做更精准的高度控制,也可以通过LocalConfiguration获取屏幕高度后计算比例传入:

val configuration = LocalConfiguration.current
val screenHeight = configuration.screenHeightDp.dp
// 计算目标高度对应的比例
val targetRatio = 0.7f

val sheetState = rememberModalBottomSheetState(
    initialValue = if (isFullScreen) {
        ModalBottomSheetValue.Expanded
    } else {
        ModalBottomSheetValue.HalfExpanded
    },
    halfExpandedRatio = targetRatio
)

内容的提问来源于stack exchange,提问作者Rainmaker

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.27 08:42:08