React-Native之flexbox布局
http://blog.csdn.net/u014486880/article/details/51385688 这篇博客稍微讲解下React-Native中的布局。比较简单。RN的而布局是用css中的flexbox布局,所以布局起来与Android传统的布局样式有点像。接下来就结合图片一起来看看。 常用属性讲解RN的flexbox主要有以下几个属性alignItems,alignSelf,flex,flexDirection,flexWrap,justifyContent。 flexDirection该属性用于指定主轴的方向。即指定子view的布局方向。它有两个值可设置。
这个属性很简单,先看row的代码段: <code class="hljs xml has-numbering">render:function(){ return( <span class="hljs-tag"><<span class="hljs-title">View</span> <span class="hljs-attribute">style</span>=<span class="hljs-value">{styles.flexStyle}</span>></span> <span class="hljs-tag"><<span class="hljs-title">View</span> <span class="hljs-attribute">style</span>= {<span class="hljs-attribute">styles.flexSub</span>}/></span> <span class="hljs-tag"><<span class="hljs-title">View</span> <span class="hljs-attribute">style</span>= {<span class="hljs-attribute">styles.flexSub</span>}/></span> <span class="hljs-tag"><<span class="hljs-title">View</span> <span class="hljs-attribute">style</span>= {<span class="hljs-attribute">styles.flexSub</span>}/></span> <span class="hljs-tag"><<span class="hljs-title">View</span> <span class="hljs-attribute">style</span>= {<span class="hljs-attribute">styles.flexSub</span>}/></span> <span class="hljs-tag"></<span class="hljs-title">View</span>></span> ); } ………… var styles = StyleSheet.create({ flexStyle:{ height:600,flexDirection:'row',},flexSub:{ flex:1,height:300,backgroundColor:'#333333',marginRight:10,)}</code><ul class="pre-numbering" style=""><li>1</li><li>2</li><li>3</li><li>4</li><li>5</li><li>6</li><li>7</li><li>8</li><li>9</li><li>10</li><li>11</li><li>12</li><li>13</li><li>14</li><li>15</li><li>16</li><li>17</li><li>18</li><li>19</li><li>20</li><li>21</li><li>22</li><li>23</li><li>24</li></ul><ul class="pre-numbering" style=""><li>1</li><li>2</li><li>3</li><li>4</li><li>5</li><li>6</li><li>7</li><li>8</li><li>9</li><li>10</li><li>11</li><li>12</li><li>13</li><li>14</li><li>15</li><li>16</li><li>17</li><li>18</li><li>19</li><li>20</li><li>21</li><li>22</li><li>23</li><li>24</li></ul> 一个View里有四个小View,小View的底色是黑的,看下效果图: <code class="hljs cs has-numbering"><span class="hljs-keyword">var</span> styles = StyleSheet.create({ flexStyle:{ height:<span class="hljs-number">600</span>,flexDirection:<span class="hljs-string">'column'</span>,flexSub:{ flex:<span class="hljs-number">1</span>,height:<span class="hljs-number">100</span>,backgroundColor:<span class="hljs-string">'#333333'</span>,marginBottom:<span class="hljs-number">10</span>,)}</code><ul class="pre-numbering" style=""><li>1</li><li>2</li><li>3</li><li>4</li><li>5</li><li>6</li><li>7</li><li>8</li><li>9</li><li>10</li><li>11</li><li>12</li></ul><ul class="pre-numbering" style=""><li>1</li><li>2</li><li>3</li><li>4</li><li>5</li><li>6</li><li>7</li><li>8</li><li>9</li><li>10</li><li>11</li><li>12</li></ul> 看下效果图: alignItems用于定义子组件在垂直方向上的对齐方式。有四个属性可设置:flex-start,flex-end,center,stretch。
首先来看下flex-start,顺便改了下子组件的颜色,代码如下: <code class="hljs cs has-numbering"><span class="hljs-keyword">return</span>( <View style={styles.flexStyle}> <View style= {styles.flexSelf1}/> <View style= {styles.flexSelf2}/> <View style= {styles.flexSelf3}/> <View style= {styles.flexSelf4}/> </View> ); …… <span class="hljs-keyword">var</span> styles = StyleSheet.create({ flexStyle:{ height:<span class="hljs-number">600</span>,flexDirection: <span class="hljs-string">'row'</span>,alignItems:<span class="hljs-string">'flex-start'</span>,height:<span class="hljs-number">300</span>,flexSelf1:{ flex:<span class="hljs-number">1</span>,backgroundColor:<span class="hljs-string">'#ff0000'</span>,marginRight:<span class="hljs-number">10</span>,flexSelf2:{ flex:<span class="hljs-number">1</span>,backgroundColor:<span class="hljs-string">'#00ff00'</span>,flexSelf3:{ flex:<span class="hljs-number">1</span>,backgroundColor:<span class="hljs-string">'#0000ff'</span>,flexSelf4:{ flex:<span class="hljs-number">1</span>,} }); </code><ul class="pre-numbering" style=""><li>1</li><li>2</li><li>3</li><li>4</li><li>5</li><li>6</li><li>7</li><li>8</li><li>9</li><li>10</li><li>11</li><li>12</li><li>13</li><li>14</li><li>15</li><li>16</li><li>17</li><li>18</li><li>19</li><li>20</li><li>21</li><li>22</li><li>23</li><li>24</li><li>25</li><li>26</li><li>27</li><li>28</li><li>29</li><li>30</li><li>31</li><li>32</li><li>33</li><li>34</li><li>35</li><li>36</li><li>37</li><li>38</li><li>39</li><li>40</li><li>41</li><li>42</li><li>43</li><li>44</li><li>45</li><li>46</li><li>47</li><li>48</li></ul><ul class="pre-numbering" style=""><li>1</li><li>2</li><li>3</li><li>4</li><li>5</li><li>6</li><li>7</li><li>8</li><li>9</li><li>10</li><li>11</li><li>12</li><li>13</li><li>14</li><li>15</li><li>16</li><li>17</li><li>18</li><li>19</li><li>20</li><li>21</li><li>22</li><li>23</li><li>24</li><li>25</li><li>26</li><li>27</li><li>28</li><li>29</li><li>30</li><li>31</li><li>32</li><li>33</li><li>34</li><li>35</li><li>36</li><li>37</li><li>38</li><li>39</li><li>40</li><li>41</li><li>42</li><li>43</li><li>44</li><li>45</li><li>46</li><li>47</li><li>48</li></ul> 看下效果图: justifyContent有竖直就水平。justifyContent和alignItems是相对的。它有五个属性可以设置,分别是flex-start,flex-end,center,space-between,space-around。 先看水平居中(center)的,先看下代码: <code class="hljs cs has-numbering"> <span class="hljs-keyword">return</span>( <View style={styles.flexStyle}> <View style= {styles.flexSub}/> </View> ); …… <span class="hljs-keyword">var</span> styles = StyleSheet.create({ flexStyle:{ height:<span class="hljs-number">600</span>,width:<span class="hljs-number">400</span>,alignItems:<span class="hljs-string">'center'</span>,justifyContent:<span class="hljs-string">'center'</span>,flexSub:{ width:<span class="hljs-number">100</span>,});</code><ul class="pre-numbering" style=""><li>1</li><li>2</li><li>3</li><li>4</li><li>5</li><li>6</li><li>7</li><li>8</li><li>9</li><li>10</li><li>11</li><li>12</li><li>13</li><li>14</li><li>15</li><li>16</li><li>17</li><li>18</li><li>19</li><li>20</li><li>21</li><li>22</li><li>23</li></ul><ul class="pre-numbering" style=""><li>1</li><li>2</li><li>3</li><li>4</li><li>5</li><li>6</li><li>7</li><li>8</li><li>9</li><li>10</li><li>11</li><li>12</li><li>13</li><li>14</li><li>15</li><li>16</li><li>17</li><li>18</li><li>19</li><li>20</li><li>21</li><li>22</li><li>23</li></ul> 父容器设置了justifyContent:’center’属性,所以理论上子组件应该会水平剧中,来看下是否正确。如下: alignSelf该属性用来设置单独组件的竖直对齐方式,与alignItem有点像。有五个属性可以设置,auto,flex-start,flex-end,center,streth。 <code class="hljs cs has-numbering"> <span class="hljs-keyword">return</span>( <View style={styles.flexStyle}> <View style= {styles.flexSelf1}/> <View style= {styles.flexSelf2}/> <View style= {styles.flexSelf3}/> <View style= {styles.flexSelf4}/> </View> ); …… <span class="hljs-keyword">var</span> styles = StyleSheet.create({ flexStyle:{ height:<span class="hljs-number">600</span>,flexDirection:<span class="hljs-string">'row'</span>,alignSelf:<span class="hljs-string">'flex-start'</span>,alignSelf:<span class="hljs-string">'flex-end'</span>,alignSelf:<span class="hljs-string">'stretch'</span>,alignSelf:<span class="hljs-string">'auto'</span>,)}</code><ul class="pre-numbering" style=""><li>1</li><li>2</li><li>3</li><li>4</li><li>5</li><li>6</li><li>7</li><li>8</li><li>9</li><li>10</li><li>11</li><li>12</li><li>13</li><li>14</li><li>15</li><li>16</li><li>17</li><li>18</li><li>19</li><li>20</li><li>21</li><li>22</li><li>23</li><li>24</li><li>25</li><li>26</li><li>27</li><li>28</li><li>29</li><li>30</li><li>31</li><li>32</li><li>33</li><li>34</li><li>35</li><li>36</li><li>37</li><li>38</li><li>39</li><li>40</li><li>41</li><li>42</li><li>43</li><li>44</li><li>45</li><li>46</li></ul><ul class="pre-numbering" style=""><li>1</li><li>2</li><li>3</li><li>4</li><li>5</li><li>6</li><li>7</li><li>8</li><li>9</li><li>10</li><li>11</li><li>12</li><li>13</li><li>14</li><li>15</li><li>16</li><li>17</li><li>18</li><li>19</li><li>20</li><li>21</li><li>22</li><li>23</li><li>24</li><li>25</li><li>26</li><li>27</li><li>28</li><li>29</li><li>30</li><li>31</li><li>32</li><li>33</li><li>34</li><li>35</li><li>36</li><li>37</li><li>38</li><li>39</li><li>40</li><li>41</li><li>42</li><li>43</li><li>44</li><li>45</li><li>46</li></ul> 以上几个子View设置了不同的样式 ,看下效果图: flexflex指设置伸缩项目的伸缩样式,可以把它类比成android中的weight属性。 <code class="hljs cs has-numbering"> <span class="hljs-keyword">return</span>( <View style={styles.flexStyle}> <View style= {styles.flexSelf1}/> <View style= {styles.flexSelf1}/> <View style= {styles.flexSelf2}/> <View style= {styles.flexSelf3}/> </View> ); …… <span class="hljs-keyword">var</span> styles = StyleSheet.create({ flexStyle:{ height:<span class="hljs-number">600</span>,flex:<span class="hljs-number">1</span>,flexSub:{ height:<span class="hljs-number">300</span>,flexSelf2:{ flex:<span class="hljs-number">2</span>,flexSelf3:{ flex:<span class="hljs-number">4</span>,});</code><ul class="pre-numbering" style=""><li>1</li><li>2</li><li>3</li><li>4</li><li>5</li><li>6</li><li>7</li><li>8</li><li>9</li><li>10</li><li>11</li><li>12</li><li>13</li><li>14</li><li>15</li><li>16</li><li>17</li><li>18</li><li>19</li><li>20</li><li>21</li><li>22</li><li>23</li><li>24</li><li>25</li><li>26</li><li>27</li><li>28</li><li>29</li><li>30</li><li>31</li><li>32</li><li>33</li><li>34</li><li>35</li><li>36</li><li>37</li><li>38</li><li>39</li><li>40</li></ul><ul class="pre-numbering" style=""><li>1</li><li>2</li><li>3</li><li>4</li><li>5</li><li>6</li><li>7</li><li>8</li><li>9</li><li>10</li><li>11</li><li>12</li><li>13</li><li>14</li><li>15</li><li>16</li><li>17</li><li>18</li><li>19</li><li>20</li><li>21</li><li>22</li><li>23</li><li>24</li><li>25</li><li>26</li><li>27</li><li>28</li><li>29</li><li>30</li><li>31</li><li>32</li><li>33</li><li>34</li><li>35</li><li>36</li><li>37</li><li>38</li><li>39</li><li>40</li></ul> 效果图如下: flexWrap其实这些属性都是CSS原有的属性,只是RN只支持了部分的属性。flexWrap用于设置是否可换行,有两个属性可设置nowrap和wrap。
(编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |