Litho
  • Introduction
  • 简介
    • Litho 是什么
    • 动机
    • 使用
  • 快速开始
    • 新手入门
    • 教程
    • 编写 Component
    • 使用 Component
  • 参考
    • Layout Specs
    • Mount Specs
    • Props
    • State
    • Layout
    • Error Boundaries
    • Borders
    • RecyclerCollectionComponent
    • 变换动画
    • 术语
  • 事件处理
    • 总览
    • 触摸处理
    • 可见性处理
  • Sections
    • Sections 是什么
    • Sections 教程
    • GroupSection Specs
    • DiffSection Specs
    • Sections Building Blocks
    • 与UI交互
    • Sections 单元测试
    • Sections 与 Views
    • Services
    • Sections 架构
    • Working Ranges
  • 兼容性
    • Styles
    • Accessbility
    • RTL
  • 测试
    • 总览
    • 单元测试基础
    • 子组件测试
    • 匹配 Props
    • 测试 InjectProps
    • 测试事件处理
    • Espresso
    • 在Android Studio上运行测试
  • 高级指南
    • Recycler
    • 自定义布局
    • TreeProps
    • 增量安装
    • 创建 ComponentTree
  • 架构
    • 代码生成
    • 异步布局
    • 增量安装
    • 铺平 View 结构
    • 回收
  • 附加资源
    • 最佳实践
    • FAQ
  • 工具
    • 调试
    • 开发者选项
  • 更多参与
    • 如何参与
    • 社区展示
    • 资源库结构
Powered by GitBook
On this page
  1. 参考

Layout Specs

Layout Spec 与 Android 上的复合View 逻辑等价。它只需现有组件组合到一个不可变的布局树中。

实现 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();
  }
}

如您所见,Layout Spec 类使用 @LayoutSpec 注释。

在上面的示例中,布局树具有一个根容器,带有两个孩子水平叠放( Row.create )并垂直居中( Align.CENTER )。

SolidColor.create(c)
    .colorRes(color)
    .width(40)
    .height(40)
Text.create(c)
    .text(title)
    .textSizeRes(R.dimen.my_text_size)
    .grow(1f)
Previous参考NextMount Specs

Last updated 7 years ago

使用 @OnCreateLayout 注解的方法必须具有 作为其第一个参数,后跟使用 @Prop 标注的参数列表。注解处理器将在构建时对参数列表以及API中其他约束条件进行验证。

第一个孩子是一个 组件,接收 colorRes 作为Prop ,并具有40dp的宽度和高度。

第二个孩子是一个 组件,接收名为 text 的属性,使用 grow(1f) 填充 MyComponent 中剩余可用的水平空间(等同于 Android 中LinearLayout 中的 layoutWeight)。文本大小在 my_text_size dimension 资源中定义。

您可以查看完整的 文档以查看该框架公布的所有布局功能。

ComponentContext
SolidColor
Text
Yoga