react-native是facebook在 react基础上的开发出的可以用js方式开发native应用的项目,基本上沿用了reactjs的组件开发模式,使用flex布局,最近也熟悉了一下flex的布局方式,之前有接触过adoble flex,所以多flex box并不陌生,今天正好有空写了个qq登录窗口,顺便熟悉下flex布局方式。

主要代码:
/**
* Sample React Native App
* https://github.com/facebook/react-native
* @flow
*/
import React, { Component } from 'react';
import {
AppRegistry,
StyleSheet,
Text,
View,
Image,
TextInput
} from 'react-native';
import Dimensions from 'Dimensions';
let deviceWidth = Dimensions.get('window').width;
export class loginView extends Component {
render() {
return (
<View style={styles.container}>
<Image source={require('./img/icon1.jpg')} style={styles.headerStyle} />
<TextInput style={styles.textInputStyle}
placeholder={'请输入QQ号码'}
clearButtonMode={'while-editing'}
/>
<TextInput style={styles.textInputStyle}
placeholder={'请输入密码'}
password={true}
clearButtonMode={'while-editing'}
/>
<View style={styles.loginBtnStyle}>
<Text style={{ color: '#fff' }}>登录</Text>
</View>
<View style={styles.serviceStyle}>
<Text style={styles.nologin}>无法登录</Text>
<Text style={styles.forgetPwd}>忘记密码</Text>
</View>
<View style={styles.otherLogin}>
<Text style={{ marginRight: 10 }}>其它登录方式</Text>
<Image source={require('./img/icon3.png')} style={styles.iconImgStyle} />
<Image source={require('./img/icon5.png')} style={styles.iconImgStyle} />
<Image source={require('./img/icon7.png')} style={styles.iconImgStyle} />
</View>
</View>
);
}
}
const styles = StyleSheet.create({
container: {
flex: 1,
// 侧轴对其方式
alignItems: 'center',
backgroundColor: '#eeeeee',
},
textInputStyle: {
backgroundColor: '#fff',
marginBottom: 1,
height: 40,
padding: 5,
textAlign: 'center'
},
headerStyle: {
width: 80,
height: 80,
borderRadius: 40,
borderWidth: 3,
borderColor: 'white',
marginTop: 50,
marginBottom: 40
},
loginBtnStyle: {
marginTop: 20,
width: deviceWidth * 0.9,
height: 40,
backgroundColor: '#4bbcf4',
justifyContent: 'center',
alignItems: 'center',
borderRadius: 8
},
imgStyle: {
borderRadius: 8
},
serviceStyle: {
marginTop: 20,
marginBottom: 20,
// 设置主轴方向
flexDirection: 'row',
// 主轴对其方式
justifyContent: 'space-between',
width: deviceWidth * 0.9
},
otherLogin: {
flexDirection: 'row',
alignItems: 'center',
position: 'absolute',
bottom: 10,
margin: 20
},
nologin: {
color: '#07b1ea'
},
forgetPwd: {
color: '#07b1ea'
},
iconImgStyle: {
width: 30,
height: 30,
marginRight: 10
},
});
module.exports = loginView;flex布局主要需要了解的属性:(以下内容来源阮老师整理)
flex-direction
flex-wrap
flex-flow
justify-content
align-items
align-content
flex-direction属性
flex-direction属性决定主轴的方向(即项目的排列方向)。
.box {
flex-direction: row | row-reverse | column | column-reverse;
}
它可能有4个值。
row(默认值):主轴为水平方向,起点在左端。
row-reverse:主轴为水平方向,起点在右端。
column:主轴为垂直方向,起点在上沿。
column-reverse:主轴为垂直方向,起点在下沿。
flex-wrap属性
默认情况下,项目都排在一条线(又称"轴线")上。flex-wrap属性定义,如果一条轴线排不下,如何换行。

.box{
flex-wrap: nowrap | wrap | wrap-reverse;
}它可能取三个值。
(1)nowrap(默认):不换行。

(2)wrap:换行,第一行在上方。

(3)wrap-reverse:换行,第一行在下方。

flex-flow
flex-flow属性是flex-direction属性和flex-wrap属性的简写形式,默认值为row nowrap。
.box {
flex-flow: <flex-direction> || <flex-wrap>;
}justify-content属性
justify-content属性定义了项目在主轴上的对齐方式。
.box {
justify-content: flex-start | flex-end | center | space-between | space-around;
}
它可能取5个值,具体对齐方式与轴的方向有关。下面假设主轴为从左到右。
flex-start(默认值):左对齐
flex-end:右对齐
center: 居中
space-between:两端对齐,项目之间的间隔都相等。
space-around:每个项目两侧的间隔相等。所以,项目之间的间隔比项目与边框的间隔大一倍。
align-items属性
align-items属性定义项目在交叉轴上如何对齐。
.box {
align-items: flex-start | flex-end | center | baseline | stretch;
}
align-content属性
align-content属性定义了多根轴线的对齐方式。如果项目只有一根轴线,该属性不起作用。
.box {
align-content: flex-start | flex-end | center | space-between | space-around | stretch;
}
该属性可能取6个值。
flex-start:与交叉轴的起点对齐。
flex-end:与交叉轴的终点对齐。
center:与交叉轴的中点对齐。
space-between:与交叉轴两端对齐,轴线之间的间隔平均分布。
space-around:每根轴线两侧的间隔都相等。所以,轴线之间的间隔比轴线与边框的间隔大一倍。
stretch(默认值):轴线占满整个交叉轴。