React Native 学习笔记五(关于样式的使用)
2016-09-13 14:12
806 查看
React Native中样式的使用和web的css基本上没有什么变化 样式名基本上是遵循了web上的CSS的命名,只是按照JS的语法要求使用了驼峰命名法,例如将
实际开发中 建议使用StyleSheet.create的方法进行声明 就像一个单独的css文件一样 以提高样式的复用 和代码的可读性 使用起来比较方便
我在上个state的基础上进行的修改 示例如下:
注意:后声明的样式会覆盖之前的样式
background-color改为
backgroundColor。
实际开发中 建议使用StyleSheet.create的方法进行声明 就像一个单独的css文件一样 以提高样式的复用 和代码的可读性 使用起来比较方便
我在上个state的基础上进行的修改 示例如下:
import { AppRegistry, StyleSheet, Text, View } from 'react-native'; class Blank extends Component{ //初始化state(从ES6开始 在construct中进行初始化 ES5中使用getInitinalState进行初始化 不过会逐渐被淘汰) constructor(props){ super(props); //初始化状态为显示文本 同时定义showText 后面根据showText修改state的值 this.state={showText:true}; //每隔200ms进行取反 隐藏文本操作 //lambda表达式 表示对于一个方法的某个参数的具体实现 也可以用来便利map集合等等 setInterval(()=>{ this.setState({showText:!this.state.showText}); },200); } render(){ let display=this.state.showText?this.props.text:' '; return( //根据当前的showText的值 决定时候显示text的内容 <Text style={styles.red}>{display}</Text> ); } } class helloReact extends Component { //渲染 render() { return ( //设置显示风格 自定义View 显示结果为Hellohello HelloWorld <View style={{backgroundColor:'yellow'}}> <Blank text='I want you'/> <Blank text='I want you1111'/> <Blank text='I want you2222'/> </View> ); } } const styles = StyleSheet.create({ container: { flex: 1, justifyContent: 'center', alignItems: 'center', backgroundColor: '#F5FCFF', }, welcome: { fontSize: 20, textAlign: 'center', margin: 10, }, red: { fontSize: 20, textAlign: 'right', margin: 10, color:'#ff0000' }, instructions: { textAlign: 'center', color: '#333333', marginBottom: 5, }, });
注意:后声明的样式会覆盖之前的样式
相关文章推荐
- React Native 学习笔记四(关于state的使用)
- React Native 学习笔记二十(关于ListView的使用)
- React Native 学习笔记(五)-- 样式Style
- React Native 学习笔记九(ScrollView的使用)
- react native 学习笔记 2016_1223 (环境,箭头函数,state设置,图片使用等)
- React Native 学习笔记十五(图片的使用)
- React Native 学习笔记六(关于宽高的设置)
- React-Native傻瓜式学习笔记(一):ListView的简单使用
- React-Native学习笔记之:弹框框架Popover简单使用
- React-Native学习笔记 使用ListView加载网络数据
- react native 学习笔记之指定样式,宽,高
- React Native 学习笔记十九(关于开发环境)
- React-Native学习笔记之:WebView控件使用
- React-Native学习笔记之:使用Tab react-native-tab-navigator框架
- MFC关于CBimtap类的使用和创建位图数组的问题【学习笔记】
- JS学习笔记(一):关于{}和[]的使用
- 【前端学习笔记】关于CSS通过一个块改变另一个块的样式
- 第三讲:React Native & HTML5+(学习笔记1)
- Android(java)学习笔记94:关于广播接收者的注册和使用心得
- react-native学习笔记——ViewStack组件