Android compose源码浅析——Modifier
Modifier浅析
-
- Modifier的使用
- foldOut
- foldIn
- any
- all
- 总结
Modifier的使用
先来一段代码1:
@Preview(showBackground = true)
@Composable
fun GreetingPreview() {
ComposeTestTheme {
Box(modifier = Modifier
.size(DpSize(Dp(100f),Dp(100f)))
.padding(Dp(10f))
.background(Color(0x7c1daf11))
.alpha(1.0f)
)
}
}
在代码中,我们使用建造者模式调用Modifier的方法,这些方法都是Modifier伴生类的静态扩展方法,伴生对象的方法如下:
// The companion object implements `Modifier` so that it may be used as the start of a
// modifier extension factory expression.
companion object : Modifier {
override fun <R> foldIn(initial: R, operation: (R, Element) -> R): R = initial
override fun <R> foldOut(initial: R, operation: (Element, R) -> R): R = initial
override infix fun then(other: Modifier): Modifier = other
//...省略
}