안드로이드에서 제공하는 Layout은 ViewGroup의 subclass이다.
이 장에서는 그 중에서 일반적으로 사용되는 Layout Object를 살펴보기로 한다.
FrameLayout
Framelayout은 Layout의 가장 간단한 타입이다. 기본적으로 single object로 채워질 수 있는 screen의 blank space 이다.
FlameLayout의 모든 child view는 screen의 좌측상단(0,0)에 고정되며 child view의 위치를 지정할 수 없다.
LinearLayout
LinearLayout은 모든 child view를 단일방향으로 배치한다. - vertically or horizontally(XML의 orientation attribute 참조)
LinearLayout의 모든 child view는 순차적으로 stack되어 있어서 vertical인 경우 device의 width와 관계없이 row별로 하나의 child를 가진다. (horizontal일 경우도 유사함)
LinearLayout은 child view 사이의 margin과 각 chile view의 gravity(right, center or left)를 중요시 한다.
또한 개별 child view에 weight를 배정하도록 할 수있는데, 이는 parent view의 남은 공간을 채울 수 있는지에 대한 attribute로 중요한 값이다.
weight의 이해를 돕기 위해 아래 그림을 설명한다.
혹시 layout과 관련된 부분을 정확히 읽었다면 button은 wrap_content로 설정되어있고, 다른 text box는 fill_parent로 되어있을 거라고 짐작할 것이다.
TableLayout
TableLayout은 child view를 row와 column으로 포지셔닝한다. 단 border line을 출력하지 않고 cell에 대한 span을 지원하지 않는다.
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:stretchColumns="1">
<TableRow>
<TextView
android:layout_column="1"
android:text="Open..."
android:padding="3dip" />
<TextView
android:text="Ctrl-O"
android:gravity="right"
android:padding="3dip" />
</TableRow>
<TableRow>
<TextView
android:layout_column="1"
android:text="Save..."
android:padding="3dip" />
<TextView
android:text="Ctrl-S"
android:gravity="right"
android:padding="3dip" />
</TableRow>
</TableLayout>
RelativeLayout
RelativeLayout은 chiled view에게 parent view나 서로간의 상대적인 position을 설정할 수 있도록 한다.
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="@drawable/blue"
android:padding="10px" >
<TextView android:id="@+id/label"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Type here:" />
<EditText android:id="@+id/entry"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="@android:drawable/editbox_background"
android:layout_below="@id/label" />
<Button android:id="@+id/ok"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/entry"
android:layout_alignParentRight="true"
android:layout_marginLeft="10px"
android:text="OK" />
<Button android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toLeftOf="@id/ok"
android:layout_alignTop="@id/ok"
android:text="Cancel" />
</RelativeLayout>
Summary of Important View Groups
Class | Description |
---|---|
FrameLayout |
Layout that acts as a view frame to display a single object. |
Gallery |
A horizontal scrolling display of images, from a bound list. |
GridView |
Displays a scrolling grid of m columns and n rows. |
LinearLayout |
A layout that organizes its children into a single horizontal or vertical row. It creates a scrollbar if the length of the window exceeds the length of the screen. |
ListView |
Displays a scrolling single column list. |
RelativeLayout |
Enables you to specify the location of child objects relative to each other (child A to the left of child B) or to the parent (aligned to the top of the parent). |
ScrollView |
A vertically scrolling column of elements. |
Spinner |
Displays a single item at a time from a bound list, inside a one-row textbox. Rather like a one-row listbox that can scroll either horizontally or vertically. |
SurfaceView |
Provides direct access to a dedicated drawing surface. It can hold child views layered on top of the surface, but is intended for applications that need to draw pixels, rather than using widgets. |
TabHost |
Provides a tab selection list that monitors clicks and enables the application to change the screen whenever a tab is clicked. |
TableLayout |
A tabular layout with an arbitrary number of rows and columns, each cell holding the widget of your choice. The rows resize to fit the largest column. The cell borders are not visible. |
ViewFlipper |
A list that displays one item at a time, inside a one-row textbox. It can be set to swap items at timed intervals, like a slide show. |
ViewSwitcher |
Same as ViewFlipper. |