[Android Study] 레이아웃 - LinearLayout

728x90
반응형

* 대표적인 레이아웃 클래스를 정리해보았다.

 

* LinearLayout

- LinearLayout은 뷰를 가로나 세로 방향으로 나열하는 레이아웃 클래스이다. orientation 속성에 horizontal이나 vertical값으로 방향을 지정한다.

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">
    
    <Button />
    
    <Button />
    
</LinearLayout>

 

 * layout_weight

    - 위젯을 배치하다보면 가로나 세로 방향으로 여백이 생길 수 있다. 이 때 여백을 뷰로 채우기 위해서 layout_weight을 사용한다.

    - layout_weight는 뷰의 가중치를 주어서 배치한다. 예를들어, layout_weigt="2", layout_weight="1"으로 두개의 버튼을 배치한다면 화면의 2/3과 1/3을 각각 차지하게 배치가 되는것이다.

    - layout_weight은 같은 영역에 있는 뷰끼리만 여백을 나누어 차지한다. 아래 사진에서 BUTTON1, 2는 BUTTON3에 영향을 주지 않았다. BUTTON3은 부모요소인 orientation="vertical"기준으로 화면에서 세로로 layout_weight="1"을 차지하게 된다.

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

    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <!-- 420dpi = 약 2.8x -->
    
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">
        
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="2"
            android:layout_marginBottom="5dp"
            android:layout_marginTop="5dp"
            android:background="@drawable/rectangle_button"
            android:text="@string/button1" />
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:layout_marginBottom="5dp"
            android:layout_marginTop="5dp"
            android:background="@drawable/rectangle_button"
            android:text="@string/button2" />
    </LinearLayout>

    <Button
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:layout_marginBottom="5dp"
        android:layout_marginTop="5dp"
        android:background="@drawable/rectangle_button"
        android:text="@string/button3" />
    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginBottom="5dp"
        android:layout_marginTop="5dp"
        android:background="@drawable/rectangle_button"
        android:text="@string/button4" />
</LinearLayout>

 

 

 * gravity, layout_gravity

    - 뷰를 정렬할 때는 gravity와 layout_gravity 속성을 사용한다. 이 속성을 이용하지 않으면 기본값은 left/top이다. 

  • gravity: view 내부의 content (child)를 어떻게 배치할지
    • – left, right, top, bottom, center, center_horizontal, center_vertical, start, end
    • – right | bottom 으로 합쳐서 사용 가능

  • layout_gravity: 자기 자신 view를 parent 기준으로 어떻게 배치할지
    • – left, right, top, bottom, center, center_horizontal, center_vertical, start, end
    • – right | bottom 으로 합쳐서 사용 가능

* 주의할 점으로는, LinearLayout의 orientation 속성에 설정한 방향과 같은 방향으로는 layout_gravity속성이 적용되지 않는다.

   그럴 때는 안의 뷰에 layout_gravity속성을 설정하지 말고 부모 요소에서 gravity 속성으로 정렬을 해야한다.

728x90
반응형
TAGS.

Comments