【问题标题】:How to show semi-transparent loading overlay above full Composable如何在完整的 Composable 上方显示半透明加载叠加层
【发布时间】:2023-01-27 23:54:21
【问题描述】:

我正在尝试创建一个包装另一个 content 可组合项的可组合项,并在其上显示一个 CircularProgressBar 作为叠加层,覆盖整个 content 可组合项。

我几乎让它按预期工作,请参阅以下图片:

初始状态

加载状态

但正如您所见,覆盖图填满了整个屏幕,而不仅仅是灰色的LazyRow 项目和文本字段。因此,按钮被推离屏幕。

这是我当前的代码:

@Composable
fun LoadingBox(
    modifier: Modifier = Modifier,
    isLoading: Boolean = false,
    loadingText: String,
    content: @Composable() () -> Unit
) {
    Box(modifier = modifier
        .fillMaxWidth()
        .wrapContentHeight()
    ) {

        content()
        if (isLoading) {

            Surface(
                modifier = Modifier
                    .fillMaxSize()
                    .alpha(0.5f),
            ) {
                Column(
                    modifier = Modifier.fillMaxSize(),
                    verticalArrangement = Arrangement.Center,
                    horizontalAlignment = Alignment.CenterHorizontally
                ) {
                    CircularProgressIndicator()
                    Text(text = loadingText)
                }
            }
        }
    }
}

在屏幕截图中,我提供了灰色框和文本字段作为content 参数。所以叠加层应该只覆盖灰色的LazyRow 项目和文本字段。

我已经偶然发现了instrinsic measures,但是当我将 LazyRow 提供为content 时,由于以下错误,我无法使用它们,因为应用程序崩溃了:

java.lang.IllegalStateException: Asking for intrinsic measurements of SubcomposeLayout layouts is not supported. This includes components that are built on top of SubcomposeLayout, such as lazy lists, BoxWithConstraints, TabRow, etc. To mitigate this:
- if intrinsic measurements are used to achieve 'match parent' sizing,, consider replacing the parent of the component with a custom layout which controls the order in which children are measured, making intrinsic measurement not needed

【问题讨论】:

  • 您在 Surface 中使用 fillMaxSize()

标签: android-jetpack-compose


【解决方案1】:

去掉Column中的SurfaceverticalArrangement,在Box中添加contentAlignment = Alignment.Center

就像是:

Box(modifier = modifier
    .fillMaxWidth(),
    contentAlignment = Alignment.Center
) {

    content()
    if (isLoading) {


        Column(
            modifier = Modifier.fillMaxWidth().alpha(0.5f),
            horizontalAlignment = Alignment.CenterHorizontally
        ) {
            androidx.compose.material.CircularProgressIndicator()
            Text(text = loadingText)
        }

    }
}

【讨论】:

    猜你喜欢
    • 2019-07-30
    • 2014-08-07
    • 2012-06-12
    • 1970-01-01
    • 2020-10-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多