릴레티브 레이아웃(RelativeLayout)

참고 프로젝트: RelativeLayoutDemoA1

릴레티브 레이아웃은 상대적 위치에 기반하여 뷰들을 배치하는 레이아웃이다. 일명 상대적 배치관리자라고 한다. 이 레이아웃을 사용할 경우에는 어떤 뷰의 왼쪽에 배치하거나 아래에 배치하는 방식으로 뷰를 배치해야 한다.

다음은 릴레티브 레이아웃의 상속 계층도와 이를 사용한 예제 화면이다.

java.lang.Object
  ↳ android.view.View
    ↳ android.view.ViewGroup
      ↳ android.widget.RelativeLayout

이 화면을 구성하는 레이아웃은 다음과 같다.

코드 RelativeLayout

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

    <TextView
        android:id="@+id/label"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:paddingTop="15px"
        android:text="Name" />

    <EditText
        android:id="@+id/edit"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_toRightOf="@id/label" />

    <Button
        android:id="@+id/ok"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/edit"
        android:layout_toLeftOf="@+id/cancel"
        android:text="OK" />

    <Button
        android:id="@+id/cancel"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_below="@id/edit"
        android:text="Cancel" />
</RelativeLayout>

릴레티브 레이아웃에서 중요하게 보아야 할 속성은 android:layout_toRightOf, android:layout_toLeftOf, android:layout_below이다. 각각 지정된 뷰의 오른쪽, 왼쪽, 아래에 배치하라는 의미를 가지며 이러한 속성은 다양하게 정의되어 있으므로 안드로이드 API를 통해 해당 속성들을 살펴봐야 한다. 주요 속성에 대해서 간단히 살펴보겠다.

android:layout_toRightOf="@id/label"
현재 뷰를 아이디가 label인 뷰의 오른쪽에 배치하라는 선언이다.

android:layout_toLeftOf="@+id/cancel"
현재 뷰를 아이디가 cancel인 뷰의 왼쪽에 배치하라는 선언이다.

android:layout_below="@id/edit"
현재 뷰를 아이디가 edit인 뷰의 아래에 배치하라는 선언이다.

android:layout_alignParentRight="true"
현재 뷰를 부모 뷰의 오른쪽에 배치하라는 선언이다.

릴레티브 레이아웃과 관련된 속성을 살펴보고 싶다면 다음 링크를 참고하기 바란다.
URL http://www.androidside.com/docs/reference/android/widget/RelativeLayout.html

results matching ""

    No results matching ""