Flexboxで横方向のレイアウトとflex比率を学びます
React Native - 第1章: React Native基礎 - コンポーネントとスタイリング
flexDirection: 'row'を使うと、要素を横に並べられます。 また、flexプロパティで幅の比率を指定できます。
flexDirection: 'row'を設定flex: 1、Box2はflex: 2flexDirection: 'row'で横並びにします:
const styles = StyleSheet.create({
container: {
flex: 1,
flexDirection: 'row', // 横方向
justifyContent: 'center',
alignItems: 'center',
},
});flexプロパティで幅の比率を指定します:
box1: {
flex: 1, // 比率1
height: 100,
},
box2: {
flex: 2, // 比率2(box1の2倍の幅)
height: 150,
},
box3: {
flex: 1, // 比率1
height: 100,
}この場合、画面幅を4等分して、Box1が1/4、Box2が2/4(半分)、Box3が1/4の幅になります。
ポイント:flexDirection: 'row'で横並びに、flexで幅の比率を調整します。
┌────┬──────────┬────┐ │ │ │ │ │Box1│ Box2 │Box3│ │ │ │ │ └────┴──────────┴────┘ 1 2 1