flex流布局的用法

1:如果设置了父view属性display:flex;而没有设置flex-direction属性,那么flex-direction默认是row。

2:在流布局中flex-direction: row-reverse;就是flex-direction: row;的反方向, flex-direction: column-reverse;是flex-direction: column;的反方向,示例代码如下:

示例一:
<view style='width:375rpx;display:flex;  flex-direction:  row;background-color:red'>
  <text style=''>我是文本</text>
</view>
<view style='width:600rpx;display:flex;  flex-direction:  row-reverse;background-color:green'>
  <text style=''>我是文本</text>
</view>
效果图
示例二:
<view style='height:375rpx;width:100rpx;display:flex;  flex-direction:  column-reverse;background-color:red'>
  <text style=''>我是文本</text>
</view>
<view style='height:600rpx;width:100rpx;display:flex;  flex-direction:  column;background-color:green'>
  <text style=''>我是文本</text>
</view>
效果图

3:流布局的flex-wrap属性,默认情况下是不换行的,nowrap(默认):不换行;wrap:换行,第一行在上方;wrap-reverse:换行,第一行在下方。

示例代码如下:
<view style='display:flex;  flex-direction:  row;background-color:red; flex-wrap:  wrap;'>
  <text style=''>我是文本</text>
  <text style=''>我是文本</text>
  <text style=''>我是文本</text>
  <text style=''>我是文本</text>
  <text style=''>我是文本</text>
  <text style=''>我是文本</text>
</view>
<view style='display:flex;  flex-direction:  row;background-color:yellow; flex-wrap:  wrap-reverse;'>
  <text style=''>我是文本</text>
  <text style=''>我是文本</text>
  <text style=''>我是文本</text>
  <text style=''>我是文本</text>
  <text style=''>我是文本</text>
  <text style=''>我是文本</text>
</view>
<view style='display:flex;  flex-direction:  row;background-color:green;'>
  <text style=''>我是文本</text>
  <text style=''>我是文本</text>
  <text style=''>我是文本</text>
  <text style=''>我是文本</text>
  <text style=''>我是文本</text>
  <text style=''>我是文本</text>
</view>
效果图

4:流布局的flex-flow属性:flex-flow属性是flex-direction属性和flex-wrap属性的简写形式,默认值为row nowrap

5:justify-content属性定义了项目在主轴上的对齐方式,具体对齐方式与轴的方向有关。下面假设主轴为从左到右。

flex-start(默认值):左对齐
flex-end:右对齐
center: 居中
space-between:两端对齐,项目之间的间隔都相等。
space-around:每个项目两侧的间隔相等。所以,项目之间的间隔比项目与边框的间隔大一倍。

<view style='display:flex;  flex-direction:  row;background-color:yellow; justify-content:  flex-start;'>
  <text style=''>左对齐</text>
  <text style=''>左对齐</text>
  <text style=''>左对齐</text>
</view>
<view style='display:flex;  flex-direction:  row;background-color:green;justify-content:  center;'>
  <text style=''>居中</text>
  <text style=''>居中</text>
  <text style=''>居中</text>
</view>
<view style='display:flex;  flex-direction:  row;background-color:red;justify-content:  flex-end;'>
  <text style=''>右对齐</text>
  <text style=''>右对齐</text>
  <text style=''>右对齐</text>
</view>
<view style='display:flex;  flex-direction:  row;background-color:pink;justify-content:  space-between;'>
  <text style=''>两端对齐</text>
  <text style=''>两端对齐</text>
  <text style=''>两端对齐</text>
</view>
<view style='display:flex;  flex-direction:  row;background-color:orange;justify-content:  space-around;'>
  <text style=''>两侧间隔</text>
  <text style=''>两侧间隔</text>
  <text style=''>两侧间隔</text>
</view>
效果图

6:流布局中的子view可以设置order属性,order属性定义项目的排列顺序。数值越小,排列越靠前,默认为0,示例代码如下:

<view style='display:flex;  flex-direction:  row;background-color:yellow; '>
  <text style='order:6'>文本1</text>
  <text style='order:26'>文本2</text>
  <text style='order:16'>文本3</text>
  <text style='order:60'>文本4</text>
  <text style='order:6'>文本5</text>
  <text style=''>文本6</text>
</view>
效果图

7:flex-grow属性:flex-grow属性定义项目的放大比例,默认为0,即如果存在剩余空间,也不放大。如果所有项目的flex-grow属性都为1,则它们将等分剩余空间(如果有的话)。如果一个项目的flex-grow属性为2,其他项目都为1,则前者占据的剩余空间将比其他项多一倍。

<view style='display:flex;  flex-direction:  row;background-color:yellow; '>
  <text style='flex-grow: 1;'>文本1</text>
  <text style='flex-grow: 1;'>文本2</text>
  <text style='flex-grow: 1;'>文本3</text>
</view>
<view style='display:flex;  flex-direction:  row;background-color:red; '>
  <text style='flex-grow: 2;'>文本1</text>
  <text style='flex-grow: 1;'>文本2</text>
  <text style='flex-grow: 1;'>文本3</text>
</view>
<view style='display:flex;  flex-direction:  row;background-color:pink;'>
  <text style='flex-grow: 1;'>文本1</text>
  <text style='flex-grow: 2;'>文本2</text>
  <text style='flex-grow: 1;'>文本3</text>
</view>
<view style='display:flex;  flex-direction:  row;background-color:green;'>
  <text style='flex-grow: 1;'>文本1</text>
  <text style='flex-grow: 1;'>文本2</text>
  <text style='flex-grow: 3;'>文本3</text>
</view>
效果图

8:flex-shrink属性:flex-shrink属性定义了项目的缩小比例,默认为1,即如果空间不足,该项目将缩小。 如果所有项目的flex-shrink属性都为1,当空间不足时,都将等比例缩小。如果一个项目的flex-shrink属性为0,其他项目都为1,则空间不足时,前者不缩小。 负值对该属性无效。

<view style='display:flex;  flex-direction:  row;background-color:yellow; '>
  <text style='flex-shrink: 1;'>文本3</text>
  <text style='flex-shrink: 0;'>文本2</text>
  <text style='flex-shrink: 1;'>文本3</text>
  <text style='flex-shrink: 1;'>文本3</text>
  <text style='flex-shrink: 1;'>文本3</text>
  <text style='flex-shrink: 1;'>文本3</text>
  <text style='flex-shrink: 1;'>文本3</text>
  <text style='flex-shrink: 1;'>文本3</text>
  <text style='flex-shrink: 1;'>文本3</text>
</view>
<view style='display:flex;  flex-direction:  row;background-color:pink; '>
  <text style='flex-shrink: 1;'>文本3</text>
  <text style='flex-shrink: 1;'>文本2</text>
  <text style='flex-shrink: 1;'>文本3</text>
  <text style='flex-shrink: 1;'>文本3</text>
  <text style='flex-shrink: 1;'>文本3</text>
  <text style='flex-shrink: 1;'>文本3</text>
  <text style='flex-shrink: 1;'>文本3</text>
  <text style='flex-shrink: 1;'>文本3</text>
  <text style='flex-shrink: 1;'>文本3</text>
</view>
<view style='display:flex;  flex-direction:  row;background-color:orange; '>
  <text style='flex-shrink: 1;'>文本3</text>
  <text style='flex-shrink: 10;'>文本2</text>
  <text style='flex-shrink: 1;'>文本3</text>
  <text style='flex-shrink: 1;'>文本3</text>
  <text style='flex-shrink: 1;'>文本3</text>
  <text style='flex-shrink: 1;'>文本3</text>
  <text style='flex-shrink: 1;'>文本3</text>
  <text style='flex-shrink: 1;'>文本3</text>
  <text style='flex-shrink: 1;'>文本3</text>
</view>
效果图

9:flex属性:flex属性是flex-grow, flex-shrink 和 flex-basis的简写,默认值为0 1 auto。后两个属性可选。 该属性有两个快捷值:auto (1 1 auto) 和 none (0 0 auto)。 建议优先使用这个属性,而不是单独写三个分离的属性,因为浏览器会推算相关值。

10:align-items属性:align-items属性适用于所有的flex容器,它是用来设置每个flex元素在侧轴上的默认对齐方式。在使用该属性时记得先固定父view的高度,只有先给父view的高度设置固定的值该属性才能生效,align-items和align-content有相同的功能,不过不同点是它是用来让每一个单行的容器居中而不是让整个容器居中。

<view style='display:flex;height:150rpx;  flex-direction:  row;background-color:yellow;'>
  <text style=''>文本3</text>
  <text style=''>文本3</text>
  <text style=''>文本3</text>
</view>
<view style='display:flex;height:150rpx;  flex-direction:  row;background-color:pink;align-items:flex-start; '>
  <text style=''>文本3</text>
  <text style=''>文本3</text>
  <text style=''>文本3</text>
</view>
<view style='display:flex;height:150rpx;  flex-direction:  row;background-color:orange;align-items:center; '>
  <text style=''>文本3</text>
  <text style=''>文本3</text>
  <text style=''>文本3</text>
</view>
<view style='display:flex;height:150rpx;  flex-direction:  row;background-color:green;align-items:flex-end; '>
  <text style=''>文本3</text>
  <text style=''>文本3</text>
  <text style=''>文本3</text>
</view>
<view style='display:flex;height:150rpx;  flex-direction:  row;background-color:#bbacba; '>
  <text style=''>文本3</text>
  <text style='font-size:20rpx;'>文本3</text>
  <text style=''>文本3</text>
</view>
<view style='display:flex;height:150rpx;  flex-direction:  row;background-color:#89abca;align-items:baseline; '>
  <text style=''>文本3</text>
  <text style='font-size:20rpx;'>文本3</text>
  <text style=''>文本3</text>
</view>
效果图

11:align-self属性是写在flex的子view属性上的,在使用该属性时记得先固定父view的高度,align-self属性定义flex的子项单独在侧轴(纵轴)方向上的对齐方式。注意:align-self 属性可重写灵活容器的 align-items 属性。

<view style='display:flex;height:150rpx;  flex-direction:  row;background-color:yellow;'>
  <text style=' align-self: flex-start;'>文本3</text>
  <text style=' align-self: center;'>文本3</text>
  <text style=' align-self: flex-end;'>文本3</text>
</view>
效果图

12:align-content属性定义了多根轴线的对齐方式。如果项目只有一根轴线,该属性不起作用,换句话说就是如果父view中的内容不换行,该属性是不起作用的,因此为了换行,这是我们需要用到flex-wrap: wrap;属性。在使用该属性时记得先固定父view的高度

该属性可能取6个值:
flex-start:与交叉轴的起点对齐。
flex-end:与交叉轴的终点对齐。
center:与交叉轴的中点对齐。
space-between:与交叉轴两端对齐,轴线之间的间隔平均分布。
space-around:每根轴线两侧的间隔都相等。所以,轴线之间的间隔比轴线与边框的间隔大一倍。
stretch(默认值):轴线占满整个交叉轴。

<view style='display:flex;height:250rpx;  flex-direction:  row;background-color:#89abca;  align-content: stretch;  flex-wrap:  wrap;'>
  <text style='width:100%'>文本3</text>
  <text style='width:100%'>文本3</text>
  <text style=''>文本3</text>
</view>
<view style='display:flex;height:250rpx;  flex-direction:  row;background-color:yellow;  align-content: flex-start;  flex-wrap:  wrap;'>
  <text style='width:100%'>文本3</text>
  <text style='width:100%'>文本3</text>
  <text style=''>文本3</text>
</view>
<view style='display:flex;height:250rpx;  flex-direction:  row;background-color:pink;  align-content: center;  flex-wrap:  wrap;'>
  <text style='width:100%'>文本3</text>
  <text style='width:100%'>文本3</text>
  <text style=''>文本3</text>
</view>
<view style='display:flex;height:250rpx;  flex-direction:  row;background-color:orange;  align-content: flex-end;  flex-wrap:  wrap;'>
  <text style='width:100%'>文本3</text>
  <text style='width:100%'>文本3</text>
  <text style=''>文本3</text>
</view>
<view style='display:flex;height:250rpx;  flex-direction:  row;background-color:green;  align-content: space-around;  flex-wrap:  wrap;'>
  <text style='width:100%'>文本3</text>
  <text style='width:100%'>文本3</text>
  <text style=''>文本3</text>
</view>
<view style='display:flex;height:250rpx;  flex-direction:  row;background-color:#bbacba;  align-content: space-between;  flex-wrap:  wrap;'>
  <text style='width:100%'>文本3</text>
  <text style='width:100%'>文本3</text>
  <text style=''>文本3</text>
</view>
效果图

以上flex学习的参考网址:

https://blog.csdn.net/weixin_37049906/article/details/77066238
https://www.jianshu.com/p/997e1a83f07e
http://www.cnblogs.com/zmc-change/p/6178757.html
http://www.ruanyifeng.com/blog/2015/07/flex-grammar.html
http://www.ruanyifeng.com/blog/2015/07/flex-examples.html

13:关于块级元素(这里指view)和非块级元素(这里指text)的心得:

1:块级元素是霸道的,当它出现的时候,如果不设置width加以限制的话,它会填满屏幕的宽度,这时如果给它设置宽度,不管多给宽度还是少给宽度,它都要了,而非块级元素则不是,它只会占用刚刚好的宽度,不管多给宽度还是少给宽度,它都不要;
2:块级元素会自动换行,即使宽度没有填满屏幕的宽度,也会自己对处于一行,很独特,而非块级元素则不是;
3:只有用flex才能管的住块级元素,给父块级元素设置了flex之后,其子块级元素就老实了,它就只占用刚刚好的宽度,不敢再占用整个屏幕的宽度了,并且它也不敢强行自己独占一行了,另外非块级元素也老实了,给它设置再小的宽度,它也得接受,不能不要了;
示例代码如下:
<view style='width:700rpx;background-color:red;margin-bottom:40rpx;'>
  <view style='background-color:green;'>文本3</view>
  <text style='background-color:yellow;width:200rpx'>文本3</text>
  <text style='background-color:pink;width:200rpx'>文本3</text>
</view>
<view style='width:700rpx;background-color:red;margin-bottom:40rpx;'>
  <view style='background-color:green;width:200rpx'>文本3</view>
  <text style='background-color:yellow;width:200rpx'>文本3</text>
  <text style='background-color:pink;width:200rpx'>文本3</text>
</view>
<view style='width:700rpx;background-color:red;margin-bottom:40rpx;'>
  <view style='background-color:green;width:20rpx'>文本3</view>
  <text style='background-color:yellow;width:20rpx'>文本3</text>
  <text style='background-color:pink;width:20rpx'>文本3</text>
</view>
<view style='width:700rpx;background-color:orange; display: flex;margin-bottom:40rpx;'>
  <view style='background-color:green;'>文本3</view>
  <text style='background-color:yellow;width:200rpx'>文本3</text>
  <text style='background-color:pink;width:200rpx'>文本3</text>
</view>
<view style='width:700rpx;background-color:orange; display: flex;margin-bottom:40rpx;'>
  <view style='background-color:green;width:200rpx'>文本3</view>
  <text style='background-color:yellow;width:200rpx'>文本3</text>
  <text style='background-color:pink;width:200rpx'>文本3</text>
</view>
<view style='width:700rpx;background-color:orange; display: flex;margin-bottom:40rpx;'>
  <view style='background-color:green;width:20rpx'>文本3</view>
  <text style='background-color:yellow;width:20rpx'>文本3</text>
  <text style='background-color:pink;width:20rpx'>文本3</text>
</view>
<view style='background-color:green;width:200rpx;margin-bottom:40rpx;'>文本3</view>
<text style='background-color:yellow;width:200rpx;margin-bottom:40rpx;'>文本3</text>
<text style='background-color:yellow;width:30rpx;margin-bottom:40rpx;'>文本3</text>
效果图
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 200,527评论 5 470
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 84,314评论 2 377
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 147,535评论 0 332
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 54,006评论 1 272
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 62,961评论 5 360
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 48,220评论 1 277
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 37,664评论 3 392
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 36,351评论 0 254
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 40,481评论 1 294
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 35,397评论 2 317
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 37,443评论 1 329
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 33,123评论 3 315
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 38,713评论 3 303
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 29,801评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 31,010评论 1 255
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 42,494评论 2 346
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 42,075评论 2 341

推荐阅读更多精彩内容

  • 在学习weex的过程中看到了常用标签相关的内容,为了自己以后能够快速查阅特整理出此文档。 a 简介组件定义了指向某...
    TyroneTang阅读 4,632评论 1 3
  • H5移动端知识点总结 阅读目录 移动开发基本知识点 calc基本用法 box-sizing的理解及使用 理解dis...
    Mx勇阅读 4,376评论 0 26
  • 选择qi:是表达式 标签选择器 类选择器 属性选择器 继承属性: color,font,text-align,li...
    wzhiq896阅读 1,724评论 0 2
  • 选择qi:是表达式 标签选择器 类选择器 属性选择器 继承属性: color,font,text-align,li...
    love2013阅读 2,300评论 0 11
  • ①刚刚过去的一年,无论世界、国家,还是家庭、个人,都经历了各自路途中的阴晴雨雪。过去这一年中的喜怒哀乐,哪些是值得...
    俊岐阅读 271评论 0 0