React Native CSS样式传递
来源:互联网 发布:js源代码加密 编辑:程序博客网 时间:2024/05/15 00:40
转载:http://www.zplan.net/news.php?id=194
React Native如何定义css 样式
React Native 不实现 CSS,而是依赖于 JavaScript 来为你的应用程序设置样式。这是一个有争议的决定,你可以阅读那些幻灯片,了解背后的基本原理。
声明样式
在 React Native 中声明样式的方法如下:
var styles = StyleSheet.create({ base: { width: 38, height: 38, }, background: { backgroundColor: '#222222', }, active: { borderWidth: 2, borderColor: '#00ff00', },});
StyleSheet.create 的创建是可选的,但提供了一些关键优势。它通过将它们转换为引用一个内部表的纯数字,来确保值是不可变的和不透明的。通过将它放在文件的最后,也确保了它们为应用程序只创建一次,而不是每一个 render 都创建。
所有的属性名称和值是工作在网络中的一个子集。对于布局来说,React Native实现了 Flexbox。
使用样式
所有的核心组件接受样式属性。
<Text style={styles.base} /><View style={styles.background} />
它们也接受一系列的样式。
<View style={[styles.base, styles.background]} />
行为与 Object.assign 相同:在冲突值的情况下,从最右边元素的值将会优先,并且 falsy 值如 false,undefined 和 null 将被忽略。一个常见的模式是基于某些条件有条件地添加一个样式。
<View style={[styles.base, this.state.active && styles.active]} />
最后,如果真的需要,您还可以在render中创建样式对象,但是这种做法非常不赞成。最后把它们放在数组定义中。
<View style={[styles.base, { width: this.state.width, height: this.state.width * this.state.aspectRatio }]}/>
样式传递
为了让一个 call site 定制你的子组件的样式,你可以通过样式传递。使用 View.propTypes.style 和 Text.propTypes.style,以确保只有样式被传递了。
var List = React.createClass({ propTypes: { style: View.propTypes.style, elementStyle: View.propTypes.style, }, render: function() { return ( <View style={this.props.style}> {elements.map((element) => <View style={[styles.element, this.props.elementStyle]} /> )} </View> ); }});// ... in another file ...<List style={styles.list} elementStyle={styles.listElement} />
属性支持
你可以在以下的链接中检测最新的 CSS 属性支持。
- View 属性
- Image 属性
- Text 属性
- Flex 属性
0 0
- React Native CSS样式传递
- React Native 三:样式
- React Native之样式
- React Native之样式
- React-native之样式
- React Native之样式
- react native样式设计
- React-Native 样式指南
- 【React Native】React Native元素样式设置
- react-native css属性
- React Native 之样式总结
- React-Native 自定义TextInput样式
- React-Native:删除线样式
- React Native 属性.样式.状态
- [React-Native]样式和布局
- react-native样式的框架
- React-native外部引入样式
- react native 之样式分离
- 关于属性中包含shadow的属性
- 关于margin-top溢出父节点元素的处理方法
- npm install 无响应解决方案
- 关于串口接收的处理策略
- Bootstrap 学习之(十五)------ 媒体对象,well
- React Native CSS样式传递
- Unity摄像机旋转以及拉近拉远代码
- spring boot 中使用定时器
- idea16(2016.1.1)下载、安装、破解
- hadoop之端口被占用问题namenode无法启动
- 关于css3动画基础知识总结
- qduoj 韬韬与炉石传说
- 微信公众平台实现获取用户OpenID的方法
- java基础总结之java对象的序列化和反序列化