一个SubcomposeLayout 小例子

236 阅读1分钟
@Composable
fun SubcomposeBottomFirstLayout(modifier: Modifier, bottom: @Composable () -> Unit, other: @Composable () -> Unit) {
    val TAG = "BottomFirstLayout"
    SubcomposeLayout(modifier) { constraints: Constraints ->
        var bottomHeight = 0
        val bottomPlaceables = subcompose("bottom", bottom).map {
            val placeable = it.measure(constraints.copy(minWidth = 0, minHeight = 0))
            bottomHeight = placeable.height
            placeable
        }
        val h = constraints.maxHeight - bottomHeight //        Log.d(TAG, "SubcomposeBottomFirstLayout: max:${constraints.maxHeight} bh:$bottomHeight")
        val otherPlaceables = subcompose("other", other).map {
            it.measure(constraints.copy(minHeight = 0, maxHeight = h))
        }

        layout(constraints.maxWidth, constraints.maxHeight) {
            bottomPlaceables[0].placeRelative(0, h)
            otherPlaceables[0].placeRelative(0, h - otherPlaceables[0].height)
        }
    }
}

加油.png