Facebook出品的Android声明式开源新框架Litho文档翻译-准备工作

欢迎转载,转载请标明出处.
英文原文文档地址: Litho-doc

快速开始

准备工作


集成Litho至你的工程



gradle


你可以通过在你的Gradle的build.gradle文件中添加以下代码来把Litho添加至你的Android工程中.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
dependencies {
// ...
// Litho
compile 'com.facebook.litho:litho-core:0.2.0'
compile 'com.facebook.litho:litho-widget:0.2.0'
provided 'com.facebook.litho:litho-annotations:0.2.0'
annotationProcessor 'com.facebook.litho:litho-processor:0.2.0'
// SoLoader
compile 'com.facebook.soloader:soloader:0.2.0'
// 调试选项
debugCompile 'com.facebook.litho:litho-stetho:0.2.0'
// 集成Fresco的支持
compile 'com.facebook.litho:litho-fresco:0.2.0'
// 供测试
testCompile 'com.facebook.litho:litho-testing:0.2.0'
}


Buck


你可以通过在你的Buck的BUCK文件中添加以下代码来把Litho添加至你的Android工程中.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
android_prebuilt_aar(
name = "litho",
aar = ":litho.aar",
visibility = ["PUBLIC"],
)
remote_file(
name = "litho-core.aar",
sha1 = "sha1here",
url = "mvn:com.facebook.litho:litho-core:aar:0.2.0",
)
prebuilt_jar(
name = "litho-annotation",
binary_jar = ":litho-annotation.jar",
visibility = ["PUBLIC"],
)
remote_file(
name = "litho-processor.aar",
sha1 = "sha1here",
url = "mvn:com.facebook.litho:litho-processor:aar:0.2.0",
)
prebuilt_jar(
name = "litho-processor",
binary_jar = ":litho-processor.jar",
visibility = ["PUBLIC"],
)
remote_file(
name = "litho-annotation.jar",
sha1 = "sha1here",
url = "mvn:com.facebook.litho:litho-annotation:jar:0.2.0",
)
android_prebuilt_aar(
name = "litho-widget",
aar = ":litho-widget.aar",
visibility = ["PUBLIC"],
)
remote_file(
name = "litho-widget.aar",
sha1 = "sha1here",
url = "mvn:com.facebook.litho:litho-widget:aar:0.2.0",
)
litho_android_library(
...
# Your target here
...
annotation_processor_deps = [
":litho-annotation",
":litho-processor",
],
annotation_processors = [
"com.facebook.litho.processor.ComponentsProcessor",
],
deps = [
":litho",
":litho-widget",
...
]
)


测试集成情况


你可以通过添加一个由Litho创建的view至activity来测试集成情况

首先,初始化SoLoader.Litho依赖SoLoader来加载底层布局引擎Yoga所需的本地库.在Application中进行这个操作比较合适.

1
2
3
4
5
6
7
8
9
10
[MyApplication.java]
public class MyApplication extends Application {
@Override
public void onCreate() {
super.onCreate();
SoLoader.init(this, false);
}
}

然后,添加一个预定义的Litho Text组件至activity来显示”Hello world”.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
[MyActivity.java]
import com.facebook.litho.ComponentContext;
import com.facebook.litho.LithoView;
public class MyActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
final ComponentContext c = new ComponentContext(this);
final LithoView lithoView = LithoView.create(
this /* context */,
Text.create(c)
.text("Hello, World!")
.textSizeDip(50)
.build());
setContentView(lithoView);
}
}

现在,当你运行app的时候,你就可以看到”Hello World”显示在屏幕上.




回到导航页