Android

[ Android tutorial ] Hello, Views > Relative Layout

개발여우 2010. 4. 4. 23:34
Hello, Views >

Relative Layout

이글은 http://developer.android.com/ 의 튜토리얼을 제 나름대로 정리한 글입니다.
주관적이고 잘못된 요소가 들어 있을 수 있습니다.

# Relative layout 

 이것은 상대적인 레이아웃으로 생각하면되고 간단히 기준을 하나 잡고 그 기준에 맞춰
레이아웃이 맞춰 진다고 보면됩니다.

 전체 소스를 훑어보면 처음에 Relative layout이 하나 들어가고 android:layout_width과 height이
 fill_parent로 설정되어 화면의 전체로 설정합니다.  그안에 Textview, EditText, Button2개가 차례 대로
 들어 있습니다. 

 1. TextView을 보면 자신의 id를 설정해주는데 @+id/label 즉 label로 이름을 지어줍니다.
 크기는 width을 fill_parent로 화면 가득 차게 설정해 줍니다. height은 wrap_content방식으로
 자신의 코드까지만 화면을 차지합니다.

 2. EditText창은 android:id="@+id/entry" 라고 첫줄에 있으며 자신을 entry라 명명합니다.
 layout크기는 Textview와 동일하고 background를 @android:drawable/editbox_background" 라고
 설정합니다. edit박스를 불러올 곳을 지정해 주는듯 합니다.
 주의할곳은 android:layout_below="@+id/lable" 이곳인데 바로 lable의 아래(below)에
 EditText창을 위치한다고 설정하는 것입니다.

 3. 첫번째 Button은 id가 ok로 설정되어있으며  크기는 wrap방식으로 설정되어있고
 위치는 entry의 below에 위치합니다.
 특이한점은 android:layout_alignParentRight 가 true로 설정된것입니다.
 아 중요한것 하나 android:layout_marginLeft 는 10dip로 설정되어있는데
 이것은 자신의 왼쪽 여백을 10dip로 설정하는것입니다.
 마지막줄에는 button에 들어갈 OK가 android:Text="OK"이라고 설정되어 있습니다.


4. 두번째 Button은 id가 없이 위치설정되어있고 
 android:layout_toLeftOf 에 ok즉 id가 ok인 녀석의 왼쪽에 위치한다는것을 의미합니다.
 마지막줄에는 button에 들어갈 Cancle이 android:Text="Cancle"이라고 설정되어 있습니다.
 

# 스크린샷
 


# 소스코드
 res/layout/main.xml 에 넣어 주시면 됩니다.

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
   
android:layout_width="fill_parent"
   
android:layout_height="fill_parent">
   
<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="10dip"
       
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>