React Native开发之FlexBox代码+图解

前言

FlexBox布局是React Native的布局核心,鉴于自己对FlexBox还有很多概念不太清楚,这篇文章就当成是总结,并且分享出来给大家。

FlexBox布局能够帮助你更好的帮助你控制控件的大小和位置,Flexbox非常适合Mobile端的适配,我想这也是FaceBook为什么选择FlexBox作为React Native布局的原因吧。

Getting Start

react-native init  LearnFlexBox

运行

cd LearnFlexBox/
react-native run-ios

会看到默认的截图

由于模拟器截图实在太宽,所以本文把Demo范围限定在一个小的范围内
重写Render方法

render() {
    return (
      <View style={styles.container}>
        <View style={styles.exampleContainer}>

        </View>
      </View>
    );
  }

这里的exampleContainer风格如下

exampleContainer:{
    width:320,
    height:200,
    backgroundColor:'gray'
  }

Container和Item

  • Container就是容器,有些属性是设置到Container上的例如alignItems。设置到Container上的属性决定了如何布局内部的Item
  • Item, 在容器中的子视图。设置到Item上的属性,决定了自己在Container中的位置大小

有过iOS或者安卓原生开发的同学应该能有个更清楚的认识,很像View和Subview的关系

flexDirection

这个Container属性决定了按照哪个方向来布局Item,默认从上到下
添加一个style

exampleItem:{
    width:30,
    height:30,
    backgroundColor:'#27E6E2'
  }

然后,在添加三个子视图

<View style={styles.exampleContainer}>
  <View style={styles.exampleItem}></View>
  <View style={styles.exampleItem}></View>
  <View style={styles.exampleItem}></View>
</View>

然后,修改exampleContainer,添加一个属性flexDirection

exampleContainer:{
    width:320,
    height:200,
    backgroundColor:'gray',
    flexDirection:'column'
  },

当flexDirection设置为column的时候效果

当flexDirection设置为row的时候

注意:React Native的Flexbox目前不支持row-reverse和column-reverse

alignItems-垂直轴上的位置关系

有四个值

'flex-start', 'flex-end', 'center', 'stretch'

示例代码如下

<View style={{width:320,height:200,backgroundColor:'gray',alignItems:'stretch',flexDirection:'row'}}>
  <View style={{width:50,backgroundColor:'green'}}></View>
  <View style={{width:50,backgroundColor:'blue'}}></View>
  <View style={{width:50,height:50,backgroundColor:'red'}}></View>
  <View style={{width:50,height:60,backgroundColor:'orange'}}></View>
</View>


还有一种stretch表示拉伸,拉伸的时候不需要完整的设置高度宽度,比如

<View style={{width:320,height:200,backgroundColor:'gray',alignItems:'stretch',flexDirection:'row'}}>
  <View style={{width:50,backgroundColor:'green'}}></View>
  <View style={{width:50,backgroundColor:'blue'}}></View>
  <View style={{width:50,height:50,backgroundColor:'red'}}></View>
  <View style={{width:50,height:60,backgroundColor:'orange'}}></View>
</View>

效果


alignSelf

五个值,当Item有这个属性的时候,会优先读取item的alignSelf来布局,也就是说会覆盖Container的alignItems。例如

<View style={{width:320,height:200,backgroundColor:'gray',alignItems:'center',flexDirection:'row'}}>
  <View style={{width:50,height:30,backgroundColor:'green',alignSelf:'flex-end'}}></View>
  <View style={{width:50,height:40,backgroundColor:'blue',alignSelf:'flex-start'}}></View>
  <View style={{width:50,alignSelf:'stretch',backgroundColor:'red'}}></View>
  <View style={{width:50,alignSelf:'auto',height:60,backgroundColor:'orange'}}></View>
</View>

效果


justifyContent-水平轴上的位置关系

五个值

'flex-start','flex-end', 'center', 'space-between', 'space-around'

同样,我们还是举个例子,来看看实际效果
代码如下

<View style={{width:320,height:200,backgroundColor:'gray',justifyContent:'space-around',flexDirection:'row'}}>
  <View style={{width:50,height:30,backgroundColor:'green'}}></View>
  <View style={{width:50,height:40,backgroundColor:'blue'}}></View>
  <View style={{width:50,height:50,backgroundColor:'red'}}></View>
  <View style={{width:50,height:60,backgroundColor:'orange'}}></View>
</View>




flex-占据剩余空间的权重

例如,如下,不设置宽度,高度一样,三个item,flex都是1,那么每个item的宽度占据1/3

<View style={{width:320,height:200,backgroundColor:'gray',flexDirection:'row'}}>
  <View style=![这里写图片描述](http://img.blog.csdn.net/20160524215306065){{flex:1,height:30,backgroundColor:'green'}}></View>
  <View style={{flex:1,height:30,backgroundColor:'blue'}}></View>
  <View style={{flex:1,height:30,backgroundColor:'red'}}></View>
</View>

效果

当我们固定中间宽度为50,然后两边flex都是1的时候

flexWrap-决定在flex的方向上填满后是否换行

两个值,默认不换行

'wrap', 'nowrap'

代码

<View style={{width:320,height:200,backgroundColor:'gray',flexDirection:'column',flexWrap:'wrap'}}>
  <View style={{width:50,height:30,backgroundColor:'green'}}></View>
  <View style={{width:50,height:40,backgroundColor:'blue'}}></View>
  <View style={{width:50,height:50,backgroundColor:'red'}}></View>
  <View style={{width:50,height:60,backgroundColor:'orange'}}></View>
  <View style={{width:50,height:70,backgroundColor:'pink'}}></View>
</View>

换行效果


不换行效果


top/bottom/left/right

这个比较直观,距离上下左右的距离
例如,距离top:10,left:10

<View style={{width:320,height:200,backgroundColor:'gray',flexDirection:'row'}}>
  <View style={{top:10,left:10,width:30,height:50,backgroundColor:'green'}}></View>
</View>

效果


padding相关

与Padding相关的一共有以下几个

  • padding
  • paddingBottom
  • paddingLeft
  • paddingRight
  • paddingTop
  • paddingVertical
  • paddingHorizontal

例如,一个100*100View在没有padding的时候

<View style={{width:320,height:200,backgroundColor:'gray',alignItems:'flex-start',paddingLeft:20,paddingTop:10}}>
  <View style={{width:100,height:100,backgroundColor:'pink'}}></View>
</View>

效果


设置padding

<View style={{width:320,height:200,backgroundColor:'gray',alignItems:'flex-start',paddingLeft:20,paddingTop:10}}>
  <View style={{width:100,height:100,backgroundColor:'pink'}}></View>
</View>

效果


Border相关

几个相关的属性

  • borderWidth
  • borderTopWidth
  • borderRightWidth
  • borderLeftWidth
  • borderBottomWidth

举个例子

<View style={{width:320,height:200,backgroundColor:'gray',alignItems:'center',justifyContent:'center'}}>
  <View style={{width:50,height:50,backgroundColor:'pink',borderBottomWidth:3,borderColor:'white'}}></View>
</View>

margin相关

于margin相关的一共有以下几个属性

  • margin
  • marginBottom
  • marginHorizontal
  • marginLeft
  • marginRight
  • marginTop
  • marginVertical

在不设置margin的情况下

<View style={{width:320,height:200,backgroundColor:'gray',alignItems:'center',justifyContent:'center',flexDirection:'row'}}>
  <View style={{width:30,height:40,backgroundColor:'pink' }}></View>
  <View style={{width:30,height:50,backgroundColor:'blue'}}></View>
  <View style={{width:30,height:60,backgroundColor:'green'}}></View>
</View>

效果


设置margin的时候

<View style={{width:320,height:200,backgroundColor:'gray',alignItems:'center',justifyContent:'center',flexDirection:'row'}}>
  <View style={{width:30,height:40,backgroundColor:'pink' }}></View>
  <View style={{width:30,height:50,backgroundColor:'blue',marginLeft:10,marginRight:20}}></View>
  <View style={{width:30,height:60,backgroundColor:'green'}}></View>
</View>

效果


参考:
http://blog.csdn.net/hello_hwc/article/details/51480458
A Complete Guide to Flexbox
react-native 之布局篇
React Native官网也有FlexBox相关的文档,不过讲解的内容不多。

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 195,898评论 5 462
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 82,401评论 2 373
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 143,058评论 0 325
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 52,539评论 1 267
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 61,382评论 5 358
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 46,319评论 1 273
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 36,706评论 3 386
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 35,370评论 0 254
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 39,664评论 1 294
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 34,715评论 2 312
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 36,476评论 1 326
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 32,326评论 3 313
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 37,730评论 3 299
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 29,003评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 30,275评论 1 251
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 41,683评论 2 342
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 40,877评论 2 335

推荐阅读更多精彩内容