React Native 之 flexbox 布局详解篇 (清晰明了)_html/css_WEB-ITnose
回复『 1024 』你懂的!
阅读本文大约需要5分钟
本文详情
本文讲解React Native中的布局,该布局用CSS的flex布局,掌握了这个布局对后面RN应用的开发用途非常之大。本文由 showCar 投稿(点击 阅读原文 )。文章地址: http://blog.csdn.net/u014486880
1.常用属性
RN的flexbox主要有以下几个属性alignItems,alignSelf,flex,flexDirection,flexWrap,justifyContent。
2.flexDirection
该属性用于指定主轴的方向。即指定子view的布局方向。它有两个值可设置。
-
row:横向布局。
-
column:纵向布局。
这个属性很简单,先看row的代码段:
一个View里有四个小View,小View的底色是黑的,看下效果图: 可以看到,四个view横向的进行布局。那改成column呢,样式修改如下: 看下效果图:
就是纵向布局。
3.alignItems
用于定义子组件在垂直方向上的对齐方式。有四个属性可设置:flex-start,flex-end,center,stretch。
-
flex-start:与父组件的顶部对齐。
-
flex-end:与父组件的底部对齐。
-
center:处于父容器的中间位置。
-
stretch:竖直上填充整个容器。
首先来看下flex-start,顺便改了下子组件的颜色,代码如下: 看下效果图:
就是和父容器顶部对齐。
看下flex-end属性,代码就不贴出来了,只要改alignItems属性就好了。效果图如下:
可以看到,和底部对齐了。 再看下center,这个可以说是用的最多的属性,它会让子组件位于父容器的中间位置,看下效果图:
stretch就是竖直填充,前提是子组件没有设置height属性。看下效果图:
4.justifyContent
有竖直就水平。justifyContent和alignItems是相对的。它有五个属性可以设置,分别是flex-start,flex-end,center,space-between,space-around。
-
flex-start:伸缩项目与父容器左端靠齐。
-
flex-end:与父容器右端靠齐。
-
center:水平居中。
-
space-between:第一个子组件位于父容器左端,最后一个子组件位于父容器最右端。然后平均分配在父容器水平方向上。
-
space-around:所有子组件平均分配在父容器的水平方向上,左右都有留空隙。
先看水平居中(center)的,先看下代码:
父容器设置了justifyContent:’center’属性,所以理论上子组件应该会水平剧中,来看下是否正确。如下:
justifyContent:’flex-start’,水平居左:
justifyContent:’flex-end’,水平居右:
这些都挺简单的,来看下space-between和space-around的区别,先看下space-between的效果图:
可以看到它左右都不留空隙。均匀分布。 再看下space-around的效果图: 它左右都留有空隙,是平均的位于整个界面的水平方向上。
5.alignSelf
该属性用来设置单独组件的竖直对齐方式,与alignItem有点像。有五个属性可以设置,auto,flex-start,flex-end,center,streth。
-
auto:按照自身设置的宽高来显示,如果没设置,效果跟streth一样。
-
flex-start:与父容器顶部对齐。
-
flex-end:与父容器底部对齐。
-
center:位于垂直位置。
-
streth:垂直拉伸。
这个用法跟上面的很像,只是它用于单个组件,如本例子的子View中,看下代码: 以上几个子View设置了不同的样式 ,看下效果图:
看到了,flex-start就是顶部对齐,flex-end就是与底部对齐。第三个View是streth,垂直拉伸了。第四个View是auto,因为设置了高度,所以显示如图所示。没有显示center,但它的效果可想而知,就不再演示啦。
6.flex
flex指设置伸缩项目的伸缩样式,可以把它类比成android中的weight属性。 看一个代码就清楚它的用法了。 效果图如下:
可以看到,flex为2的组件宽度为flex为1宽度的两倍,flex为4组件宽度则为flex为1的组件宽度的4倍。
7.flexWrap
其实这些属性都是CSS原有的属性,只是RN只支持了部分的属性。flexWrap用于设置是否可换行,有两个属性可设置nowrap和wrap。
-
nowrap:即使空间不够也不换行。
-
wrap:空间不够的话自动换行。
如设置成wrap时,空间不够效果图如下:
第四个放不下,就换行了。
8.再说两句
本文对于flex布局做了详细说明,而且应该算是图文搭配,解释的非常清晰明了,感谢投稿者~
本公众号欢迎大家投稿,如果你希望你的文章可以被更多人看到,直接将md、doc等格式的文章到我邮箱即可( 781931404@qq.com ),也可以加我QQ/微信( 781931404 )好友,需要注明(投稿),谢谢。

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



HTML is suitable for beginners because it is simple and easy to learn and can quickly see results. 1) The learning curve of HTML is smooth and easy to get started. 2) Just master the basic tags to start creating web pages. 3) High flexibility and can be used in combination with CSS and JavaScript. 4) Rich learning resources and modern tools support the learning process.

HTML defines the web structure, CSS is responsible for style and layout, and JavaScript gives dynamic interaction. The three perform their duties in web development and jointly build a colorful website.

AnexampleofastartingtaginHTMLis,whichbeginsaparagraph.StartingtagsareessentialinHTMLastheyinitiateelements,definetheirtypes,andarecrucialforstructuringwebpagesandconstructingtheDOM.

WebdevelopmentreliesonHTML,CSS,andJavaScript:1)HTMLstructurescontent,2)CSSstylesit,and3)JavaScriptaddsinteractivity,formingthebasisofmodernwebexperiences.

GiteePages static website deployment failed: 404 error troubleshooting and resolution when using Gitee...

The Y-axis position adaptive algorithm for web annotation function This article will explore how to implement annotation functions similar to Word documents, especially how to deal with the interval between annotations...

To achieve the effect of scattering and enlarging the surrounding images after clicking on the image, many web designs need to achieve an interactive effect: click on a certain image to make the surrounding...

The necessity of registering VueRouter in the index.js file under the router folder When developing Vue applications, you often encounter problems with routing configuration. Special...
