2.React Native Flex布局介绍以及实践
没有具体的介绍基本的语法,主要是说明了与标准的CSS Flex的一些区别以及一个实战的例子。如果你想学好Flex布局还是应该多多的写一些Demo。 基本介绍Flex(弹性)布局是一种普遍使用于网页的布局方式,现在在移动端也有一些尝试(Android端Flex布局)。React Native基本的布局方式就是使用的Flex布局。 基本概念
命令方式:驼峰式的。正确:flexDirection。错误:flex-direction。React Native中的尺寸都是与密度无关的像素(independent pixels)。 Group属性这种叫法(Android叫法)不是很准确,意思是一个盒子里面可以放一些东西,Group属性就是定义这个盒子的属性。
看了下CSS中Flex布局的内容,相对而言React Native简化了一些,还有flex-flow和align-content在React Native中没有。 Child属性同样这种叫法(Android叫法)不是很准确,意思是一个盒子里面可以放一些东西,Child属性就是定义放在盒子里面东西的属性。
相比于CSS的标准,没有了order属性以及flex属性对应的意思不一样。 其它属性
text1: { fontSize: 14,backgroundColor: '#0f0',marginLeft: 20,height: 25,position: 'absolute',zIndex: 3,// 展示在text2上面 },text2: { fontSize: 14,backgroundColor: '#f00',left: 10,borderWidth: 20,zIndex: 2,},
布局中一些概念区分 实战参考PPTV聚力视频里面的电影介绍,来用React Native实现。 实战参考 直接写代码了。 import React,{Component} from 'react'; import { StyleSheet,Image,Button,Text,View,} from 'react-native'; export default class MovieInfo extends Component{ _onPress(){ } render(){ return( <View style={styles.container}> {/* 整体是一个'column'样式的,然后中间的介绍是一个'row'样式的,图片右边又是一个 'column'样式的,每一行区分出来又是'row'样式的。 */} <Text style={styles.brifeText}>简介</Text> <View style={styles.moviewBrifeContainer}> <Image style={styles.movieImage} source={{uri:"http://pic4.qiyipic.com/image/20161222/d2/59/a_100039885_m_601_180_236.jpg"}}></Image> <View style={styles.movieInfo}> <View style={styles.movieDirector}> <Text style={styles.movieDirectorTitle}>导演:</Text> <Text style={styles.movieDirectorText}>路飞</Text> </View> <View style={styles.movieActor}> <Text style={styles.movieActorTitle}>主演:山下智久,长泽雅美,新垣结衣</Text> <Text style={styles.movieActorText}></Text> </View> <View style={styles.movieType}> <Text style={styles.movieTypeTitle}>类型:日漫,热血</Text> <Text style={styles.movieTypeText}></Text> </View> <View style={styles.movieArea}> <Text style={styles.movieAreaTitle}>地区:日本</Text> <Text style={styles.movieAreaText}></Text> </View> <View style={styles.movieYear}> <Text style={styles.movieYearTitle}>年代:1999</Text> <Text style={styles.movieYearText}></Text> </View> <View style={styles.movieSource}> <View style={styles.movieSource2}> <Text style={styles.movieSourceTitle}>评分:</Text> <Text style={styles.movieSourceText}>9.1</Text> </View> <Button onPress={this._onPress.bind(this)} style={styles.movieSourceButton} color='#ff9313' title="我要评分" accessibilityLabel="Learn more about purple"></Button> </View> </View> </View> <Text style={styles.detailText}>有个男人他拥有世界上切财富、名望和权势,他就是「海盗王」高路德?罗杰。 在临死前说过这样一句话:让全世界的人都奔向大海 「想要我的财宝吗?想要的话全就拿去吧……!你们去找吧!我把世界上的一切都放在那里了」。 后来世界上的人们将这个宝藏称作“一个大秘宝”(One Piece),许多人为了争夺大秘宝One Piece,无数海盗扬起旗帜,互相斗争,后来就形成了「大海盗时代」。 主角蒙奇?D?路飞在遥远的路途上找寻着志同道合的伙伴,携手共进「伟大航线」,目标当上「海盗王」。</Text> </View> ); } } const styles = StyleSheet.create({ container:{ flexDirection: 'column',justifyContent: 'flex-start',backgroundColor: '#fafafa',},brifeText:{ fontSize: 15,margin: 12,color: '#323232',moviewBrifeContainer:{ flexDirection: 'row',marginLeft: 12,marginRight: 12,marginTop: 4,movieImage:{ width: 100,aspectRatio: 0.758,movieInfo:{ flexDirection: 'column',flex: 1,movieDetail:{ flexDirection: 'column',movieDirector:{ flexDirection: 'row',movieDirectorTitle:{ fontSize: 11,color: '#646464',movieDirectorText:{ fontSize: 11,movieActor:{ flexDirection: 'row',marginTop: 5,movieActorTitle:{ fontSize: 11,movieActorText:{ fontSize: 11,movieType:{ flexDirection: 'row',movieTypeTitle:{ fontSize: 11,movieTypeText:{ fontSize: 11,movieArea:{ flexDirection: 'row',movieAreaTitle:{ fontSize: 11,movieAreaText:{ fontSize: 11,movieYear:{ flexDirection: 'row',movieYearTitle:{ fontSize: 11,movieYearText:{ fontSize: 11,movieSource:{ flexDirection: 'row',justifyContent: 'space-between',flexGrow: 1,movieSource2:{ flexDirection: 'row',alignItems: 'flex-start',movieSourceTitle:{ fontSize: 11,movieSourceText:{ fontSize: 11,movieSourceButton:{ fontSize: 13,color: '#fff',detailText:{ fontSize: 11,marginTop: 16,marginBottom: 16,});
问题
展示样式 布局分析 总结自己还有去细学React相关的语法,而是先学习了布局,倒不是语法不是很重要的,而是语法的学习还是应该放在实践中,当然布局也是需要多多写写才能更好的体会。大概是自己感觉先有一个总体上的了解再去学习细节可能更加有效吧。 参考Facebook Android Flex布局项目 (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |