then 叠加修饰符功能的作用
val reusableModifier = Modifier
.fillMaxWidth()
.background(Color.Red)
.padding(12.dp)
// Append to your reusableModifier
reusableModifier.clickable { /*...*/ }
// Append your reusableModifier
otherModifier.then(reusableModifier)
https://developer.aliyun.com/article/1409449
Compose 类型稳定性注解:@Stable & @Immutable
- 主动测量
SubcomposeLayout
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import androidx.compose.ui.layout.SubcomposeLayout
@Composable
fun DynamicContent() {
SubcomposeLayout { constraints ->
val placeable = subcompose("MyDynamicComponent") {
// 动态创建或更新子组件
Box(modifier = Modifier.fillMaxSize()) {
// 子组件内容
}
}.first().measure(constraints)
layout(placeable.width, placeable.height) {
placeable.placeRelative(0, 0)
}
}
}
以上先测量宽高再布局