반응형

LinearLayout

설명

  • 가로 혹은 세로 방향으로 뷰를 배치하는 레이아웃

주요 속성

  • orientation
    • 뷰가 배치될 방향
    • vertical, horizontal
  • weight
    • 배치 후 남은 공간을 할당 받을 비율

예시 코드

  • 가로로 두 버튼 배치
  • button2의 속성이 layout_weight가 1이므로, button1이 차지한 영역 이외의 나머지 영역을 모두 차지
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal"
    tools:context=".MainActivity">

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello World" />

    <Button
        android:id="@+id/button2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="Hello World" />

</LinearLayout>

참고

RelativeLayout

설명

  • 특정 뷰 기준으로 배치하는 레이아웃

주요 속성

  • 뷰의 위치를 부모 기준으로 배치
    • alignParentTop
    • alignParentBottom
    • alignParentLeft
    • alignParentRight
    • centerHorizontal
    • centerVertical
    • centerInParent
  • 뷰의 위치를 지정된 뷰 기준으로 배치
    • alignTop
    • alignBottom
    • alignLeft
    • alignRight
    • below : 지정된 뷰 하단에 배치
    • above : 지정된 뷰 상단에 배치
    • toRightOf : 지정된 뷰 우측에 배치
    • toLeftOf : 지정된 뷰 좌측에 배치
    • baseline : 지정된 뷰의 베이스 라인에 맞춰 배치

예시 코드

  • button1은 부모의 우측 하단 기준에 맞춰 배치
  • button2는 button1의 하단을 맞추고, 왼쪽에 배치
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_alignParentBottom="true"
        android:text="Hello World" />

    <Button
        android:id="@+id/button2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBottom="@id/button1"
        android:layout_toLeftOf="@id/button1"
        android:text="Hello World" />

</RelativeLayout>

참고

반응형

'Development > Android' 카테고리의 다른 글

[Android] Spinner  (0) 2021.02.09
[Android] ListView  (0) 2021.02.09
[Android] View  (0) 2021.02.09
[Android] Toast, Snackbar  (0) 2021.02.09
[Android] Intro  (0) 2021.02.09

+ Recent posts