본문 바로가기

프로그래밍/안드로이드

[안드로이드] 레이아웃 중첩

※ 저는 안드로이드 프로그래밍 정복(김상형 著, 한빛미디어) 책을 이용해 공부하고 있으며

예제와 코드는 이 책을 통해 공부중임을 밝힙니다.

개인적인 공부를 하면서 정리한 형식이기 때문에 심각한 오류가 있을 수 있습니다. 피드백 주시면 정말 감사하겠습니다.


※ 안드로이드 프로그래밍 정복(181~187p) 참조




※ 개략적으로 살펴보면 클래스의 계층 구조는 다음과 같다


Ojbect

   ↓

View : TextView, ImageView 등

  ↓

ViewGroup : FrameLayout, RelativeLaout, LinearLayout, AbsouluteLayout


즉, ViewGroup은 View로부터 파생된 ViewGroup의 서브 클래스로부터 다른 뷰를 차일드로 포함하여 차일드를 정렬하는 기능이 있다.


위 코드를 살펴보면 그림에서 빨간박스가 LInearLayout이고, orientation(방향)은 vertical로 설정되어 있으니 수직리니어이다.


수직 리니어 안에 "텍스트뷰, 테이블, 수평리니어"가 일차 차일드(child)로 배치되어 있다.


여기서 "테이블"은 다시 2행 3열의 텍스트 뷰(TextVIew)를 갖는다.


파란박스 수평(horizontal) 리니어는 "텍스트뷰"만 배치되어 있다.







※참고


일전 글에 썼던 내용인데, 다시 한번 첨부한다.


<TableLayout>


<TableRow(행)> 화면 크기가 허락되는 한 행과 열을 집어넣을 수 있다.


그런데 Row(행) 단위로 집어넣었으니 행을 추가시키려면 <TableRow>를 추가하면 "행"이 추가될 것이고


열을 추가시키려면 <TableRow>안에서 TextView를 추가하면 "열"이 추가될 것이다.


</TableLayout>


항상 헷갈리는 게 있다. 그것은 바로 행과 열.


column은 기둥이라는 뜻도 있는데, 기둥은 세로로 세워놓는다는 걸 생각하면 쉽게 암기(?) 할 수 있다.


나도 항상 헷갈려서 고생했다. 몸에 익숙해지는 수 밖에 없으니 손가락으로 십자가를 그어서라도 숙지해야 한다.



<소스 첨부>

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
>

<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="레이아웃 안에 다른 레이아웃을 배치한다."/>

<TableLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#808080">

<TableRow>
<TextView android:text="국어" android:textSize="30sp" android:padding="10dip" />
<TextView android:text="영어" android:textSize="30sp" android:padding="10dip" />
<TextView android:text="수학" android:textSize="30sp" android:padding="10dip" />

</TableRow>

<TableRow>
<TextView android:text="88" android:textSize="30sp" android:padding="10dip" />
<TextView android:text="92" android:textSize="30sp" android:padding="10dip" />
<TextView android:text="76" android:textSize="30sp" android:padding="10dip" />
</TableRow>
</TableLayout>

<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content">

<TextView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="사자"/>

<TextView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="호랑이"/>

<TextView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="코끼리"/>

</LinearLayout>
</LinearLayout>