레이아웃 영역을 잡아주기 위한 flex 개념을 익혀봄
flex
주어진 공간을 얼만큼 채울지 정함
- 숫자를 넣어서 비율을 주는 방식
import React from 'react';
import { View } from 'react-native';
const FlexDemo = () => {
return (
<View style={{ marginTop: 30, height: '100%' }}>
<View style={{ flex: 1, backgroundColor: '#e93e43' }} />
<View style={{ flex: 2, backgroundColor: '#f5a941' }} />
<View style={{ flex: 3, backgroundColor: '#4ebd7a' }} />
</View>
);
};
export default FlexDemo;
[결과]
Flex Direction
flexDirection은 부모가 자식 뷰를 어떤 방향으로 배치할지 정함
- column : 위/아래, 세로 방향(기본값)
- row : 좌/우, 가로 방향
- column-reverse : 아래/위, 역세로 방향
- row-reverse : 우/좌, 역가로 방향
import React from 'react';
import { View, Text } from 'react-native';
const FlexDemo = () => {
return (
<View style={{ flex: 1, flexDirection: 'row', margin: 30 }}>
<View style={{ width: 50, height: 50, backgroundColor: '#e93e43' }}>
<Text>1</Text>
</View>
<View style={{ width: 50, height: 50, backgroundColor: '#f5a941' }}>
<Text>2</Text>
</View>
<View style={{ width: 50, height: 50, backgroundColor: '#4ebd7a' }}>
<Text>3</Text>
</View>
</View>
);
};
export default FlexDemo;
[결과]
Justify Content
자식뷰를 flexDirection 방향으로 어떻게 정렬할 것인지 지정
- flex-start : flexDirection 지정한 시작 지점에 붙여서 정렬
- flex-end : flexDirection 지정한 끝 지점에 붙여서 정렬
- center : flexDirection 지정한 가운데 지점에 붙여서 정렬
- space-between : flexDirection 지정한 방향으로 정렬할 때, 자식뷰 사이에 발생하는 공간을 동일하게 분배함
(양 끝에 공간 없음)
- space-around : flexDirection 지정한 방향으로 정렬할 때, 자식뷰 양 옆으로도 남은 공간을 동일하게 분배
(양 끝의 공간은 가운데 공간 크기의 절반만 가져감)
- space-evenly : flexDirection 지정한 방향으로 정렬할 때, 자식뷰 양 옆 포함해서 모든 빈 공간의 사이즈가 동일함
(양 끝의 공간과 가운데 공간 크기 모두가 동일함)
import React from 'react';
import { View, Text } from 'react-native';
const FlexDemo = () => {
return (
<View
style={{
flex: 1,
flexDirection: 'column',
margin: 30,
justifyContent: 'flex-start',
}}
>
<View style={{ width: 50, height: 50, backgroundColor: '#e93e43' }}>
<Text>1</Text>
</View>
<View style={{ width: 50, height: 50, backgroundColor: '#f5a941' }}>
<Text>2</Text>
</View>
<View style={{ width: 50, height: 50, backgroundColor: '#4ebd7a' }}>
<Text>3</Text>
</View>
</View>
);
};
export default FlexDemo;
[결과]
Align Item
자식뷰를 정렬할 때, 수직 교차 방향으로 어떻게 정렬할 것인지 지정
- stretch : 교차축 방향으로 공간을 채우기 위해 자식뷰를 늘임
(기본값, 조건 : 교차축 방향으로 width나 height가 설정 안되어야 함)
- flex-start : 교차축 시작 지점에서 부터 정렬
- flex-end : 교차축 끝 지점에서 부터 정렬
- center : 교차축 중앙으로 정렬
(flex-start, flex-end, center는 교차축 방향으로 width나 height가 설정 안되어 있으면 컨텐츠 크기만큼 영역만 할당함)
import React from 'react';
import { View, Text } from 'react-native';
const FlexDemo = () => {
return (
<View
style={{
flex: 1,
flexDirection: 'column',
margin: 30,
justifyContent: 'center',
alignItems: 'stretch',
}}
>
<View style={{ width: 50, height: 50, backgroundColor: '#e93e43' }}>
<Text>1</Text>
</View>
<View style={{ height: 50, backgroundColor: '#f5a941' }}>
<Text>2</Text>
</View>
<View style={{ height: 100, backgroundColor: '#4ebd7a' }}>
<Text>3</Text>
</View>
</View>
);
};
export default FlexDemo;
[결과]
- baseline : 자식뷰의 공통 베이스 라인(글자 써지는 기준선)을 기준으로 정
import React from 'react';
import { View, Text } from 'react-native';
const FlexDemo = () => {
return (
<View
style={{
flex: 1,
flexDirection: 'row',
margin: 30,
justifyContent: 'center',
alignItems: 'baseline',
}}
>
<View style={{ width: 50, height: 50, backgroundColor: '#e93e43' }}>
<Text style={{ fontSize: 40 }}>A</Text>
</View>
<View
style={{
width: 50,
height: 50,
backgroundColor: '#f5a941',
justifyContent: 'center',
}}
>
<Text style={{ fontSize: 20 }}>p</Text>
</View>
<View style={{ width: 50, height: 100, backgroundColor: '#4ebd7a' }}>
<Text style={{ fontSize: 40 }}>E</Text>
</View>
</View>
);
};
export default FlexDemo;
[결과]
참고 사이트
https://tech.10000lab.xyz/reactnative/react-native-flex.html
React Native 플렉스박스 레이아웃 - 스타트업 엔지니어링
리액트네이티브에서 UI 래이아웃을 작성할 때 사용하는 플랙스박스(Flexbox)에대해서 간단히 코드를 작성해 보면서 살펴 보도록 하겠습니다. 안드로이드나 아이오에스 앱을 만들 때 UI를 구성하고
tech.10000lab.xyz
https://reactnative.dev/docs/flexbox
Layout with Flexbox · React Native
A component can specify the layout of its children using the Flexbox algorithm. Flexbox is designed to provide a consistent layout on different screen sizes.
reactnative.dev
https://reactnative.dev/docs/layout-props
Layout Props · React Native
More detailed examples about those properties can be found on the Layout with Flexbox page.
reactnative.dev
'React Native' 카테고리의 다른 글
React Native) FlatList 컴포넌트 활용하기 (0) | 2023.07.26 |
---|---|
React Native) Component 활용하기 - 2 (0) | 2023.07.23 |
React Native) Component 활용하기 - 1 (0) | 2023.07.21 |
React Native 컴포넌트 개념 잡기 - 2 (0) | 2023.07.19 |
React Native 컴포넌트 개념 잡기 - 1 (0) | 2023.07.19 |