react-native 之布局总结

nlts2569 7年前
   <h2>前言</h2>    <p>之前我们讲了很多react-native的基础控件,为了方便大家的理解,我们来对react-native的布局做一个总结,观看本节知识,你将看到。</p>    <ul>     <li>宽度单位和像素密度</li>     <li>flex的布局</li>     <li>图片布局</li>     <li>绝对定位和相对定位</li>     <li>padding和margin的区别和应用场合</li>     <li>文本元素</li>    </ul>    <h2>宽度单位和像素密度</h2>    <p>我们知道在Android中是用设备像素来作为单位的(后面又出现了百分比这么 一个概念),ios中后面也有了Auto Layout和1倍图,二倍图等概念(xib+storyboard)。然而react的宽度不支持百分比,那么React怎么提供尺寸的呢? <a href="/misc/goto?guid=4959735275134926173" rel="nofollow,noindex">PixelRatio</a> ,PixelRatio及像素密度,可以看看官方的介绍。</p>    <pre>  <code class="language-javascript"> var image = getImage({     width: 200 * PixelRatio.get(),     height: 100 * PixelRatio.get()   });   <Image source={image} style={{width: 200, height: 100}} /></code></pre>    <h2>flex的布局</h2>    <p>我们知道一个div如果不设置宽度,默认的会占用100%的宽度, 为了验证100%这个问题, 做三个实验:</p>    <ol>     <li>根节点上方一个View, 不设置宽度</li>     <li>固定宽度的元素上设置一个View, 不设置宽度</li>     <li>flex的元素上放一个View宽度, 不设置宽度</li>    </ol>    <pre>  <code class="language-javascript"> <Text style={[styles.text, styles.header]}>        根节点上放一个元素,不设置宽度    </Text>              <View style={{height: 20, backgroundColor: '#333333'}} />      <Text style={[styles.text, styles.header]}>        固定宽度的元素上放一个View,不设置宽度    </Text>       <View style={{width: 100}}>      <View style={{height: 20, backgroundColor: '#333333'}} />    </View>      <Text style={[styles.text, styles.header]}>        flex的元素上放一个View,不设置宽度    </Text>       <View style={{flexDirection: 'row'}}>      <View style={{flex: 1}}>        <View style={{height: 20, backgroundColor: '#333333'}} />      </View>      <View style={{flex: 1}}/>    </View></code></pre>    <p>来看一下运行的结果:</p>    <p style="text-align:center"><img src="https://simg.open-open.com/show/1889c933b92838a883713b0581117e14.png"></p>    <h2>水平垂直居中</h2>    <p>css 里边经常会将一个文本或者图片水平垂直居中,如果使用过css 的flexbox当然知道使用alignItems 和 justifyContent ,那如果用React Native如何实现呢?</p>    <pre>  <code class="language-javascript"><Text style={[styles.text, styles.header]}>          水平居中      </Text>        <View style={{height: 100, backgroundColor: '#333333', alignItems: 'center'}}>        <View style={{backgroundColor: '#fefefe', width: 30, height: 30, borderRadius: 15}}/>      </View>         <Text style={[styles.text, styles.header]}>          垂直居中      </Text>      <View style={{height: 100, backgroundColor: '#333333', justifyContent: 'center'}}>        <View style={{backgroundColor: '#fefefe', width: 30, height: 30, borderRadius: 15}}/>      </View>        <Text style={[styles.text, styles.header]}>          水平垂直居中      </Text>      <View style={{height: 100, backgroundColor: '#333333', alignItems: 'center', justifyContent: 'center'}}>        <View style={{backgroundColor: '#fefefe', width: 30, height: 30, borderRadius: 15}}/>      </View></code></pre>    <p style="text-align:center"><img src="https://simg.open-open.com/show/31373946512a4aa85e8416daa20bca01.png"></p>    <h2>网格布局</h2>    <p>通常页面不是很复杂的时候,我们可以使用flex布局等分做到网格,复杂的那么就要用ListView实现,或者第三方控件。</p>    <h3>等分的网格</h3>    <pre>  <code class="language-javascript"><View style={styles.flexContainer}>        <View style={styles.cell}>          <Text style={styles.welcome}>            cell1          </Text>        </View>        <View style={styles.cell}>          <Text style={styles.welcome}>            cell2          </Text>        </View>        <View style={styles.cell}>          <Text style={styles.welcome}>            cell3          </Text>        </View>      </View>        styles = {          flexContainer: {              // 容器需要添加direction才能变成让子元素flex              flexDirection: 'row'          },          cell: {              flex: 1,              height: 50,              backgroundColor: '#aaaaaa'          },          welcome: {              fontSize: 20,              textAlign: 'center',              margin: 10          },      }</code></pre>    <p style="text-align:center"><img src="https://simg.open-open.com/show/a95d8e8df996ee3d1c5b0ed3f3bc45fd.png"> </p>    <h2>图片布局</h2>    <pre>  <code class="language-javascript"><Text style={styles.welcome}> 100px height </Text>    <Image style={{height: 100}} source={{uri: 'http://gtms03.alicdn.com/tps/i3/TB1Kcs5GXXXXXbMXVXXutsrNFXX-608-370.png'}} /></code></pre>    <p>100px 高度, 可以看到图片适应100高度和全屏宽度,背景居中适应未拉伸但是被截断也就是cover。</p>    <pre>  <code class="language-javascript">  <Text style={styles.welcome}> 100px height with resizeMode contain </Text>    <View style={[{flex: 1, backgroundColor: '#fe0000'}]}>        <Image style={{flex: 1, height: 100, resizeMode: Image.resizeMode.contain}} source={{uri: 'http://gtms03.alicdn.com/tps/i3/TB1Kcs5GXXXXXbMXVXXutsrNFXX-608-370.png'}} />    </View></code></pre>    <p>contain 模式容器完全容纳图片,图片自适应宽高。</p>    <pre>  <code class="language-javascript">  <Text style={styles.welcome}> 100px height with resizeMode cover </Text>    <View style={[{flex: 1, backgroundColor: '#fe0000'}]}>        <Image style={{flex: 1, height: 100, resizeMode: Image.resizeMode.cover}} source={{uri: 'http://gtms03.alicdn.com/tps/i3/TB1Kcs5GXXXXXbMXVXXutsrNFXX-608-370.png'}} />    </View></code></pre>    <p>stretch模式图片被拉伸适应屏幕</p>    <pre>  <code class="language-javascript"> <Text style={styles.welcome}> set height to image container </Text>    <View style={[{flex: 1, backgroundColor: '#fe0000', height: 100}]}>        <Image style={{flex: 1}} source={{uri: 'http://gtms03.alicdn.com/tps/i3/TB1Kcs5GXXXXXbMXVXXutsrNFXX-608-370.png'}} />    </View></code></pre>    <h2>绝对定位和相对定位</h2>    <pre>  <code class="language-javascript"><View style={{flex: 1, height: 100, backgroundColor: '#333333'}}>      <View style={[styles.circle, {position: 'absolute', top: 50, left: 180}]}>      </View>    </View>    styles = {      circle: {      backgroundColor: '#fe0000',      borderRadius: 10,      width: 20,      height: 20      }    }</code></pre>    <p style="text-align:center"><img src="https://simg.open-open.com/show/8e372cce89ded8bc5533bb5969e22ce6.png"></p>    <p>和css的标准不同的是, 元素容器不用设置position:’absolute|relative’ 。</p>    <pre>  <code class="language-javascript"><View style={{flex: 1, height: 100, backgroundColor: '#333333'}}>      <View style={[styles.circle, {position: 'relative', top: 50, left: 50, marginLeft: 50}]}>      </View>    </View></code></pre>    <h2>padding和margin</h2>    <p>我们知道在css中区分inline元素和block元素,既然react-native实现了一个超级小的css subset。那我们就来实验一下padding和margin在inline和非inline元素上的padding和margin的使用情况。</p>    <p>padding</p>    <pre>  <code class="language-javascript"> <Text style={[styles.text, styles.header]}>      在正常的View上设置padding     </Text>      <View style={{padding: 30, backgroundColor: '#333333'}}>      <Text style={[styles.text, {color: '#fefefe'}]}> Text Element</Text>    </View>      <Text style={[styles.text, styles.header]}>      在文本元素上设置padding    </Text>    <View style={{padding: 0, backgroundColor: '#333333'}}>      <Text style={[styles.text, {backgroundColor: '#fe0000', padding: 30}]}>        text 元素上设置paddinga      </Text>    </View></code></pre>    <p style="text-align:center"><img src="https://simg.open-open.com/show/69fbb9ab62fd0eb45f64c26fb364c4c1.png"></p>    <p>margin</p>    <pre>  <code class="language-javascript"> <Text style={[styles.text, styles.header]}>      在正常的View上设置margin     </Text>      <View style={{backgroundColor: '#333333'}}>      <View style={{backgroundColor: '#fefefe', width: 30, height: 30, margin: 30}}/>    </View>      <Text style={[styles.text, styles.header]}>      在文本元素上设置margin    </Text>    <View style={{backgroundColor: '#333333'}}>      <Text style={[styles.text, {backgroundColor: '#fe0000', margin: 30}]}>        text 元素上设置margin      </Text>      <Text style={[styles.text, {backgroundColor: '#fe0000', margin: 30}]}>        text 元素上设置margin      </Text>    </View></code></pre>    <p style="text-align:center"><img src="https://simg.open-open.com/show/e5c3deec32559ffb3d5f28133a70d28b.png"></p>    <h2>文本元素</h2>    <p>先看看文字有哪些支持的style属性:</p>    <pre>  <code class="language-javascript">Attributes.style = {      color string      containerBackgroundColor string      fontFamily string      fontSize number      fontStyle enum('normal', 'italic')      fontWeight enum("normal", 'bold', '100', '200', '300', '400', '500', '600', '700', '800', '900')      lineHeight number      textAlign enum("auto", 'left', 'right', 'center')      writingDirection enum("auto", 'ltr', 'rtl')    }</code></pre>    <h2>Text的样式继承</h2>    <p>实际上React-native里边是没有样式继承这种说法的, 但是对于Text元素里边的Text元素是可以继承的。到底是继承的最外层的Text的值呢,还是继承父亲Text的值呢?肯定是继承父亲Text的值。</p>    <pre>  <code class="language-javascript"> <Text style={[styles.text, styles.header]}>        文本样式继承    </Text>      <View style={{backgroundColor: '#333333', padding: 10}}>      <Text style={{color: 'white'}}>        <Text style={{color: 'red'}} onPress={this.onPressTitle}>           文本元素{'\n'}          <Text>我是white还是red呢?{'\n'} </Text>        </Text>        <Text>我应该是white的</Text>      </Text>    </View></code></pre>    <p style="text-align:center"><img src="https://simg.open-open.com/show/758aa1e0d6cd1cbf12c68771514fe908.png"></p>    <h2>总结</h2>    <p>针对上面的实例,我们做一个总结。</p>    <ul>     <li>react 宽度基于pt为单位, 可以通过Dimensions 来获取宽高,PixelRatio 获取密度。</li>     <li>基于flex的布局:<br> view默认宽度为100%<br> 水平居中用alignItems, 垂直居中用justifyContent<br> 基于flex能够实现现有的网格系统需求,且网格能够各种嵌套无bug</li>     <li>图片布局<br> 通过Image.resizeMode来适配图片布局,包括contain, cover, stretch 三种模式<br> 默认不设置模式等于cover模式<br> contain模式自适应宽高,给出高度值即可<br> cover铺满容器,但是会做截取<br> stretch铺满容器,拉伸</li>     <li> <p>绝对定位和相对定位</p> <p>定位相对于父元素,父元素不用设置position也行</p> <p>padding 设置在Text元素上的时候会存在bug。所有padding变成了marginBottom</p> </li>     <li> <p>文本元素</p> <p>文字必须放在Text元素里边</p> <p>Text元素可以相互嵌套,且存在样式继承关系</p> <p>numberOfLines 需要放在最外层的Text元素上,且虽然截取了文字但是还是会占用空间</p> </li>    </ul>    <p> </p>    <p>来自:http://blog.csdn.net/xiangzhihong8/article/details/54588342</p>    <p> </p>