样式
React Native 不实现 CSS,而是依赖于 JavaScript 来为你的应用程序设置样式。这是一个有争议的决定,你可以阅读那些幻灯片,了解背后的基本原理。
声明样式
在 React Native 中声明样式的方法如下:
1. var styles = StyleSheet.create({
2. base: {
3. width: 38,
4. height: 38,
5. },
6. background: {
7. backgroundColor: '#222222',
8. },
9. active: {
10. borderWidth: 2,
11. borderColor: '#00ff00',
12. },
13. });
StyleSheet.create 的创建是可选的,但提供了一些关键优势。它通过将它们转换为引用一个内部表的纯数字,来确保值是不可变的和不透明的。通过将它放在文件的最后,也确保了它们为应用程序只创建一次,而不是每一个 render 都创建。
所有的属性名称和值是工作在网络中的一个子集。对于布局来说,React Native实现了 Flexbox。
使用样式
所有的核心组件接受样式属性。
1. <Text style={styles.base} />
2. <View style={styles.background} />
它们也接受一系列的样式。
1. <View style={[styles.base, styles.background]} />
行为与 Object.assign 相同:在冲突值的情况下,从最右边元素的值将会优先,并且 falsy 值如 false,undefined 和 null 将被忽略。一个常见的模式是基于某些条件有条件地添加一个样式。
1. <View style={[styles.base, this.state.active && styles.active]} />
最后,如果真的需要,您还可以在render中创建样式对象,但是这种做法非常不赞成。最后把它们放在数组定义中。
1. <View
2. style={[styles.base, {
3. width: this.state.width,
4. height: this.state.width * this.state.aspectRatio
5. }]}
6. />
样式传递
为了让一个 call site 定制你的子组件的样式,你可以通过样式传递。使用 View.propTypes.style 和 Text.propTypes.style,以确保只有样式被传递了。
1. var List = React.createClass({
2. propTypes: {
3. style: View.propTypes.style,
4. elementStyle: View.propTypes.style,
5. },
6. render: function() {
7. return (
8. <View style={this.props.style}>
9. {elements.map((element) =>
10. <View style={[styles.element, this.props.elementStyle]} />
11. )}
12. </View>
13. );
14. }
15. });
16. // ... in another file ...
17. <List style={styles.list} elementStyle={styles.listElement} />
属性支持
你可以在以下的链接中检测最新的 CSS 属性支持。
