在 Jetpack 中展开 Material3 的 BottomSheetScaffold 组合到可用的工作表内容而不是完全展开

问题描述 投票:0回答:1

我目前在 Android 应用程序之一中面临 BottomSheetScafold 的问题,我希望它扩展到sheetContent 高度而不是完全扩展。我知道我可以使用sheetPeekHeight来实现它,但是我必须对peek高度进行硬编码,相反我希望它扩展到sheetContent布局高度。 这是我的代码:

BottomSheetScaffold(
            modifier = Modifier.fillMaxHeight(),
            sheetContent = { GetSheetContent() },
            sheetDragHandle = {},
            sheetShape = RoundedCornerShape(
                topStart = cornerRadius,
                topEnd = cornerRadius
            ),
            sheetPeekHeight = bottomSheetPeekHeight,
            sheetSwipeEnabled = isSheetSwipeEnabled
        ) {
            // Do nothing
        }

以下是工作表内容,我添加了一些示例内容。

@Composable
fun GetSheetContent() {
Column(
        modifier = Modifier
    ) {
       Image(
            painter = painterResource(id = R.drawable.ic_location),
            contentDescription = null
        )
      Text(
        modifier = Modifier.semantics { heading() },
        text = stringResource(id = titleId)
      )
      Text(
        text = stringResource(id = description)
      )
}
}
}

众所周知,Android 有各种屏幕尺寸,如果我对 sheetPeekHeight 进行硬编码,工作表内容将在某些手机中完全可见,而在其他手机中可能不会。我确实在 stackoverflow 中研究了其他解决方案,但没有得到任何可以解决我的问题的东西。有什么建议吗?

使用sheetPeekHeight来实现它,但我必须对peek高度进行硬编码,相反我希望它扩展直到sheetContent布局高度。

android android-jetpack-compose
1个回答
0
投票

默认情况下,

sheetContent
BottomSheetScaffold
应仅与展开状态下的内容一样高。您可以从互联网上的各种演示中看到这一点。

如果您的情况没有发生这种情况,那么您的

GetContent
可组合项中可能存在某些内容导致 BottomSheet 占据全屏高度。例如,这可以是
fillMaxHeight
fillMaxSize
修饰符。

如果您发布的代码是您的实际代码,那么请尝试为图像可组合项指定固定高度,如下所示:

@Composable
fun GetSheetContent() {

    Column(
            modifier = Modifier.fillMaxWidth().wrapContentHeight()
    ) {
        Image(
            modifier = Modifier.size(100.dp),
            painter = painterResource(id = R.drawable.ic_location),
            contentDescription = null
        )
        Text(
            modifier = Modifier.semantics { heading() },
            text = stringResource(id = titleId)
        )
        Text(
            text = stringResource(id = description)
          )
    }

}

如果您的代码只是示例代码,请编辑您的问题并发布您的实际

GetContent
可组合项,那里可能会出现问题。

© www.soinside.com 2019 - 2024. All rights reserved.