Kél*_*ian 4 android android-button android-jetpack-compose
假设我有一个像这样的可组合:
@Composable
fun LoadingButton() {
val (isLoading, setIsLoading) = state { false }
Button(
onClick = setIsLoading,
text = {
if (isLoading) {
Text(text = "Short text")
} else {
Text(text = "Very very very long text")
}
}
)
}
Run Code Online (Sandbox Code Playgroud)
如何为按钮的宽度更新设置动画?
我很清楚我可以向按钮添加一个 preferredWidth 修饰符,并使用以下方法为该宽度设置动画:
val buttonWidth = animate(target = if (isLoading) LoadingButtonMinWidth else LoadingButtonMaxWidth)
Run Code Online (Sandbox Code Playgroud)
但这不是我想要的。我需要为自动“包装内容”宽度设置动画。
提前致谢。
Gab*_*tti 12
您可以应用修改器animateContentSize
当其子修改器(或子可组合项,如果它已位于链尾部)更改大小时,此修改器会为其自己的大小设置动画。这允许父修改器观察平滑的尺寸变化,从而产生整体连续的视觉变化。
就像是:
var isLoading by remember { mutableStateOf(false) }
val text = if (isLoading) "Short text" else "Very very very long text"
Button(onClick = { isLoading = !isLoading },
modifier = Modifier
.animateContentSize(
animationSpec = tween(durationMillis = 300,
easing = LinearOutSlowInEasing))
) {
Text(
text = text,textAlign = TextAlign.Center
)
}
Run Code Online (Sandbox Code Playgroud)
您需要为Composable添加一个animateContentSize修饰符Text:
@Composable
fun LoadingButton() {
val (isLoading, setIsLoading) = state { false }
Button(onClick = { setIsLoading(!isLoading) }) {
Text(
text = if (isLoading) {
"Short text"
} else {
"Very very very long text"
},
modifier = Modifier.animateContentSize()
)
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1572 次 |
| 最近记录: |