【ReactNative】react-native 布局
react-native 布局1 flex布局基本概念flex是Flexible Box的缩写,意为"弹性布局",用来为盒状模型提供最大的灵活性。采用flex布局的元素,称为flex容器(flex container),简称"容器"。它的所有子元素自动成为容器成员,称为flex项目(flex item),简称"项目"。如下图所示:
容器默认存在两根轴:主轴(main axis)和交叉轴(cross axis)。主轴的开始位置(与边框的交叉点)叫做main start,结束位置叫做main end;交叉轴的开始位置叫做cross start,结束位置叫做cross end。 Flex布局与Android的线性布局(LinearLayout)有点类似,都可以设置布局方向,对齐方式,以及项目的布局占位权重,区别是flex容器中项目分布的总长度超出屏幕宽度,超出的那部分项目不可见,项目不会变形,或者可以设置flexWrap属性,让容器可以分行布局,所有项目都能显示出来。 2 flex基本属性 flex属性声明在:
// https://developer.mozilla.org/en-US/docs/Web/CSS/flex-direction
flexDirection: ReactPropTypes.oneOf([
'row','column'
]),// https://developer.mozilla.org/en-US/docs/Web/CSS/flex-wrap
flexWrap: ReactPropTypes.oneOf([
'wrap',68)">'nowrap'
]),38)">// How to align children in the main direction
//developer.mozilla.org/en-US/docs/Web/CSS/justify-content
justifyContent: ReactPropTypes.oneOf([
'flex-start',68)">'flex-end',68)">'center',68)">'space-between',68)">'space-around'
]),38)">// How to align children in the cross direction
//developer.mozilla.org/en-US/docs/Web/CSS/align-items
alignItems: ReactPropTypes.oneOf([
'stretch'
]),38)">// How to align the element in the cross direction
/alignSelf: ReactPropTypes.oneOf([
'auto',38)">/developer.mozilla.org/en-US/docs/Web/CSS/flex
flex: ReactPropTypes.number,
由上述代码,我们可以看到flex的属性并不多,而且很好记忆,以下将会一一介绍 flex属性可以分为容器属性和项目属性 项目属性包括: 以下介绍会使用到一些代码和图片,先定义两个简单组件,方便理解 //定义一个默认半径为20,颜色为#527fe4的圆组件 var Circle = React.createClass({ render : function(){ var size = this.props.size || 20; var color = this.props.color || '#527fe4'; return <View style={{backgroundColor:color,borderRadius:size/2,height:size,width:size,margin:1}}/> },}); //定义一个放置标题和项目的容器,传入的value属性将会是需要介绍的flex属性 var Value = React.createClass({ render : function(){ var value = <View> <Text style={styles.valueText}>{this.props.title}</Text> <{[styles.valueContainer,this.props.value]}> {this.props.children} </View> </View>; return value; },}); //定义一个数组放置5个圆 var children = [<Circle/>,<Circle/>]; 2.1 容器属性
2.2 项目属性
3 Layout的其他属性 layout除了flex属性之外,当然还有其他属性,同样声明在: |属性 |类型 |描述 |
|---------------------|------|-----------------------------------|
|width |number|容器或者项目的宽度 |
|height |number|容器或者项目的高度 |
|top,bottom,left,right|number|在父容器的上下左右偏移量 |
|margin |number|留边,留边的空间不属于容器或者项目自身空间|
|marginHorizontal |number|水平方向留边 |
|marginVertical |number|垂直方向留边 |
|padding |number|填充,填充的空间输入容器或者项目自身空间 |
|paddingHorizontal |number|水平方向填充 |
|paddingVertical |number|垂直方向填充 |
|borderWidth |number|边界宽度 |
|position |enum |位置方式:absolute与relative |
position:默认值为relative |值 |描述 |
|--------|------|
|absolute|绝对布局|
|relative|相对布局|
react的默认位置方式是relative,项目是一个接一个排列下去的,absolute为绝对布局,一般会与left和top属性一起使用。有时候我们需要实现某些项目重叠起来,absolute属性就能发挥作用了,例如下图: react的基本组件暂时不支持以图片作为背景,所以这里的的 <View style={{width:80,height:'center'}}>
<Image style={{position:'absolute',left:0,top:0,resizeMode:'contain',width:80,height:80}} source={require('image!finance_usercenter_ic_into')}/> <{{width:80,textAlign:'center',color:'white',fontSize:16}}>转入</Text> </View>
这里的View跟Android的View有点不一样,View是可以作为容器也可以作为项目,View作为容器还有其他很多属性,例如backgroundColor,borderWidth,borderColor,opacity等等,这里不一一介绍。 react native的宽高是不需要带单位的,那些 alignItems
调整伸缩项目在侧轴上的定位方式 可选值:
alignSelf auto,249)">stretch
原文链接:http://139.196.14.76/t/react-native-flex/181/1 参考文章:http://www.52php.cn/article/p-fgftethr-a.html (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |