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
相关产品推荐
相关产品推荐

