在Jetpack Compose中替代使用View.INVISIBLE的方式是:

huangapple go评论58阅读模式
英文:

Alternate use of View.INVISIBLE in jetpack compose

问题

在 Jetpack Compose 中,使用 AnimatedVisibility 来达到 XML 中 View.INVISIBLE 的效果。这个方法可以让视图在不可见时不占据布局空间。示例代码如下:

AnimatedVisibility(
    // true or false
) {
    Button() // 按钮的代码
}
英文:

In xml we use View.INVISIBLE to just don't show the view at all, but it still takes up space for layout purposes. What is the alternative in jetpack compose?

AnimatedVisibility(
    // true or false
) {
    Button() // button code.
}

答案1

得分: 6

你可以构建一个自定义修饰符来测量组合内容所占据的空间。

类似这样的代码:

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) {}
    }
}
英文:

You can build a custom modifier to measure the Composable to take up space.

Something like:

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) {}
    }
}

答案2

得分: 1

以下是代码的翻译部分:

// 接受的答案非常完美,但您还可以查看以下代码。

@Composable
fun ShowContentOrNot() {

    var isShow by remember { mutableStateOf(true) }

    Column(
        modifier = Modifier.fillMaxSize(),
        horizontalAlignment = Alignment.CenterHorizontally,
        verticalArrangement = Arrangement.Center
    ) {
        if (isShow)
            Text(text = "Some content")

        Spacer(modifier = Modifier.height(20.dp))

        Button(onClick = {
            isShow = !isShow
        }) {
            Text(text = "Hide/Show")
        }

    }

}

请注意,由于原始代码中包含HTML实体(例如"),我已将它们还原为普通文本。

英文:

The Accepted answer is perfect , but you can also look into the below code.

@Composable
fun ShowContentOrNot() {

    var isShow by remember { mutableStateOf(true) }

    Column(
        modifier = Modifier.fillMaxSize(),
        horizontalAlignment = Alignment.CenterHorizontally,
        verticalArrangement = Arrangement.Center
    ) {
        if (isShow)
            Text(text = "Some content")

        Spacer(modifier = Modifier.height(20.dp))

        Button(onClick = {
            isShow = !isShow
        }) {
            Text(text = "Hide/Show")
        }

    }

}

答案3

得分: 0

我只使用alpha修饰符扩展函数来实现这个功能,它可以涵盖90%的使用情况,除非你正在将alpha通道用于其他用途。例如,我可以这样做,对于文本:

Text( modifier = Modifier.alpha(0f), text = "Lorem Ipsum")
英文:

I just use the alpha Modifier extension function to achieve this functionality which may cover 90% of the use cases, unless you're using the alpha channel for other purposes. I do it like this, for example, on a text:

Text( modifier = Modifier.alpha(0f), text = "Lorem Ipsum")

huangapple
  • 本文由 发表于 2023年2月16日 19:25:47
  • 转载请务必保留本文链接:https://go.coder-hub.com/75471583.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定