프로그래머로의 여정
LinearLayout vs RelativeLayout in Android Studio
by nunaaa
2025. 10. 20.
LinearLayout vs RelativeLayout 차이
핵심 개념: 선형 배치 vs 상대적 배치
| 구분 |
LinearLayout |
RelativeLayout |
| 배치 방식 |
일렬 정렬 (직선적) |
상대적 위치 (자유로운) |
| 개념 |
책을 책꽂이에 차례로 꽂는 방식 |
가구를 방 안에 자유롭게 배치하는 방식 |
| Android 버전 |
API 1 (최초부터) |
API 1 (최초부터) |
상세 비교
1. 배치 원리
LinearLayout:
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button android:text="Button 1" android:id="@+id/btn1"/>
<Button android:text="Button 2" android:id="@+id/btn2"/>
<Button android:text="Button 3" android:id="@+id/btn3"/>
</LinearLayout>
결과:
┌─────────────────┐
│ Button 1 │
│ Button 2 │
│ Button 3 │
└─────────────────┘
RelativeLayout:
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:id="@+id/btn1"
android:text="Button 1"
android:layout_centerInParent="true"/>
<Button
android:id="@+id/btn2"
android:text="Button 2"
android:layout_below="@id/btn1"
android:layout_alignParentRight="true"/>
</RelativeLayout>
2. 주요 속성 비교
LinearLayout 주요 속성:
| 속성 |
설명 |
android:orientation |
vertical(세로) 또는 horizontal(가로) |
android:gravity |
자식 뷰들의 내부 정렬 |
android:layout_weight |
가중치에 따른 공간 분배 |
RelativeLayout 주요 속성:
| 속성 |
설명 |
android:layout_above |
특정 뷰의 위쪽에 배치 |
android:layout_below |
특정 뷰의 아래쪽에 배치 |
android:layout_toLeftOf |
특정 뷰의 왼쪽에 배치 |
android:layout_alignParentTop |
부모의 위쪽에 정렬 |
android:layout_centerInParent |
부모의 가운데에 배치 |
실제 사용 예제 비교
동일한 UI 구현하기
목표:
┌─────────────────────────────────┐
│ [이미지] [제목] [시간] │
│ [부제목] │
└─────────────────────────────────┘
1. LinearLayout 구현 (중첩 필요)
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageView
android:id="@+id/icon"
android:layout_width="48dp"
android:layout_height="48dp"/>
<LinearLayout
android:orientation="vertical"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1">
<TextView
android:id="@+id/title"
android:text="메인 제목"/>
<TextView
android:id="@+id/subtitle"
android:text="부제목"/>
</LinearLayout>
<TextView
android:id="@+id/time"
android:text="10:30"/>
</LinearLayout>
2. RelativeLayout 구현 (단일 계층)
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageView
android:id="@+id/icon"
android:layout_width="48dp"
android:layout_height="48dp"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"/>
<TextView
android:id="@+id/title"
android:layout_toRightOf="@id/icon"
android:layout_alignParentTop="true"
android:text="메인 제목"/>
<TextView
android:id="@+id/subtitle"
android:layout_toRightOf="@id/icon"
android:layout_below="@id/title"
android:text="부제목"/>
<TextView
android:id="@+id/time"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:text="10:30"/>
</RelativeLayout>
성능 비교
측정 과정:
- LinearLayout: 각 뷰를 순차적으로 측정
- RelativeLayout: 의존 관계 분석 후 측정 (더 복잡)
성능 순위:
측정 속도:
LinearLayout (단순) ██████████
RelativeLayout ███████
ConstraintLayout █████████
장단점 비교
LinearLayout:
장점:
- ✅ 간단하고 예측 가능
- ✅ 성능이 좋음 (단순한 구조에서)
- ✅ 가중치(
layout_weight) 지원 → 비율 배치 쉬움
단점:
- ❌ 복잡한 레이아웃시 중첩 증가
- ❌ 유연성 부족
RelativeLayout:
장점:
- ✅ 유연한 배치 가능
- ✅ 중첩 감소 가능
- ✅ 자유로운 위치 지정
단점:
- ❌ 성능 이슈 (의존 관계 계산)
- ❌ 의존성 관리 복잡
- ❌ 순환 참조 위험
사용 추천 시나리오
LinearLayout이 적합한 경우:
- 단순한 목록 (버튼 그룹, 메뉴 항목)
- 가중치를 이용한 비율 배치 필요시
- 성능이 중요한 간단한 UI
RelativeLayout이 적합한 경우:
- 다른 뷰를 기준으로 배치해야 할 때
- 중첩을 피해야 할 때
- 간단한 오버랩 레이아웃
현대적 관점에서의 비교
Google의 권장사항:
- ConstraintLayout이 LinearLayout + RelativeLayout의 장점을 모두 보유
- 새로운 프로젝트에서는 ConstraintLayout 권장
레거시 프로젝트에서:
LinearLayout : 리스트 아이템, 간단한 폼
RelativeLayout : 프로필 카드, 복잡한 아이템
ConstraintLayout: 모든 새로운 레이아웃
실전 선택 가이드
이럴 때 LinearLayout:
<!-- 버튼 그룹 -->
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent">
<Button android:layout_weight="1" android:text="확인"/>
<Button android:layout_weight="1" android:text="취소"/>
</LinearLayout>
이럴 때 RelativeLayout:
<!-- 프로필 헤더 -->
<RelativeLayout>
<ImageView android:id="@+id/background" android:layout_width="match_parent"/>
<ImageView android:id="@+id/profile" android:layout_centerInParent="true"/>
<TextView android:id="@+id/name" android:layout_below="@id/profile"/>
</RelativeLayout>
결론
| 기준 |
LinearLayout |
RelativeLayout |
| 배치 |
✅ 직관적 |
✅ 유연 |
| 성능 |
✅ 빠름 |
❌ 상대적 느림 |
| 학습 곡선 |
✅ 쉬움 |
⚠️ 보통 |
| 유지보수 |
❌ 중첩시 어려움 |
⚠️ 의존성 관리 |
| 현재 추천도 |
⚠️ 제한적 사용 |
❌ 비추천 |
최종 조언:
- 새 프로젝트: ConstraintLayout 사용
- 기존 프로젝트: 각 레이아웃의 장점 활용
- 단순 목록: LinearLayout
- 레거시 유지보수: RelativeLayout 이해 필요
RelativeLayout은 ConstraintLayout의 과도기적 솔루션으로 이해하시면 됩니다! 🔄