Android LinearLayout, RealativeLayout 관련 설명 및 예제



1. LinearLayout

가장 기본적으로 많이 사용하는 레이아웃.
레이아웃을 수평 수직으로 배치해줍니다.

아래의 소스의 밑줄친 소스를 보시면 orientation 속성을 통해 vertical 세로배치와  horizontal 가로배치로 해줄수 있다.
추가로 기초적인 속성을 살펴보면
layout_width, layout_height 속성값에는 꽉채워주는 "fill_parent", 필요한 만큰의 공간 차지 "wrap_content", 직접값을 "120px" 처럼 값을 지정해줄수 있다.

추가로 html 처럼 padding이라는 속성이 있어 자유롭게 레이아웃을 꾸며줄수 도 있다.

다음 예제는 linearlayout으로 가로배치와 세로배치를 살펴볼수 있다.
그리고 패딩값을 줌으로써 다음 LinearLayout에서 그 공간을 제외하고서 안에 bagroundColor가 채워진것을 보고서 fill_parent, wrap_content 를 살펴보면 된다.
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3.    android:orientation="vertical"
  4.    android:layout_width="fill_parent"
  5.    android:layout_height="fill_parent"
  6.    android:background="#ffffff"
  7.    android:paddingLeft = "20px"
  8.    android:paddingTop = "20px"
  9.    android:paddingRight = "20px"
  10.    android:paddingBottom = "20px"
  11.    >
  12.         <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  13.             android:orientation="vertical"   =========>세로배치
  14.             android:layout_width="fill_parent"
  15.             android:layout_height="wrap_content"
  16.             android:background="#ff0000"
  17.             >
  18.             <TextView
  19.                 android:layout_width="wrap_content"
  20.                         android:layout_height="wrap_content"
  21.                         android:text="vertical"
  22.                 />
  23.                 <Button
  24.                         android:text="버튼"
  25.                         android:layout_width="wrap_content"
  26.                         android:layout_height="wrap_content"
  27.                 />
  28.                 <EditText
  29.                         android:text="EditText"
  30.                         android:layout_width="wrap_content"
  31.                         android:layout_height="wrap_content"
  32.                 />             
  33.     </LinearLayout>
  34.     <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  35.             android:orientation="horizontal"   ========>가로배치
  36.             android:layout_width="fill_parent"
  37.             android:layout_height="wrap_content"
  38.             android:background="#0000ff"
  39.    >
  40.         <TextView
  41.                 android:layout_width="wrap_content"
  42.                         android:layout_height="wrap_content"
  43.                         android:text="horizontal"
  44.                 />
  45.                 <Button
  46.                         android:text="버튼"
  47.                         android:layout_width="wrap_content"
  48.                         android:layout_height="wrap_content"
  49.                 />
  50.                 <EditText
  51.                         android:text="EditText"
  52.                         android:layout_width="wrap_content"
  53.                         android:layout_height="wrap_content"
  54.                 />
  55.     </LinearLayout>
  56. </LinearLayout>



2. RealativeLayout
레이아웃내의 요소들끼리 위치관계를 부여하고, 그 관계에 따라 화면을 구성.
속성값을 살펴보면..


 속성 설명
 layout_above ~의 위에 배치한다.
 layout_below ~의 아래에 배치한다.
 layout_toLeftOf ~의 왼쪽에 배치한다.
 layout_toRightOf ~의 오른쪽에 배치한다.
 layout_alignLeft ~와 왼쪽 변을 맞춘다.
 layout_alignTop ~와 위쪽 변을 맞춘다.
 layout_alignRight ~와 오른쪽 변을 맞춘다.
 layout_alignBottom ~와 아래쪽 변을 맞춘다.
 layout_alignParentLeft true이면 부모와 왼쪽 변을 맞춘다.
 layout_alignParentTop true이면 부모와 위쪽 변을 맞춘다.
 layout_alignParentRight true이면 부모와 오른쪽 변을 맞춘다.
 layout_alignParentBottom true이면 부모와 아래쪽 변을 맞춘다.
 layout_alignBaseline ~와 베이스라인을 맞춘다.
 layout_alignWithParentIfMissing layout_toLeftOf 등의 속성에 대해 앵커가 발견되지 않으면 부모를 앵커로 사용한다.
 layout_centerHorizontal true이면 부모의 수평 중앙에 배치한다.
 layout_centerVertical true이면 부모의 수직 중앙에 배치한다.
 layout_centerInParent true이면 부모의 수평, 수직 중앙에

다음 예제의 맨 밑줄에 속성들을 자세히 살펴보면 다음 RelativeLayout이 쓰이는 용도를 알 수 있다.


  1. <?xml version="1.0" encoding="utf-8"?>
  2. <RelativeLayout
  3.  android:layout_width="fill_parent"
  4.  android:layout_height="wrap_content"
  5.  android:background="#ffffff"
  6.  android:padding="5px"
  7.  >
  8.   <ImageView
  9.         android:id="@+id/picture"
  10.         android:layout_width="wrap_content"
  11.         android:layout_height="wrap_content"
  12.         android:layout_alignParentLeft="true"
  13.         android:layout_marginRight="5px"
  14.         android:src="@drawable/picman"
  15.  />
  16.   <Button
  17.         android:id="@+id/btndel"
  18.         android:layout_width="wrap_content"
  19.         android:layout_height="wrap_content"
  20.         android:text="Del"
  21.         android:textSize="6pt"
  22.         android:layout_below="@id/picture"
  23.  />
  24.   <TextView
  25.         android:id="@+id/name"
  26.         android:layout_width="wrap_content"
  27.         android:layout_height="wrap_content"
  28.         android:text="최찬호"
  29.         android:textColor="#000000"
  30.         android:textSize="12pt"
  31.         android:layout_alignParentTop="true"
  32.         android:layout_toRightOf="@id/picture"
  33.  />
  34.   <TextView
  35.         android:id="@+id/call"
  36.         android:layout_width="wrap_content"
  37.         android:layout_height="wrap_content"
  38.         android:text="010-3456-8146"
  39.         android:textColor="#0000ff"
  40.         android:textSize="6pt"
  41.         android:layout_alignParentRight="true"
  42.         android:layout_alignBaseline="@id/name"
  43.  />
  44.     <TextView
  45.         android:id="@+id/description"
  46.         android:layout_width="wrap_content"
  47.         android:layout_height="wrap_content"
  48.         android:text="이것저것 쓰면서 아무거나 에헤라디야 안녕하세요 저는 최찬호입니다. 친해지고 싶네요..ㅋㅋ 야호야호 써지나 한번 볼까"
  49.         android:textColor="#000000"
  50.         android:textSize="6pt"
  51.         android:layout_below="@id/name"
  52.         android:layout_alignLeft="@id/name"
  53.  />
  54. </RelativeLayout>

    3. TableLayout
    말그대로 표 형태의 레이아웃을 정리해준다.
    행의 구분은 TableRow로 구분해 준다.
  55. <?xml version="1.0" encoding="utf-8"?>
  56.        
  57. <TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
  58.         android:orientation="vertical"
  59.         android:layout_width="fill_parent"
  60.         android:layout_height="fill_parent"
  61. >
  62.         <TableRow
  63.                 android:layout_weight = "1"
  64.         >
  65.                 <EditText
  66.                         android:id = "@+id/resultText"
  67.                         android:layout_width="fill_parent"
  68.                         android:layout_height="fill_parent"
  69.                         android:layout_weight = "1"
  70.                         android:text=""
  71.                         android:gravity="center_vertical|right"
  72.                         android:background="#ffffff"
  73.                 />
  74.         </TableRow>
  75.         <TableRow
  76.                 android:layout_weight = "1"
  77.                 android:background="#ff0000"
  78.         >
  79.                 <Button
  80.                         android:id="@+id/num7"
  81.                         android:layout_width="fill_parent"
  82.                         android:layout_height="fill_parent"
  83.                         android:text="7"
  84.                         android:layout_weight = "1"
  85.                        
  86.                 />
  87.                 <Button
  88.                         android:id="@+id/num8"
  89.                         android:layout_width="fill_parent"
  90.                         android:layout_height="fill_parent"
  91.                         android:text="8"
  92.                         android:layout_weight = "1"
  93.                 />
  94.                 <Button
  95.                         android:id="@+id/num9"
  96.                         android:layout_width="fill_parent"
  97.                         android:layout_height="fill_parent"
  98.                         android:text="9"
  99.                         android:layout_weight = "1"
  100.                 />
  101.                 <Button
  102.                         android:id="@+id/divide"
  103.                         android:layout_width="fill_parent"
  104.                         android:layout_height="fill_parent"
  105.                         android:text="/"
  106.                         android:layout_weight = "1"
  107.                 />
  108.         </TableRow>
  109.         <TableRow
  110.                 android:layout_weight = "1"
  111.                 android:background="#00ff00"
  112.         >
  113.                 <Button
  114.                         android:id="@+id/num4"
  115.                         android:layout_width="fill_parent"
  116.                         android:layout_height="fill_parent"
  117.                         android:text="4"
  118.                         android:layout_weight = "1"
  119.                 />
  120.                 <Button
  121.                         android:id="@+id/num5"
  122.                         android:layout_width="fill_parent"
  123.                         android:layout_height="fill_parent"
  124.                         android:text="5"
  125.                         android:layout_weight = "1"
  126.                 />
  127.                 <Button
  128.                         android:id="@+id/num6"
  129.                         android:layout_width="fill_parent"
  130.                         android:layout_height="fill_parent"
  131.                         android:text="6"
  132.                         android:layout_weight = "1"
  133.                 />
  134.                 <Button
  135.                         android:id="@+id/multi"
  136.                         android:layout_width="fill_parent"
  137.                         android:layout_height="fill_parent"
  138.                         android:text="*"
  139.                         android:layout_weight = "1"
  140.                 />
  141.         </TableRow>
  142.         <TableRow
  143.                 android:layout_weight = "1"
  144.                 android:background="#0000ff"
  145.         >
  146.                 <Button
  147.                         android:id="@+id/num1"
  148.                         android:layout_width="fill_parent"
  149.                         android:layout_height="fill_parent"
  150.                         android:text="1"
  151.                         android:layout_weight = "1"
  152.                 />
  153.                 <Button
  154.                         android:id="@+id/num2"
  155.                         android:layout_width="fill_parent"
  156.                         android:layout_height="fill_parent"
  157.                         android:text="2"
  158.                         android:layout_weight = "1"
  159.                 />
  160.                 <Button
  161.                         android:id="@+id/num3"
  162.                         android:layout_width="fill_parent"
  163.                         android:layout_height="fill_parent"
  164.                         android:text="3"
  165.                         android:layout_weight = "1"
  166.                 />
  167.                 <Button
  168.                         android:id="@+id/minus"
  169.                         android:layout_width="fill_parent"
  170.                         android:layout_height="fill_parent"
  171.                         android:text="-"
  172.                         android:layout_weight = "1"
  173.                 />
  174.         </TableRow>
  175.         <TableRow
  176.                 android:layout_weight = "1"
  177.                 android:background="#666666"
  178.         >
  179.                 <Button
  180.                         android:id="@+id/clear"
  181.                         android:layout_width="fill_parent"
  182.                         android:layout_height="fill_parent"
  183.                         android:text="c"
  184.                         android:layout_weight = "1"
  185.                 />
  186.                 <Button
  187.                         android:id="@+id/num0"
  188.                         android:layout_width="fill_parent"
  189.                         android:layout_height="fill_parent"
  190.                         android:text="0"
  191.                         android:layout_weight = "1"
  192.                 />
  193.                 <Button
  194.                         android:id="@+id/result"
  195.                         android:layout_width="fill_parent"
  196.                         android:layout_height="fill_parent"
  197.                         android:text="="
  198.                         android:layout_weight = "1"
  199.                 />
  200.                 <Button
  201.                         android:id="@+id/plus"
  202.                         android:layout_width="fill_parent"
  203.                         android:layout_height="fill_parent"
  204.                         android:text="+"
  205.                         android:layout_weight = "1"
  206.                 />
  207.         </TableRow>
  208. </TableLayout>