๋ ์ด์์
: ViewGroup์ ํ์ ํด๋์ค. ํฌํจ ๋ View๋ฅผ ์ ๋ ฌํ๋ ๊ธฐ๋ฅ
1. Linear Layout : ์ปจํ ์ด๋์ ํฌํจ ๋(์์) ๋ทฐ๋ค์ ์ํ ๋๋ ์์ง์ผ๋ก ์ผ๋ ฌ ๋ฐฐ์นํ๋ ๋ ์ด์์
- ๊ฐ์ฅ ๋จ์ํ๊ณ ์ง๊ด์ , ์ฌ์ฉ ๋น๋๊ฐ ๋์
- LinearLayout์ ์์์ผ๋ก ๋ฐฐ์น๋๋ View ์์ ฏ๋ค์ ํ ๋ฐฉํฅ(๊ฐ๋ก ๋๋ ์ธ๋ก)๋ก๋ง ๋ฐฐ์น
- ์์ ฏ์ ํฌ๊ธฐ(๋์ด ๋๋ ๋๋น)์ ๊ด๊ณ์์ด ํ ์ค๋ก๋ง ๋ฐฐ์ด
- LinearLayout์ ์์๋ค์ ์ค์ฒฉ๋์ง ์๊ณ , ์ง์ ํ ๋ฐฉํฅ์ผ๋ก ์์ด๋ ํํ๋ก ํ์๋จ
- LayoutWeight : ์์๋ค์ด ๋ฐฐ์น๋ ๋, ์ ์ฒด ์์ญ ๋๋น ๋น์จ์ ๊ฐ๋
์ผ๋ก ์ง์ ํ ์ ์๋ ๊ฐ์ค์น(weight)๋ฅผ ์ค์
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout ...>
<LinearLayout
...
android:layout_weight="1" //์ฌ๊ธฐ[1]์
android:orientation="vertical"
...>
<Button ...
android:text="Button 1"/> />
<Button
...
android:text="Button 2" />
<Button
...
android:layout_weight="1" //์๊ฐ ๋๋จธ์ง๋ฅผ ๊ฝ ์ฑ์ฐ๋ ๊ฒ
android:text="Button 3"
/>
</LinearLayout>
<LinearLayout
...
android:layout_weight="1" //์ฌ๊ธฐ[1] ๋๋ฌธ์ 1:1์ด ๋จ
android:orientation="horizontal"
...>
<Button
...
android:text="Button 4"/>
<Button
...
android:text="Button 5"/>
<Button
...
android:text="Button 6"/> //weight์ ๋ํ ์์ฑ์ ์ฃผ์ง ์์ ๋์ผํ ์ฌ์ด์ฆ
</LinearLayout>
</LinearLayout>

*์ฃผ์ ์์ฑ
1) orientation : vertical(์ฐจ์ผ๋๋ฅผ ์์์ ์๋๋ก ์์ง์ผ๋ก ๋ฐฐ์ด) / horizontal(์ฐจ์ผ๋๋ฅผ ์ผ์ชฝ์์ ์ค๋ฅธ์ชฝ์ผ๋ก ์ํ๋ฐฐ์ด)
2. Relative Layout : ๋ทฐ๋ฅผ ์๋ก ๊ฐ์ ์์น ๊ด๊ณ๋ ์ปจํ ์ด๋์์ ์์น ๊ด๊ณ๋ฅผ ์ง์ ํด ๋ฐฐ์นํ๋ ๋ ์ด์์
- ์์ view ์์ ฏ๋ค์ด ์๋ก๊ฐ์ ์๋์ ๋ฐฐ์น ๊ด๊ณ์ ๋ฐ๋ผ ํ๋ฉด์ ํ์๋ ์์น๊ฐ ๊ฒฐ์ ๋๋๋ก ๋ง๋ค์ด์ฃผ๋ Layout ํด๋์ค
//ex. B๋ฅผ A์ ์ค๋ฅธ์ชฝ์ ๋ฐฐ์น : B toRight Of A, D๋ฅผ C์ ์๋์ชฝ์ ๋ฐฐ์น : D below C
- ๋ถ๋ชจ ์์ฒด๊ฐ ์๋์ ์์น์ ๊ธฐ์ค์ ์ผ๋ก ์ฌ์ฉ๋ ์๋ ์์
// ex. A๋ฅผ RelativeLayout์ ์ผ์ชฝ์ ์ ๋ ฌ : alignParentLeft, B๋ฅผ RelativeLayout์ ๊ฐ์ด๋ฐ ์ ๋ ฌ : centerInParent
- ์์๋ค์ ์๋์ ์ธ ๋ฐฐ์น ๊ธฐ์ค์ ์ง์ ํ์ง ์์ ๊ฒฝ์ฐ, RelativeLayout ๋ด๋ถ์์ ์ค์ฒฉ๋์ด ํ์๋จ
* ์ฃผ์ ์์ฑ
1) ์์ ฏ๋ผ๋ฆฌ์ ๊ด๊ณ๋ฅผ ์ง์ ํ๋ ์์ฑ (anchor view์ id๋ฅผ ์ง์ )
| ์์ฑ | ์ค๋ช |
| layout_alignBaseline | anchor view์ baseline์ ๋ง์ถค |
| layout_alignBottom / _alignTop | anchor view์ ์๋์ชฝ / ์์ชฝ ๊ฐ์ฅ์๋ฆฌ๋ฅผ ๋ง์ถค |
| layout_alignLeft / _alignRight | anchor view์ ์ผ์ชฝ / ์ค๋ฅธ์ชฝ ๊ฐ์ฅ์๋ฆฌ๋ฅผ ๋ง์ถค |
| layout_Above / _Below | anchor view์ ์์ชฝ / ์๋์ชฝ์ ๋ฐฐ์น |
| layout_toLeft / _toRight | anchor view์ ์ผ์ชฝ / ์ค๋ฅธ์ชฝ์ ๋ฐฐ์น |
2) Parent์์ ๊ด๊ณ๋ฅผ ์ง์ ํ๋ ์์ฑ
| ์์ฑ | ์ค๋ช |
| layout_centerHorizontal | ์ํ ๋ฐฉํฅ์ผ๋ก ์ปจํ ์ด๋์ ๊ฐ์ด๋ฐ ๋ฐฐ์น |
| layout_centerVertical | ์์ง ๋ฐฉํฅ์ผ๋ก ์ปจํ ์ด๋์ ๊ฐ์ด๋ฐ ๋ฐฐ์น |
| layout_centerInParent | ?? |
| layout_alignParentLeft / Right | ์ปจํ ์ด๋์ ์ผ์ชฝ / ์ค๋ฅธ์ชฝ ๊ฐ์ฅ์๋ฆฌ๋ฅผ ๋ง์ถค |
| layout_alignParentBottom / Top | ์ปจํ ์ด๋์ ์๋์ชฝ / ์์ชฝ ๊ฐ์ฅ์๋ฆฌ๋ฅผ ๋ง์ถค |
<RelativeLayout ...>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button 1"
android:layout_alignParentTop="true"
android:layout_alignParentRight="true"
android:id="@+id/button1"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toLeftOf="@id/button1" //1๋ฒ์ ์ผ์ชฝ์ผ๋ก
android:text="Button 2"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/button1" //1๋ฒ์ ์๋๋ก
android:layout_alignParentRight="true"
android:text="Button 3"/>
</RelativeLayout>

3. Table Layout : ํ ํ์์ผ๋ก ์ฐจ์ผ๋๋ฅผ ๋ฐฐ์นํ๋ ๋ ์ด์์
- ์์ View ์์ ฏ๋ค์ ํ ์ด๋ธ(ํ๊ณผ ์ด)๋ก ๋๋์ด ํ์ํ๋ Layout ํด๋์ค

- ํ๋ฅผ ๊ตฌ์ฑํ๋ ํ์ ๊ฐ์๋งํผ TableRow๋ฅผ ํฌํจํ๊ณ TableRow๋ ๊ฐ ํ์ ํฌํจ๋ ์ (view)๋ฅผ ํฌํจํจ
*์ฃผ์ ์์ฑ
- stretchColumns : ๋๋ฆด ์ด์ ์ง์ (์ธ๋ฑ์ค๋ 0๋ถํฐ ์์๋จ)
1) "*" : ๋ชจ๋ ์ด์ ๋์ฌ์ ๋ฐฐ์น
2) "1, 2" : 1์ด(์ผ์ชฝ์์ 2๋ฒ์งธ)๊ณผ 2์ด(์ผ์ชฝ์์ 3๋ฒ์งธ)์ ๋์ฌ์ ๋ฐฐ์น
- TableRow -> ์ ํด์ง ๊ท์น์ ๋ฐ๋ผ ํฌ๊ธฐ๊ฐ ๊ฒฐ์ ๋๋ฏ๋ก, layout_width / height๋ฅผ ์ง์ ํ ํ์ ์์
// layout_height ๋ ํญ์ wrap_content, layout_width ๋ ํญ์ match_parent
<TableLayout
...
android:stretchColumns="0"
...>
<TableRow>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button 4"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button 5"/>
</TableRow>
<TableRow
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button 6"/>
</TableRow>
</TableLayout>

4. Frame Layout : ์ปจํ ์ด๋์ ํฌํจ๋ ๋ทฐ๋ค์ ์ ๋ถ ์ข์๋จ์ ๋ฐฐ์นํ๋ ๋ ์ด์์
- ํ๋์ ์์ view ์์ ฏ๋ง ํ์ํ ๋ ์ฌ์ฉํ๋ Layout ํด๋์ค (ex. ์์ ๋์ฐ๊ธฐ ๋ฑ)
- FrameLayout์ ์์์ผ๋ก ์ฌ๋ฌ View ์์ ฏ์ ์ถ๊ฐํ๋ฉด ๊ฒน์ณ์ง ํํ๋ก ํ์๋๊ณ ,
๊ฐ์ฅ ์ต๊ทผ์ ์ถ๊ฐ๋ View ์์ ฏ์ด ๊ฐ์ฅ ์์ ํ์๋จ
<FrameLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:background="#ff0000">
<Button
android:layout_width="100dp"
android:layout_height="wrap_content"
android:text="button 7"/>
<Button
android:layout_width="100dp"
android:layout_height="wrap_content"
android:text="button 8"/>
<Button
android:layout_width="100dp"
android:layout_height="wrap_content"
android:text="button 9"/>
</FrameLayout>
5. Constraint Layout : ์ ์ฝ ์กฐ๊ฑด์ ์ฌ์ฉํด ํ๋ฉด์ ๊ตฌ์ฑํ๋ ๋ฐฉ๋ฒ
- ๋ณต์กํ ๋ ์ด์์์ ๋จ์ํ ๊ณ์ธต๊ตฌ์กฐ๋ฅผ ์ด์ฉํด ํํํ ์ ์๋ ViewGroup์
- ํ์ view๋ค๊ณผ์ ๊ด๊ณ๋ฅผ ์ ์ํด ๋ ์ด์์์ ๊ตฌ์ฑํ๋ค๋ ์ ์์ RelativeLayout๊ณผ ์ ์ฌ
- but ๋ ์ ์ฐํ๊ณ ๋ค์ํ ๊ธฐ๋ฅ์ ์ ๊ณต
(+) ์์ ๋ทฐ๊ฐ์ ์ํธ ๊ด๊ณ ์ ์ ๊ฐ๋ฅ (RelativeLayout์์ ๋ถ๊ฐ๋ฅ -ex. ๋ view๋ฅผ ์์๋๋ก ๋ถ์ฌ ์ปจํ
์ด๋ ์ค์์)
(+) LinearLayout์ ์จ์ผ ํ๋ ๋ทฐ ๋น์จ ์กฐ์ ๋ ๊ฐ๋ฅ
(+) ๋ทฐ ๊ณ์ธต์ ๊ฐ๋จํ ํ ์ ์์ด ์ ์ง๋ณด์, ์ฑ๋ฅ์์ ์ฅ์

1) ์์ ฏ์ ํฌ๊ธฐ ์ค์
(1) ๋ฆฌ์ฌ์ด์ฆ ํธ๋ค ์ด์ฉํด ์ค์
- ์์ ฏ ํด๋ฆญ ์ ๋ค ๊ผญ์ง์ ์ ์ฌ๊ฐํ์ ํธ๋ค์ด ์๊น, ๋ง์ฐ์ค๋ก ๋๋๊ทธํด ํฌ๊ธฐ ์ค์
- ๋๋๊ทธํด์ ํฌ๊ธฐ ์กฐ์ ์ ์ฐ์ธก ์์ฑ์ฐฝ์ Layout_width, Layout_height์ ๋ฐ์ค๋ฅผ ์
๋ ฅํด ์ค์ ๊ฐ๋ฅ
(2) ์ง์ ๊ฐ์ ์ ๋ ฅํด ์ค์
- ์์ ฏ ํด๋ฆญ ์ ์ฐ์ธก ์์ฑ์ฐฝ์์ layout_width, layout_height๋ฅผ ์
๋ ฅํด ํฌ๊ธฐ ์ค์ ๊ฐ๋ฅ
- ์์น ์ธ์ wrap_content, match_parent๋ ์ฌ์ฉ ๊ฐ๋ฅ
2) ์์ ฏ์ ์๋์ ์ธ ์ ๋ ฌ
- ์ฌ์ด๋ํธ๋ค(์์ ฏ์ ๊ฐ ๋ณ์ ๊ฐ์ด๋ฐ)์ ์ด์ฉํด ์์ ฏ์ด ๋ค๋ฅธ ์์ ฏ ๋๋ ํ๋ฉด์ ๊ฒฝ๊ณ์ ๋ํด ์ด๋ป๊ฒ ์ ๋ ฌ๋ ์ง ์ง์ ๊ฐ๋ฅ
- ์ข์ฐ์ ํธ๋ค : ๊ฐ๋ก์ถ์ ์๋ ์์ ฏ๋ค ๊ฐ์ ์ ๋ ฌ ์ค์ , ์ํ์ ํธ๋ค : ์ธ๋ก์ถ ๊ฐ์ ์ ๋ ฌ์ ์ค์
- ์ข์ฐ ํธ๋ค์ ์ํ ํธ๋ค์ ์ฐ๊ฒฐํ ์๋ ์์
- ์์ ฏ, ํ๋ฉด์ ๊ฐ ๊ฒฝ๊ณ, ์์๋ก ์ถ๊ฐ๋ ์์ง, ์ํ์ ๊ฐ์ด๋ ๋ผ์ธ๊ณผ์ ์ ๋ ฌ ์ค์ ๊ฐ๋ฅ
- ๋๋๊ทธ, ์ฐ์ธก ์์ฑ์ฐฝ์ ์์น ๋ฑ์ ํตํด ์ค์ ๊ฐ๋ฅ
3) ์์ ฏ ๋ด์ ํฌํจ๋์ด ์๋ ํ ์คํธ์ ์์น๋ก ์ ๋ ฌ๊ธฐ์ค ์ค์ ํ๊ธฐ
- ๋ฒ ์ด์ค๋ผ์ธ ํธ๋ค
: ์์ ฏ์ ๋ง์ฐ์ค๋ฅผ ๊ฐ์ ธ๊ฐ๋ฉด ์ข์ธก ํ๋จ์ ๋ํ๋๋ ab ์์ด์ฝ ํด๋ฆญ
-> ์์ ฏ์ ํ์คํธ ํ๋จ์ ๋ฐฐ์ด์ค ๋ผ์ธ ํธ๋ค์ด ๋ณด์
-> ๋ฒ ์ด์ค๋ผ์ธ ํธ๋ค์ ํด๋ฆญํด ๋ค๋ฅธ ์์ ฏ์ ํ
์คํธ์ ์ฐ๊ฒฐ -> ์ฐ๊ฒฐ๋ ์์ ฏ์ ํ
์คํธ์ ํ๋จ์ด ์ผ์น๋๋๋ก ์ ๋ ฌ๋จ
4) constraint ํด๋ฐ ์ฌ์ฉํ๊ธฐ
(1) Autoconnect (๋ง๊ตฝ์์) : ์ผ๋๋ฉด ์์ ฏ์ ๋ฐฐ์นํ ๋ ์๋์ผ๋ก ์ด์ํ ์์ ฏ๊ณผ ๊ด๊ณ๋ฅผ ๋งบ์ด์ค
(2) Clear All Constraints (์ฐ๊ฒฐ x ๋ชจ์) : ๋ชจ๋ ๊ด๊ณ๋ฅผ ์ญ์ ํจ
(3) Infer constraints (๋ง๋ฒ์งํก์ด ๋ชจ์) : ๋ ์ด์์ ๋ด ๋ฐฐ์น๋ ๋ชจ๋ ์์ ฏ์ ํ์ฌ ์์น๋ฅผ ๊ธฐ๋ฐ์ผ๋ก ๊ด๊ณ ๋งบ์ด์ค
(4) Default Margins (8) : ์์ ฏ ๊ฐ์ ๊ธฐ๋ณธ ๋ง์ง ๊ฐ์ ์ง์
(5) Guidelines (๋ณด์กฐ์ ) : ์ํ, ์์ง ๊ฐ์ด๋๋ผ์ธ์ ๋ง๋ค ์ ์๊ณ , ์์ ฏ์ ์ด ๊ฐ์ด๋๋ผ์ธ์ ๊ธฐ์ค์ผ๋ก ์ ๋ ฌ ๊ฐ๋ฅ
5) ์์ฑ์ฐฝ
(1) Margin (๋ฒฝ๊ณผ ์ฌ๊ฐํ ์ฌ์ด) : layout_width ๊ฐ match_constraint์ด๋ฉด ์ฌ๋ฐฑ์ผ๋ก ์ค์ ํ ๊ฐ ์ ์ธ ์ ์ฒด๋ฅผ ์์ ฏ์ด ์ฐจ์ง
(2) Aspect Ratio (์ฌ๊ฐํ ์ข์ธก ์๋จ) : ํด๋ฆญ ์ ์์ ฏ์ ๊ฐ๋ก, ์ธ๋ก ๋น์จ์ ์ง์ ํ ์ ์์
(3) Wrap Content (>>>) : ๋ถ๋ฑํธ 3๊ฐ๋ก ๋ ๋ชจ์์ wrap_content๋ก ์ง์ ๋ ๊ฒ
(4) Any size (ww) : ํฑ๋๋ชจ์์ ์ต๋ํ์ ๋๋น๋ฅผ ์ฐจ์งํ๊ฒ ๋๊ณ , match_constraint๋ก ์ง์ ๋์ด ์์
(5) ์ ์ (...) : ์์ง ๊ด๊ณ๊ฐ ์์
(6) Horizontal Bias (ํ๋จ์ ์ ๊ณผ ์ ์ ์ซ์) : ์์ ฏ์ ์ข์ฐ์ธก ์์น ๋น์จ์ ์ง์ , ๋๋๊ทธํด์ ๋ณ๊ฒฝ ๊ฐ๋ฅ