3.1 Flexbox

1.Flexbox layout

The main idea behind the flex layout is to give the container the ability to alter its items' width/height (and order) to best fill the available space (mostly to accommodate to all kind of display devices and screen sizes). A flex container expands items to fill available free space, or shrinks them to prevent overflow.

 3.1 Flexbox  - 图1

2.flex:1

 3.1 Flexbox  - 图2

  1. const styles = StyleSheet.create({
  2. container: {
  3. flex: 1
  4. },
  5. header: {
  6. height: 200,
  7. backgroundColor: 'red'
  8. },
  9. main: {
  10. flex: 1,
  11. backgroundColor: 'blue'
  12. },
  13. footer: {
  14. height: 200,
  15. backgroundColor: 'green'
  16. },
  17. text: {
  18. color: '#ffffff',
  19. fontSize: 80
  20. }
  21. });

3.flexDirection:'row'|'column'

 3.1 Flexbox  - 图3

 3.1 Flexbox  - 图4

4.justifyContent:'flex-start'|'flex-end'|'center'|'space-between'|'space-around'

 3.1 Flexbox  - 图5

5.alignItems:'flex-start'|'flex-end'|'center'|'stretch'

 3.1 Flexbox  - 图6

6.alignSelf:'auto'|'flex-start'|'flex-end'|'center'|'stretch'

 3.1 Flexbox  - 图7

7.flexWrap:'wrap'|'nowrap'

 3.1 Flexbox  - 图8

8.Box model

 3.1 Flexbox  - 图9

width = borderLeftWidth(25)+paddingLeft(25)+100+borderRightWidth(25)+paddingRight(25)=200

height = borderTopWidth(25)+paddingTop(25)+100+borderBottomWidth(25)+paddingBottom(25)=200

  1. class Main extends Component {
  2. render() {
  3. return (
  4. <View style={styles.container}>
  5. <View style={styles.header}>
  6. <Text style={styles.text}>200X100</Text>
  7. </View>
  8. <View style={styles.main}>
  9. <View style={styles.mainContent}>
  10. <Text style={styles.text}>100X100</Text>
  11. </View>
  12. </View>
  13. <View style={styles.footer}>
  14. <Text style={styles.text}>200X100</Text>
  15. </View>
  16. </View>
  17. );
  18. }
  19. }
  20. const styles = StyleSheet.create({
  21. container: {
  22. flex: 1,
  23. justifyContent: 'center',
  24. alignItems: 'center'
  25. },
  26. header: {
  27. height: 100,
  28. width: 200,
  29. backgroundColor: 'red'
  30. },
  31. main: {
  32. height: 200,
  33. width: 200,
  34. padding: 25,
  35. borderWidth: 25,
  36. borderColor: 'black',
  37. margin: 25,
  38. backgroundColor: 'blue'
  39. },
  40. mainContent: {
  41. flex: 1,
  42. justifyContent: 'center',
  43. alignItems: 'center',
  44. backgroundColor: 'red'
  45. },
  46. footer: {
  47. height: 100,
  48. width: 200,
  49. backgroundColor: 'green'
  50. },
  51. text: {
  52. color: '#ffffff',
  53. fontSize: 20
  54. }
  55. });

 3.1 Flexbox  - 图10

原文: https://unbug.gitbooks.io/react-native-training/content/31_flexbox.html