
RN的布局采用的是Flex,彈性盒模型。
有四個(gè)主要屬性:flex、flexDirection、justifyContent、alignItems。
將對(duì)依次講解。
目錄
- flex的使用。
- flexDirection的取值和使用。
- justifyContent的取值和使用。
- alignItems的取值和使用。
- 他們的結(jié)合使用。
1. flex的使用
我們可以經(jīng)??吹皆诖a中使用 flex:1 ,那么這是什么意思呢?
可以理解為比重
· 如果同級(jí)組件上只有一個(gè),并且設(shè)置了 flex:1,那么這個(gè)組件相當(dāng)于分配了全部空間。
· 如果同級(jí)組件上只有兩個(gè),并且這兩個(gè)都設(shè)置了 flex:1,那么相當(dāng)于這兩個(gè)組件平均分配了全部空間。
· 如果同級(jí)組件上只有兩個(gè),并且第一個(gè)組件設(shè)置了 flex:1,第二個(gè)組件設(shè)置了 flex:2,那么相當(dāng)于第一個(gè)組件占據(jù)全部空間的三分之一,第二個(gè)組件占據(jù)全部空間的三分之二。
· 如果沒有設(shè)置 flex 屬性,那么這個(gè)組件按需分配空間。
以此類推。
下面給出三個(gè)例子:
1.最外層同級(jí)只有一個(gè)組件,并且設(shè)置了flex:1,那么它(粉色)占據(jù)全部空間。
export default class demo extends Component {
render() {
return (
<View style={{flex : 1, backgroundColor:"pink"}}>
</View>
);
}
}

2.最外層同級(jí)只有一個(gè)組件,并且設(shè)置了flex:1,width設(shè)置了一個(gè)固定寬度,那么它(粉色)的高度占據(jù)全部空間。
(但如果height設(shè)置一個(gè)固定高度的話,則height失效不起效果,因?yàn)榻M件是豎向排列,此時(shí)flex的優(yōu)先級(jí)大于height。)
export default class demo extends Component {
render() {
return (
<View style={{width:60,flex:1,backgroundColor:"pink"}}>
</View>
);
}
}

3.最外層同級(jí)只有兩個(gè)組件,并且width設(shè)置了一個(gè)固定寬度,第一個(gè)組件設(shè)置了flex:2,第二個(gè)組件設(shè)置了flex:1,那么
第一個(gè)組件(粉色)的高度占據(jù)全部空間三分之二。
第二個(gè)組件(藍(lán)色)的高度占據(jù)全部空間三分之一。
export default class demo extends Component {
render() {
return (
<View style={{width:60,flex:1}}>
//第一個(gè)組件
<View style={{flex:2,backgroundColor:"pink"}}/>
//第二個(gè)組件
<View style={{flex:1,backgroundColor:"blue"}}/>
</View>
);
}
}

2. flexDirection 的取值和使用
取值:
column 豎向排列
column-reverse 豎向、反向排列
row 橫向排列
row-reverse 橫向、反向排列
(不設(shè)置flexDirection時(shí),默認(rèn)是column)
當(dāng)flexDirection = column
export default class demo extends Component {
render() {
return (
<View style={{flexDirection:'column'}}>
<View style={{width:50,height:50,backgroundColor:"red"}}/>
<View style={{width:50,height:50,backgroundColor:"black"}}/>
<View style={{width:50,height:50,backgroundColor:"green"}}/>
</View>
);
}
}

當(dāng)flexDirection = column-reverse
export default class demo extends Component {
render() {
return (
<View style={{flexDirection:'column-reverse'}}>
<View style={{width:50,height:50,backgroundColor:"red"}}/>
<View style={{width:50,height:50,backgroundColor:"black"}}/>
<View style={{width:50,height:50,backgroundColor:"green"}}/>
</View>
);
}
}

當(dāng)flexDirection = row
export default class demo extends Component {
render() {
return (
<View style={{flexDirection:'row'}}>
<View style={{width:50,height:50,backgroundColor:"red"}}/>
<View style={{width:50,height:50,backgroundColor:"black"}}/>
<View style={{width:50,height:50,backgroundColor:"green"}}/>
</View>
);
}
}

當(dāng)flexDirection = row-reverse
export default class demo extends Component {
render() {
return (
<View style={{flexDirection:'row-reverse'}}>
<View style={{width:50,height:50,backgroundColor:"red"}}/>
<View style={{width:50,height:50,backgroundColor:"black"}}/>
<View style={{width:50,height:50,backgroundColor:"green"}}/>
</View>
);
}
}

3. justifyContent 的取值和使用
決定其子元素沿著主軸的排列方式
假如是垂直排列(由flexDirection決定),那么:
取值:
flex-start 集中在最上方
center 整體豎向居中
flex-end 集中在最下方
space-around 均勻分布
space-between 均勻鋪滿
(不設(shè)置justifyContent時(shí),默認(rèn)是flex-start)
注意外層容器要加上flex:1,讓組件有足夠的空間。
當(dāng)justifyContent = flex-start
export default class demo extends Component {
render() {
return (
<View style={{flex:1,justifyContent:'flex-start',backgroundColor:"pink"}}>
<View style={{width:50,height:50,backgroundColor:"red"}}/>
<View style={{width:50,height:50,backgroundColor:"black"}}/>
<View style={{width:50,height:50,backgroundColor:"green"}}/>
</View>
);
}
}

當(dāng)justifyContent = center
export default class demo extends Component {
render() {
return (
<View style={{flex:1,justifyContent:'center',backgroundColor:"pink"}}>
<View style={{width:50,height:50,backgroundColor:"red"}}/>
<View style={{width:50,height:50,backgroundColor:"black"}}/>
<View style={{width:50,height:50,backgroundColor:"green"}}/>
</View>
);
}
}

當(dāng)justifyContent = flex-end
export default class demo extends Component {
render() {
return (
<View style={{flex:1,justifyContent:'flex-end',backgroundColor:"pink"}}>
<View style={{width:50,height:50,backgroundColor:"red"}}/>
<View style={{width:50,height:50,backgroundColor:"black"}}/>
<View style={{width:50,height:50,backgroundColor:"green"}}/>
</View>
);
}
}

當(dāng)justifyContent = space-around
export default class demo extends Component {
render() {
return (
<View style={{flex:1,justifyContent:'space-around',backgroundColor:"pink"}}>
<View style={{width:50,height:50,backgroundColor:"red"}}/>
<View style={{width:50,height:50,backgroundColor:"black"}}/>
<View style={{width:50,height:50,backgroundColor:"green"}}/>
</View>
);
}
}

當(dāng)justifyContent = space-between
export default class demo extends Component {
render() {
return (
<View style={{flex:1,justifyContent:'space-between',backgroundColor:"pink"}}>
<View style={{width:50,height:50,backgroundColor:"red"}}/>
<View style={{width:50,height:50,backgroundColor:"black"}}/>
<View style={{width:50,height:50,backgroundColor:"green"}}/>
</View>
);
}
}

4. alignItems的取值和使用
決定其子元素沿著次軸的排列方式,次軸與主軸垂直,意味著當(dāng)flexDirection為column時(shí),主軸是豎向,次軸是橫向,flexDirection為row時(shí),主軸是橫向,次軸是豎向。
假如是豎向排列(由flexDirection決定),那么:
取值:
- flex-start 集中在最左邊
- center 整體橫向居中
- flex-end 集中在最右邊
- stretch 水平撐滿,子組件不能設(shè)置固定的width。
(不設(shè)置alignItems時(shí),默認(rèn)是flex-start)
注意外層容器要加上flex:1,讓組件有足夠的空間。
當(dāng)alignItems = flex-start
export default class demo extends Component {
render() {
return (
<View style={{flex:1,alignItems:'flex-start',backgroundColor:"pink"}}>
<View style={{width:50,height:50,backgroundColor:"red"}}/>
<View style={{width:50,height:50,backgroundColor:"black"}}/>
<View style={{width:50,height:50,backgroundColor:"green"}}/>
</View>
);
}
}

當(dāng)alignItems = center
export default class demo extends Component {
render() {
return (
<View style={{flex:1,alignItems:'center',backgroundColor:"pink"}}>
<View style={{width:50,height:50,backgroundColor:"red"}}/>
<View style={{width:50,height:50,backgroundColor:"black"}}/>
<View style={{width:50,height:50,backgroundColor:"green"}}/>
</View>
);
}
}

當(dāng)alignItems = flex-end
export default class demo extends Component {
render() {
return (
<View style={{flex:1,alignItems:'flex-end',backgroundColor:"pink"}}>
<View style={{width:50,height:50,backgroundColor:"red"}}/>
<View style={{width:50,height:50,backgroundColor:"black"}}/>
<View style={{width:50,height:50,backgroundColor:"green"}}/>
</View>
);
}
}

當(dāng)alignItems = stretch
export default class demo extends Component {
render() {
return (
<View style={{flex:1,alignItems:'stretch',backgroundColor:"pink"}}>
<View style={{height:50,backgroundColor:"red"}}/>
<View style={{height:50,backgroundColor:"black"}}/>
<View style={{height:50,backgroundColor:"green"}}/>
</View>
);
}
}

5. 他們的結(jié)合使用。
看過上面的講解后,其實(shí)知道單個(gè)是什么效果,兩個(gè)屬性一起用也就不難理解了。
下面給出一個(gè)。
flex 設(shè)置為 1 讓組件擁有全個(gè)屏幕的空間(粉色區(qū)域)
flexDirection 設(shè)置為 row-reverse 組件橫向、反向排列
justifyContent 設(shè)置為 space-around 均勻分布
alignItems 設(shè)置為 center 水平居中
這樣的話,效果就是。
export default class demo extends Component {
render() {
return (
<View style={{flex:1,
flexDirection:"row-reverse",
justifyContent:'space-around',
alignItems:'center',
backgroundColor:"pink"}}>
<View style={{width:50,height:50,backgroundColor:"red"}}/>
<View style={{width:50,height:50,backgroundColor:"black"}}/>
<View style={{width:50,height:50,backgroundColor:"green"}}/>
</View>
);
}
}

以上,完畢。