【React Native】FlexBox布局

一、FlexBox布局
  • FlexBox是什么?

弹性盒模型(The Flexible Box Module),又叫Flexbox,意为“弹性布局”,旨在通过弹性的方式来对齐和分布容器中内容的空间,使其能适应不同屏幕,为盒装模型提供最大的灵活性。
Flex布局主要思想是:让容器有能力让其子项目能够改变其宽度、高度(甚至是顺序),以最佳方式填充可用空间;
React native中的FlexBox是这个规范的一个子集。

二、Flexbox在开发中的应用场景

  • Flexbox在布局中能够解决什么问题?
    浮动布局
    各种机型屏幕的适配
    水平和垂直居中
    自动分配宽度
    ......
  • 在CSS中,常规的布局是基于块和内联流方向,而Flex布局是基于flex-flow流,下图很好解释了Flex布局的思想

1.png

容器默认存在两根轴:水平的主轴(main axis)和垂直的交叉轴(cross axis)。主轴的开始位置(与边框的交叉点)叫做main start,结束位置叫做main end;交叉轴的开始位置叫做cross start,结束位置叫做cross end
项目默认沿主轴排列。单个项目占据的主轴空间叫做main size,占据的交叉轴空间叫做cross size

三、Flexbox的常用属性

  • 容器属性

  • flexDirection: row | row-reverse | column | column-reverse
    该属性决定主轴的方向(即项目的排列方向)。
    主要是父视图来控制子视图的显示

    row:主轴为水平方向,起点在左端;
    row-reverse:主轴为水平方向,起点在右端;
    column(默认值):主轴为垂直方向,起点在上沿。
    column-reverse:主轴为垂直方向,起点在下沿。

export default class meituanDemo extends Component {
  render() {
    return (
      <View style={styles.contains}>
        <View style={styles.title}> 
            <Text style={{width: 80, textAlign: 'center'}}>row</Text>
            <Text style={{width: 80, textAlign: 'center'}}>row-reverse</Text>  
            <Text style={{width: 80, textAlign: 'center'}}>column</Text>  
            <Text style={{width: 80, textAlign: 'center'}}>column-reverse</Text>   
        </View>
        <View style={styles.example}> 
          <View style={styles.one}> 
            <View style={styles.top}>
            </View>
            <View style={styles.middle}>
            </View>
            <View style={styles.bottom}>
            </View> 
          </View>
          <View style={styles.two}>  
            <View style={styles.top}>
            </View>
            <View style={styles.middle}>
            </View>
            <View style={styles.bottom}>
            </View> 
          </View>
          <View style={styles.three}> 
            <View style={styles.top}>
            </View>
            <View style={styles.middle}>
            </View>
            <View style={styles.bottom}>
            </View> 
          </View>
          <View style={styles.four}> 
            <View style={styles.top}>
            </View>
            <View style={styles.middle}>
            </View>
            <View style={styles.bottom}>
            </View> 
          </View>
        </View>
      </View> 
    );
  }
}

const styles = StyleSheet.create({
    contains: { 
      flexDirection: 'column',
      // justifyContent: 'space-around', 
      // alignItems: 'center',
      flex: 1
    },
    title: { 
      flexDirection: 'row',
      justifyContent: 'space-around', 
      alignItems: 'center', 
      height: 30,
      marginTop: 200
    },
    example: {
      flexDirection: 'row',
      justifyContent: 'space-around', 
      alignItems: 'center', 
      height: 200,
    },
    one: {
      backgroundColor: 'yellow',
      width: 80,
      height: 200,
      flexDirection: 'row'
    },
    two: {
      backgroundColor: 'purple',
      width: 80,
      height: 200,
      flexDirection: 'row-reverse'
    },
    three: {
      backgroundColor: 'blue',
      width: 80,
      height: 200,
      flexDirection: 'column'
    },
    four: {
      backgroundColor: 'red',
      width: 80,
      height: 200,
      flexDirection: 'column-reverse'
    },
    top: {
      backgroundColor: 'green',
      flex: 1
    },
    middle: {
      backgroundColor: 'black',
      flex: 1
    },
    bottom: {
      backgroundColor: 'orange',
      flex: 1
    } 
}); 
flexDirection 效果图.png
  • justifyContent:flex-start | flex-end | center | space-between | space-around
    定义了伸缩项目在主轴线的对齐方式:

flex-start(默认值):伸缩项目向一行的起始位置靠齐。
flex-end:伸缩项目向一行的结束位置靠齐。
center:伸缩项目向一行的中间位置靠齐。
space-between:两端对齐,项目之间的间隔都相等。
space-around:伸缩项目会平均地分布在行里,两端保留一半的空间。

export default class meituanDemo extends Component {
  render() {
    return (
      <View style={styles.contains}>
        <View style={styles.title}> 
            <Text style={{height: 30, textAlign: 'center'}}>flex-start</Text>
            <Text style={{height: 30, textAlign: 'center'}}>flex-end</Text>  
            <Text style={{height: 30, textAlign: 'center'}}>center</Text>  
            <Text style={{height: 30, textAlign: 'center'}}>space-between</Text>   
            <Text style={{height: 30, textAlign: 'center'}}>space-around</Text> 
        </View>
        <View style={styles.example}> 
          <View style={styles.one}> 
            <View style={styles.left}>
            </View>
            <View style={styles.middle}>
            </View>
            <View style={styles.right}>
            </View>  
          </View>
          <View style={styles.two}>  
            <View style={styles.left}>
            </View>
            <View style={styles.middle}>
            </View>
            <View style={styles.right}>
            </View>  
          </View>
          <View style={styles.three}> 
            <View style={styles.left}>
            </View>
            <View style={styles.middle}>
            </View>
            <View style={styles.right}>
            </View>  
          </View>
          <View style={styles.four}> 
            <View style={styles.left}>
            </View>
            <View style={styles.middle}>
            </View>
            <View style={styles.right}>
            </View>  
          </View>
          <View style={styles.five}>
            <View style={styles.left}>
            </View>
            <View style={styles.middle}>
            </View>
            <View style={styles.right}>
            </View>  
          </View> 
        </View>
      </View> 
    );
  }
}

const styles = StyleSheet.create({
    contains: { 
      flexDirection: 'row',
      justifyContent: 'space-around', 
      alignItems: 'center',
      flex: 1
    },
    title: { 
      flexDirection: 'column',
      justifyContent: 'space-around', 
      alignItems: 'center', 
      width: 100,
      height: 300 
    },
    example: {
      flexDirection: 'column',
      justifyContent: 'space-around',  
      flex: 1, 
      height: 300,
    },
    one: {
      backgroundColor: 'gray', 
      height: 50,  
      justifyContent: 'flex-start',
      flexDirection: 'row',
    },
    two: {
      backgroundColor: 'gray', 
      height: 50, 
      justifyContent: 'flex-end',
      flexDirection: 'row',
    },
    three: {
      backgroundColor: 'gray', 
      height: 50, 
      justifyContent: 'center',
      flexDirection: 'row',
    },
    four: {
      backgroundColor: 'gray', 
      height: 50, 
      justifyContent: 'space-between',
      flexDirection: 'row',
    },
    five: {
      backgroundColor: 'gray', 
      height: 50, 
      justifyContent: 'space-around',
      flexDirection: 'row',
    },
    left: {
      backgroundColor: 'green', 
      width: 50
    },
    middle: {
      backgroundColor: 'black', 
      width: 50
    },
    right: {
      backgroundColor: 'orange', 
      width: 50
    } 
});
justifyContent 效果图.png
  • alignItems: flex-start | flex-end | center | baseline | stretch
    定义项目在交叉轴上如何对齐,可以把其想像成侧轴(垂直于主轴)的“对齐方式”。

flex-start:交叉轴的起点对齐。
flex-end:交叉轴的终点对齐 。
center:交叉轴的中点对齐。
baseline:项目的第一行文字的基线对齐。
stretch(默认值):如果项目未设置高度或设为auto,将占满整个容器的高度。

const styles = StyleSheet.create({
  contains: { 
    flexDirection: 'row',
    justifyContent: 'space-around', 
    alignItems: 'center',
    flex: 1
  },
  title: { 
    flexDirection: 'column',
    justifyContent: 'space-around', 
    alignItems: 'center', 
    width: 100,
    height: 300 
  },
  example: {
    flexDirection: 'column',
    justifyContent: 'space-around',  
    flex: 1, 
    height: 300,
  },
  one: {
    backgroundColor: 'gray', 
    height: 50,  
    justifyContent: 'flex-start',
    flexDirection: 'row',
    alignItems: 'flex-start'
  },
  two: {
    backgroundColor: 'gray', 
    height: 50, 
    justifyContent: 'flex-end',
    flexDirection: 'row',
    alignItems: 'flex-end'
  },
  three: {
    backgroundColor: 'gray', 
    height: 50, 
    justifyContent: 'center',
    flexDirection: 'row',
    alignItems: 'center'
  },
  four: {
    backgroundColor: 'gray', 
    height: 50, 
    justifyContent: 'space-between',
    flexDirection: 'row',
    alignItems: 'baseline'
  },
  five: {
    backgroundColor: 'gray', 
    height: 50, 
    justifyContent: 'space-around',
    flexDirection: 'row',
    alignItems: 'stretch'
  },
  left: {
    backgroundColor: 'green', 
    width: 50,
    height: 30
  },
  middle: {
    backgroundColor: 'black', 
    width: 50,
    height: 20
  },
  right: {
    backgroundColor: 'orange', 
    width: 50,
    height: 50
  } 
});
alignItems 效果图.png
  • flexWrap: nowrap | wrap | wrap-reverse
    默认情况下,项目都排在一条线(又称"轴线")上。flex-wrap属性定义,如果一条轴线排不下,如何换行。

nowrap(默认值):不换行。
wrap:换行,第一行在上方。

export default class meituanDemo extends Component {
  render() {
    return (
      <View style={styles.contains}>
        <View style={styles.title}> 
            <Text style={{height: 30, textAlign: 'center'}}>nowrap(默认值)</Text>
            <Text style={{height: 30, textAlign: 'center'}}>wrap</Text>   
        </View>
        <View style={styles.example}> 
          <View style={styles.one}> 
            <View style={{backgroundColor: 'green',width: 50,height: 20,marginLeft:10,marginTop:2}}>
            </View>
            <View style={{backgroundColor: 'green',width: 50,height: 20,marginLeft:10,marginTop:2}}>
            </View>
            <View style={{backgroundColor: 'green',width: 50,height: 20,marginLeft:10,marginTop:2}}>
            </View>  
            <View style={{backgroundColor: 'green',width: 50,height: 20,marginLeft:10,marginTop:2}}>
            </View>
            <View style={{backgroundColor: 'green',width: 50,height: 20,marginLeft:10,marginTop:2}}>
            </View>
            <View style={{backgroundColor: 'green',width: 50,height: 20,marginLeft:10,marginTop:2}}>
            </View>  
          </View>
          <View style={styles.two}>  
            <View style={{backgroundColor: 'green',width: 50,height: 20,marginLeft:10,marginTop:2}}>
            </View>
            <View style={{backgroundColor: 'green',width: 50,height: 20,marginLeft:10,marginTop:2}}>
            </View>
            <View style={{backgroundColor: 'green',width: 50,height: 20,marginLeft:10,marginTop:2}}>
            </View>  
            <View style={{backgroundColor: 'green',width: 50,height: 20,marginLeft:10,marginTop:2}}>
            </View>
            <View style={{backgroundColor: 'green',width: 50,height: 20,marginLeft:10,marginTop:2}}>
            </View>
            <View style={{backgroundColor: 'green',width: 50,height: 20,marginLeft:10,marginTop:2}}>
            </View>  
          </View> 
        </View>
      </View> 
    );
  }
}
const styles = StyleSheet.create({
    contains: { 
      flexDirection: 'row',
      justifyContent: 'space-around', 
      alignItems: 'center',
      flex: 1
    },
    title: { 
      flexDirection: 'column',
      justifyContent: 'space-around', 
      alignItems: 'center', 
      width: 100,
      height: 300 
    },
    example: {
      flexDirection: 'column',
      justifyContent: 'space-around',  
      flex: 1, 
      height: 300,
    },
    one: {
      backgroundColor: 'gray', 
      height: 50,  
      justifyContent: 'flex-start',
      flexDirection: 'row',
      flexWrap: 'nowrap'
    },
    two: {
      backgroundColor: 'gray', 
      height: 50, 
      justifyContent: 'flex-end',
      flexDirection: 'row',
      flexWrap: 'wrap'
    },
    three: {
      backgroundColor: 'gray', 
      height: 50, 
      justifyContent: 'center',
      flexDirection: 'row',  
    },
    four: {
      backgroundColor: 'gray', 
      height: 50, 
      justifyContent: 'space-between',
      flexDirection: 'row',
      alignItems: 'baseline'
    },
    five: {
      backgroundColor: 'gray', 
      height: 50, 
      justifyContent: 'space-around',
      flexDirection: 'row',
      alignItems: 'stretch'
    },
    left: {
      backgroundColor: 'green', 
      width: 50,
      height: 30
    },
    middle: {
      backgroundColor: 'black', 
      width: 50,
      height: 20
    },
    right: {
      backgroundColor: 'orange', 
      width: 50,
      height: 50
    } 
});
flexWrap 效果图.png
  • 元素属性

    • flex
      flex属性是flex-grow, flex-shrinkflex-basis的简写,默认值为0 1 auto。后两个属性可选。 默认值为“0 1 auto”。

    宽度 = 弹性宽度 * ( flexGrow / sum( flexGorw ) )

    • alignSelf: “auto | flex-start | flex-end | center | baseline | stretch
      align-self属性允许单个项目有与其他项目不一样的对齐方式,可覆盖align-items属性。默认值为auto,表示继承父元素的align-items属性,如果没有父元素,则等同于stretch

四、在React Native 中使用FlexlBox布局

  • 获取当前屏幕的宽度、高度和scale
var {width, height, scale} = Dimensions.get('window')
export default class meituanDemo extends Component {
  render() {
    return (
      <View style={styles.contains}> 
        <Text style={styles.welcome}>
          当前屏幕的宽度:{width + '\n'}
          当前屏幕的高度:{height + '\n'}
          当前屏幕的scale:{scale + '\n'}
        </Text>
      </View> 
    );
  }
}

const styles = StyleSheet.create({
  contains: { 
    flexDirection: 'row',
    justifyContent: 'center', 
    alignItems: 'center',
    flex: 1,
  },
  welcome: { 
    textAlign: 'center',
    flex: 1
  } 
});
获取屏幕信息.png
  • 水平居中,垂直居中和水平垂直居中
export default class meituanDemo extends Component {
  render() {
    return (
      <View style={styles.contains}>
        <View style={styles.title}> 
            <Text style={{height: 30, textAlign: 'center'}}>水平居中</Text>
            <Text style={{height: 30, textAlign: 'center'}}>垂直居中</Text>   
            <Text style={{height: 30, textAlign: 'center'}}>水平垂直居中</Text>   
        </View>
        <View style={styles.example}> 
          <View style={styles.one}>  
            <Image source={{uri:'http://farm4.staticflickr.com/3795/9269794168_3ac58fc15c_b.jpg'}} style={{width:30,height:30}} /> 
          </View>
          <View style={styles.two}>    
            <Image source={{uri:'http://farm4.staticflickr.com/3795/9269794168_3ac58fc15c_b.jpg'}} style={{width:30,height:30}} /> 
          </View> 
          <View style={styles.three}>   
            <Image source={{uri:'http://farm4.staticflickr.com/3795/9269794168_3ac58fc15c_b.jpg'}} style={{width:30,height:30}} />  
          </View> 
        </View>
      </View> 
    );
  }
}
const styles = StyleSheet.create({
    contains: { 
      flexDirection: 'row',
      justifyContent: 'space-around', 
      alignItems: 'center',
      flex: 1
    },
    title: { 
      flexDirection: 'column',
      justifyContent: 'space-around', 
      alignItems: 'center', 
      width: 100,
      height: 300 
    },
    example: {
      flexDirection: 'column',
      justifyContent: 'space-around',  
      flex: 1, 
      height: 300,
    },
    one: {
      backgroundColor: 'gray', 
      height: 50,   
      alignItems: 'center'
    },
    two: {
      backgroundColor: 'gray', 
      height: 50, 
      justifyContent: 'center', 
    },
    three: {
      backgroundColor: 'gray', 
      height: 50, 
      alignItems: 'center',
      justifyContent: 'center', 
    }
});

居中效果图.png

备注:一旦设置alignItems属性之后,组件的大小包裹随着内容的尺寸;此外水平居中和垂直居中还要结合FlexDirection进行判断。

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

推荐阅读更多精彩内容