티스토리 뷰

LinearLayout 은 하단과 같이 view 를 수평 혹은 수직으로 배치한다.

How to
◼ LinearLayout in XML
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" | |
android:layout_width="match_parent" | |
android:layout_height="match_parent" | |
android:orientation="vertical"> | |
<Button | |
android:layout_width="100.00dp" | |
android:layout_height="100.00dp" | |
android:text="button1" /> | |
<Button | |
android:layout_width="100.00dp" | |
android:layout_height="100.00dp" | |
android:text="button2" /> | |
</LinearLayout> |

LinearLayout.LayoutParams
View group 이 view 를 배치할 때 따르는 규칙이다.

◼ ViewGroup.LayoutParams
View 의 크기를 설정한다.
- layout_width : view 너비
- layout_height : view 높이

◼ ViewGroup.MarginLayoutParams
View 주변의 여백을 설정한다.
XML 속성 | 의미 | 상세 그림 |
layout_margin | 해당 view 의 상, 하, 좌, 우 여백 설정 | ![]() |
layout_marginLeft | 해당 view 의 좌측 여백 설정 | ![]() |
layout_marginTop | 해당 view 의 상단 여백 설정 | ![]() |
layout_marginRight | 해당 view 의 우측 여백 설정 | ![]() |
layout_marginBottom | 해당 view 의 하단 여백 설정 | ![]() |
◼ LinearLayout.LayoutParams
1. View 의 수직 & 수평 정렬을 설정한다.
- layout_gravity : Orientation 에 설정한 방향에 영향이 없는 gravity 를 이용하여 view 를 배치한다.
(orientation 이 vertical 인 상태에서 layout_gravity 를 bottom 으로 설정하면 bottom 이 무시됨)
[수평 설정인 경우 (Orientation = horizontal)]

[수직 설정인 경우 (Orientation = vertical)]
2. View 의 크기를 가중치로 결정한다.
- layout_weight : 가로 or 세로에 정의된 값만큼 비율로 크기를 결정한다.

Attributes
◼ id
id 는 해당 LinearLayout 을 유일하게 식별할 수 있는 속성이다.
<LinearLayout | |
android:id="@+id/simple_linear_layout" | |
android:layout_width="match_parent" | |
android:layout_height="match_parent" /> |
◼ orientation
View 배치를 가로 or 세로로 할지 설정할 수 있는 속성이다.
<LinearLayout | |
android:layout_width="match_parent" | |
android:layout_height="match_parent" | |
android:orientation="vertical"> | |
<Button | |
android:layout_width="100.00dp" | |
android:layout_height="100.00dp" | |
android:text="button1" /> | |
<Button | |
android:layout_width="100.00dp" | |
android:layout_height="100.00dp" | |
android:text="button2" /> | |
<Button | |
android:layout_width="100.00dp" | |
android:layout_height="100.00dp" | |
android:text="button3" /> | |
</LinearLayout> |

◼ gravity
Child view 들의 정렬 방식을 설정한다.
gravity = [left | right | center_horizontal | top | center_vertical | bottom | center | fill_horizontal | fill_vertical | fill | clip_horizontal | clip_vertical ] |
<LinearLayout | |
android:layout_width="match_parent" | |
android:layout_height="match_parent" | |
android:gravity="center_vertical|right" | |
android:orientation="horizontal"> | |
<Button | |
android:layout_width="wrap_content" | |
android:layout_height="wrap_content" | |
android:text="button1" /> | |
<Button | |
android:layout_width="wrap_content" | |
android:layout_height="wrap_content" | |
android:text="button2" /> | |
<Button | |
android:layout_width="wrap_content" | |
android:layout_height="wrap_content" | |
android:text="button3" /> | |
</LinearLayout> |

◼ layout_gravity
Orientation 에 설정한 방향에 영향이 없는 gravity 를 이용하여 view 를 배치한다.
(상세 설명은 위쪽의 LinearLayout.LayoutParams 를 참조하자)
<LinearLayout | |
android:layout_width="match_parent" | |
android:layout_height="match_parent" | |
android:orientation="vertical"> | |
<Button | |
android:layout_width="wrap_content" | |
android:layout_height="wrap_content" | |
android:layout_gravity="right" | |
android:text="button1" /> | |
<Button | |
android:layout_width="wrap_content" | |
android:layout_height="wrap_content" | |
android:text="button2" /> | |
<Button | |
android:layout_width="wrap_content" | |
android:layout_height="wrap_content" | |
android:text="button3" /> | |
</LinearLayout> |

◼ layout_weight
View 의 크기를 가중치로 결정한다.
(상세 설명은 위쪽의 LinearLayout.LayoutParams 를 참조하자)
<LinearLayout | |
android:layout_width="match_parent" | |
android:layout_height="match_parent" | |
android:orientation="horizontal"> | |
<Button | |
android:layout_width="0dp" | |
android:layout_height="wrap_content" | |
android:layout_weight="1" | |
android:text="button1" /> | |
<Button | |
android:layout_width="0dp" | |
android:layout_height="wrap_content" | |
android:layout_weight="1" | |
android:text="button2" /> | |
<Button | |
android:layout_width="0dp" | |
android:layout_height="wrap_content" | |
android:layout_weight="2" | |
android:text="button3" /> | |
</LinearLayout> |

◼ weightSum
layout_weight 을 이용할 때 전체 비율을 정확히 명시한다.
<LinearLayout | |
android:layout_width="match_parent" | |
android:layout_height="match_parent" | |
android:weightSum="5" | |
android:orientation="horizontal"> | |
<Button | |
android:layout_width="0dp" | |
android:layout_height="wrap_content" | |
android:layout_weight="1" | |
android:text="btn1" /> | |
<Button | |
android:layout_width="0dp" | |
android:layout_height="wrap_content" | |
android:layout_weight="1" | |
android:text="btn2" /> | |
<Button | |
android:layout_width="0dp" | |
android:layout_height="wrap_content" | |
android:layout_weight="2" | |
android:text="btn3" /> | |
</LinearLayout> |

◼ baselineAligned & baselineAlignedChildIndex
baselineAligned 이 true 인 상태에서 baselineAlignedChildIndex 로 자식 view 의 index(0부터 시작) 를 지정하면 지정된 view 를 기준으로 나머지 view 들이 정렬된다.
<LinearLayout | |
android:layout_width="match_parent" | |
android:layout_height="match_parent" | |
android:baselineAligned="true" | |
android:baselineAlignedChildIndex="1" | |
android:orientation="horizontal"> | |
<Button | |
android:layout_width="wrap_content" | |
android:layout_height="wrap_content" | |
android:text="button0" /> | |
<Button | |
android:layout_width="wrap_content" | |
android:layout_height="100.00dp" | |
android:text="button1" /> | |
<Button | |
android:layout_width="wrap_content" | |
android:layout_height="wrap_content" | |
android:text="button2" /> | |
</LinearLayout> |

'Android > UI' 카테고리의 다른 글
ListView (1) (0) | 2020.10.28 |
---|---|
RelativeLayout (0) | 2019.11.06 |
FrameLayout (0) | 2019.11.06 |
AbsoluteLayout (0) | 2019.11.06 |
Include & merge tag (0) | 2019.10.31 |
- Total
- Today
- Yesterday
- M2E
- android intent
- registerForContextMenu
- StartService
- onContextItemSelected
- bindservice
- 안드로이드 서비스
- android flag activity
- Android Service
- StringUtils
- 앱테크
- 무료 채굴
- 안드로이드 인텐트
- BroadcastReceiver
- notifyDataSetChanged
- android task
- p2e
- android activity flag
- onCreateContextMenu
- Intent
- WEMIX
- 리워드앱
- task
- 무료채굴
- task 생성
- 앱테크 추천
- RoomDatabase
- 채굴앱
- 리워드 어플
- mPANDO
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | 5 | ||
6 | 7 | 8 | 9 | 10 | 11 | 12 |
13 | 14 | 15 | 16 | 17 | 18 | 19 |
20 | 21 | 22 | 23 | 24 | 25 | 26 |
27 | 28 | 29 | 30 |