又是一週過去了,彷佛上週我只更新了一篇文章,最近工做實在太忙,望你們見諒。今天要講的仍是佈局相關的,以前已經對佈局中主要屬性作了介紹,此次,會對佈局中其餘屬性進行介紹。android
alignSelf是指相對於父容器,自身的位置,有auto
,flex-start
,flex-end
,center
,stretch
五種屬性,對應效果以下:ios
用來表示寬高比,React Native獨有。是一個很好的屬性,例如知道了圖片展現的比例,寬度是屏幕寬度,這樣就不須要計算寬度再計算高度,只須要設置一下就好了。bash
movieImage:{
width: 100,
aspectRatio: 0.758,
},複製代碼
背景顏色這個很少說了吧佈局
與border相關的屬性有:flex
borderWidth
這裏要說明,若是隻設置某個邊界的寬度和顏色,不能設置圓角,不然沒法顯示,圓角只能使用borderWidth
來設置,若是想設置單邊的,能夠參考以下:ui
border: {
width:100,
height:100,
backgroundColor: '#fff',
borderBottomColor:'#f57f17',
borderBottomWidth:12
},複製代碼
效果以下:spa
border: {
width:100,
height:100,
backgroundColor: '#fff',
borderBottomColor:'#f57f17',
borderBottomLeftRadius:10,
borderBottomRightRadius:20,
borderRightColor:'#b9f6ca',
borderTopLeftRadius:10,
borderTopWidth:6,
borderTopColor:'#1e88e5',
borderTopRightRadius:20,
borderWidth:2,
borderColor:'#c2185b'
},複製代碼
效果以下:3d
與margin相關的屬性有:netty
border: {
width:100,
height:100,
backgroundColor: '#fff',
borderBottomColor:'#f57f17',
borderBottomLeftRadius:10,
borderBottomRightRadius:20,
borderRightColor:'#b9f6ca',
borderTopLeftRadius:10,
borderTopWidth:6,
borderTopColor:'#1e88e5',
borderTopRightRadius:20,
borderWidth:2,
borderColor:'#c2185b',
marginLeft:20,
marginRight:20,
marginTop:20,
},複製代碼
效果以下:與之相關的屬性主要有translate,scale,以及rotate這裏直接
首先須要介紹的是translate,這個很好理解,就是沿XYZ移動的距離
X軸正方向:手機右側爲正,向左爲負;
Y軸正方向:手機下方爲正,上方爲負;
Z軸正方向:從手機背面向屏幕指向爲負,反之從屏幕向背面指向爲正,看個例子,跟上面的示意圖能夠作個對比:code
border: {
width:100,
height:100,
backgroundColor: '#fff',
borderBottomColor:'#f57f17',
borderBottomLeftRadius:10,
borderBottomRightRadius:20,
borderRightColor:'#b9f6ca',
borderTopLeftRadius:10,
borderTopWidth:6,
borderTopColor:'#1e88e5',
borderTopRightRadius:20,
borderWidth:2,
borderColor:'#c2185b',
transform:[{translate:[40,140,40]}]
},複製代碼
效果以下:
border: {
width:100,
height:100,
backgroundColor: '#fff',
borderBottomColor:'#f57f17',
borderBottomLeftRadius:10,
borderBottomRightRadius:20,
borderRightColor:'#b9f6ca',
borderTopLeftRadius:10,
borderTopWidth:6,
borderTopColor:'#1e88e5',
borderTopRightRadius:20,
borderWidth:2,
borderColor:'#c2185b',
transform:[{translate:[40,140,40]},{scaleX:2},{rotateX:'45deg'}]
},複製代碼
這裏是指繞x軸旋轉45度,記住,旋轉度數必定要加deg
,而後沿X軸放大兩倍
效果以下:
ios的方法
border: {
width:100,
height:100,
backgroundColor: '#fff',
borderBottomColor:'#f57f17',
borderBottomLeftRadius:10,
borderBottomRightRadius:20,
borderRightColor:'#b9f6ca',
borderTopLeftRadius:10,
borderTopWidth:6,
borderTopColor:'#1e88e5',
borderTopRightRadius:20,
borderWidth:2,
borderColor:'#c2185b',
shadowOffset: {width: 0, height: 0},
marginLeft:50,
marginTop:50,
elevation: 20,
shadowOffset: {width: 0, height: 0},
shadowColor: 'black',
shadowOpacity: 1,
shadowRadius: 5
},複製代碼
效果以下:文字相對於Textview的佈局位置:auto
left
right
center
更改一下佈局方式,如:
myTextColor1: {
alignSelf:'stretch',
textAlign:'auto',
color: '#ffffff',
backgroundColor: '#f57f17',
},複製代碼
同理其它分別對應左中右。效果以下:
有了上面的描述textAlignVertical不難理解,就是垂直方向文字相對於Textview的佈局位置:auto
bottom
top
center
爲了看到垂直位置,咱們須要固定一下Textview的高度:
myTextColor1: {
height:70,
marginBottom:10,
alignSelf:'stretch',
textAlign:'auto',
textAlignVertical:'auto',
color: '#ffffff',
backgroundColor: '#f57f17',
},複製代碼
效果以下:
在介紹position以前,咱們先看一下margin的例子:
render() {
return (
<View style = {styles.container}>
<View style = {styles.view1}/>
<View style = {styles.view2}/>
</View>
);
}複製代碼
container: {
flex:1,
backgroundColor: '#fff',
},
view1:{
width:100,
height:100,
backgroundColor: '#000',
},
view2:{
width:100,
height:100,
marginTop:10,
backgroundColor: '#000',
},複製代碼
效果以下所示:
position
了,放了看着方便,下面的佈局我會將兩個view的背景顏色換一下。
view1:{
width:100,
height:100,
position:'absolute',
backgroundColor: '#000',
top:0,
left:20,
},
view2:{
width:100,
height:100,
position:'absolute',
backgroundColor: '#f57f17',
top:30,
left:20,
},複製代碼
使用了position:'absolute',
絕對佈局。這個是相對於父容器進行絕對佈局。也就是全部設置的位置都是相對於父容器的而不是兄弟節點,也不用去考慮兄弟節點的位置,效果以下:
view1:{
width:100,
height:100,
position:'relative',
backgroundColor: '#000',
top:0,
left:20,
},
view2:{
width:100,
height:100,
position:'relative',
backgroundColor: '#f57f17',
top:-30,
left:-20,
},複製代碼
效果以下圖所示:
此次關於佈局的這篇文章,幾乎涵蓋了全部的佈局方法,對於初學者來講,看懂這篇文章,用的例子,本身去試一下,改一下,差很少就能搞定全部佈局了,固然還要加上我以前的那篇關於容器佈局的方法。
以前文章合集:
Android文章合集
iOS文章合集
ReactNative文章合集
若是你也作ReactNative開發,並對ReactNative感興趣,歡迎關注個人公衆號,加入咱們的討論羣: