首页 > 解决方案 > 如何在 Jetpack Compose 中使用 Modifier.contentSize?

问题描述

https://developer.android.com/jetpack/compose/animation

If you are animating changes to content size:
Use Modifier.contentSize.

但是,我找不到如何访问Modifier.contentSize和使用它。有指南吗?

标签: androidandroid-jetpack-compose

解决方案


我认为您在谈论animateContentSize函数。

这里我有一个可能有帮助的例子

Column(
        modifier = Modifier
            .fillMaxWidth()
            .padding(16.dp)
            // This `Column` animates its size when its content changes.
            .animateContentSize()
    ) {
        Row {
            Icon(
                imageVector = Icons.Default.Info,
                contentDescription = null
            )
            // ...
        }
        if (expanded) { // If the expanded value is changed then the animateContentSize will be triggered
            Spacer(modifier = Modifier.height(8.dp))
            Text(
                text = stringResource(R.string.lorem_ipsum),
                textAlign = TextAlign.Justify
            )
        }
    }

在此处输入图像描述

来自 Jetpack Compose Animation 代码实验室


推荐阅读