在 jetpack compose 中替代使用 View.INVISIBLE

Viv*_*odi 4 android kotlin android-jetpack-compose

在 xml 中,我们过去View.INVISIBLE根本不显示视图,但出于布局目的,它仍然占用空间。jetpack compose 中的替代方案是什么?

AnimatedVisibility(
    // true or false
) {
    Button() // button code.
}
Run Code Online (Sandbox Code Playgroud)

Gab*_*tti 7

您可以构建自定义修改器来测量可组合项占用的空间。

就像是:

fun Modifier.visible(visible: Boolean) = if (visible) this else this.then(Invisible)

private object Invisible : LayoutModifier {

    override fun MeasureScope.measure(
        measurable: Measurable,
        constraints: Constraints
    ): MeasureResult {
        val placeable = measurable.measure(constraints)
        return layout(placeable.width, placeable.height) {}
    }
}
Run Code Online (Sandbox Code Playgroud)