子组合布局何时支持内部布局(如果有的话)?

When will Subcompose Layout support Intrinsic layout, if ever?

提问人:JJ123 提问时间:3/20/2021 更新时间:11/18/2023 访问量:4052

问:

我在 in 的源代码中看到了这个函数。androidx.compose.ui.layout.SubcomposeLayout.ktandroidx.compose.ui:ui:1.0.0-beta02

    private fun createMeasurePolicy(
        block: SubcomposeMeasureScope.(Constraints) -> MeasureResult
    ): MeasurePolicy = object : LayoutNode.NoIntrinsicsMeasurePolicy(
        error = "Intrinsic measurements are not currently supported by SubcomposeLayout"
    ) {
        ...
    }

当可组合项将在子可组合项中呈现时,我似乎不能使用内在测量。

作为参考,我正在尝试在 ModalBottomSheet 中使用这样的视图。目的是在工作表中有一个可滚动的视图,粘性视图始终位于底部(如按钮)。我希望可滚动内容只占用所需的空间,并且在工作表展开状态下并不总是全屏显示,确实如此。weight(1f)

Column(
        modifier = Modifier
                .height(IntrinsicSize.Min)
                .wrapContentHeight(Alignment.Bottom),
        verticalArrangement = Arrangement.Bottom
) {
  Column(
          content = sheetContent,
          modifier = Modifier
                  .weight(1f)
                  .wrapContentHeight(Alignment.Bottom)
  )
  Box {
    bottomStickyContent?.let { it() }
  }
}
安卓 android-jetpack-compose

评论


答:

3赞 JJ123 3/20/2021 #1

听起来答案是否定的,不会很快获得内在支持,如果有的话。SubcomposeLayout

我通过代码更新以使用约束布局解决了这个问题。

ConstraintLayout {
  val (sticky, column) = createRefs()
  Column(
          content = sheetContent,
          modifier = Modifier
                  .constrainAs(column) {
                    top.linkTo(parent.top)
                    bottom.linkTo(sticky.top)
                    height = Dimension.preferredWrapContent
                  }
                  .wrapContentHeight(Alignment.Bottom)
  )
  Box(
          modifier = Modifier
                  .constrainAs(sticky) {
                    bottom.linkTo(parent.bottom)
                  }
  ) {
    bottomStickyContent?.let { it() }
  }
}
0赞 AndroidEngineX 11/18/2023 #2

对我来说,在布局中提供支持是没有意义的。IntrinsicSubcompose

如果您认为,在正常的容器(非)布局中,该预测量(与实际测量相比,预测量很便宜,并遵循一些启发性)所有项目并找到子项可以采用的最大或最小尺寸,然后容器将该尺寸强制执行给每个子项SubcomposeIntrisic

在布局的情况下,某些组件的测量是预先完成的,但不是全部(以防止测量潜在的非常大的集合)。它无法知道所有孩子的最大或最小尺寸应该是多少。Subcompose

即使是最新的组合也会出现以下错误

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
- adding a size modifier to the component, in order to fast return the queried intrinsic measurement

我从 MAD compose 系列视频中推断出这一点