React Native 布局浅探
简述在Web开发中,页面布局基于盒子模型,主要通过定位属性、浮动属性和显示属性实现。而React Native采用的是Flex布局,但也支持盒子模型的margin、padding以及定位属性进行布局。 flex布局定义以一张图片来大致了解flex布局的思想 flex布局实例对于flex各种布局方式的实现可以参见这篇文章 移动端全兼容的flexbox速成班 这是移动端web flex布局,可以套用思路,只需要将属性改成RN的形式就可以了。
RN中的flex布局RN flex属性1)用在flex容器上的属性 2)用在flex子项上的属性 Web flex和RN flex几点区别:1)因为RN里的所有组件都是以flex作为其显示属性,所以不需要再有 布局中一些注意点1)文本必须写在Text中而非View RN中的长度单位默认单位在RN中所有的尺寸属性都是不带单位的。官网给出的默认单位是dp。 console.log('width:'+Dimensions.get('window').width+',height:'+Dimensions.get('window').height); 得到如下图结果 来看看不同iphone下的一些尺寸数值
不难发现我们这里获得的宽高正与逻辑分辨率pt对应。所以,对于常规的设计图,pt和px的转换规律是 取得屏幕大小和设备像素比1) 屏幕大小 const {devWidth,devHeight} = Dimensions.get('window'); 2) dpr PixelRatio.get() RN中组件占位问题组件默认占位大小在Web布局中,区块以div包裹,文本以span包裹。默认情况下div的宽度为100%屏幕宽度,高为0。span宽高均为0。那么RN中与其用途相似的View和Text组件默认宽高是怎么定义的呢? 做个实验: <View style={{paddingTop: 18,}}> <Text style={styles.bgcolor_1}>A testing text</Text> <Text style={[styles.bgcolor_2,{width: 200,}]}>A testing text with width was setted</Text> <View style={styles.bgcolor_3}></View> </View>
② Text作为包裹容器时 <View style={{paddingTop: 18,}}> <Text> <Text style={styles.bgcolor_1}>A testing text</Text> <Text style={[styles.bgcolor_2,}]}>A testing text with width was setted</Text> </Text> //...代码同上 </View>
2)主轴方向水平 <View style={{paddingTop: 18,flexDirection: 'row'}}> //...代码同上 </View> ① Text不作为包裹容器时
② Text作为包裹容器时
结论: 参考官方文档的定义
文本布局一些属性的占位1) numberOfLines占位问题 <Text numberOfLines={5} style={styles.textWrapper} > <Text style={{fontSize: 20}}>Title</Text>{'n'} <Text>In this example,the nested title and body text will inherit the fontFamily from styles.baseText,but the title provides its own additional styles. The title and body will stack on top of each other on account of the literal newlines,numberOfLines is Used to truncate the text with an elipsis after computing the text layout,including line wrapping,such that the total number of lines does not exceed this number.</Text> </Text>
2) lineHeight的使用方式 <Text numberOfLines={5} style={styles.textWrapper} > <Text style={{fontSize: 20,lineHeight: 50,backgroundColor: 'aliceblue'}}>Title</Text>{'n'} <Text>In this example,such that the total number of lines does not exceed this number.</Text> </Text>
我们看到另一诡异的问题是它的兄弟Text也被设定了同样的lineHeight值。 <View style={{height: 50,justifyContent: 'center',backgroundColor: 'aliceblue'}}> <Text style={{fontSize: 20}}>Title</Text> </View>
图片布局1) 默认尺寸
我们知道Flex布局中,子组件的宽高值受到父组件的约束。 <View style={{paddingTop: 18}}> <Text style={{paddingTop: 5,paddingBottom: 5,backgroundColor: '#a5e0da'}}>React Native</Text> <View style={{height: 200}}> <Image source={require('./image/react.png')} /> </View> </View>
加载动态资源时
因此无论是静态还是动态图,最好都给它设定一个宽高值。对于动态图,若只设定其中一个值,还可以通过 看一段代码,分别修改Image的resizeMode属性 <View> <Image style={{height: 100}} resizeMode={'cover'} source={{uri:"http://static.open-open.com/lib/uploadImg/20160412/20160412193341_428.png"}} /> </View>
RN中的盒模型布局对于margin、padding的使用同Web上的使用方式,但不存在margin塌陷的情况。嵌套的Text不能设置垂直方向上的margin。 绝对定位RN中元素默认的定位方式是
(编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |