实现 Layout Spec 非常简单:只需编写一个带有 @OnCreateLayout 注解的方法,该方法返回包含 ComponentLayout 对象的不可变树。
@LayoutSpec
public class MyComponentSpec {
@OnCreateLayout
static Component onCreateLayout(
ComponentContext c,
@Prop int color,
@Prop String title) {
return Row.create(c)
.alignItems(CENTER)
.child(
SolidColor.create(c)
.colorRes(color)
.widthDip(40)
.heightDip(40))
.child(
Text.create(c)
.text(title)
.textSizeRes(R.dimen.my_text_size)
.flexGrow(1f))
.build();
}
}
SolidColor.create(c)
.colorRes(color)
.width(40)
.height(40)
Text.create(c)
.text(title)
.textSizeRes(R.dimen.my_text_size)
.grow(1f)