7-2. RelativeLayout

StrayCat·2022년 10월 5일
0

RelativeLayout

상대 뷰의 위치를 기준으로 정렬하는 레이아웃 클래스이다. 기존에 출력된 뷰를 기준으로 방향을 지정하여 배치한다.

<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TextView
        android:id="@+id/text01"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello"
        android:textStyle="bold"
        android:layout_alignRight="@+id/image01"
        android:layout_above="@+id/image01"
        />

    <ImageView
        android:id="@+id/image01"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/ic_launcher_foreground"
        android:layout_centerInParent="true"
        >
    </ImageView>
</RelativeLayout>
  • 각 뷰는 아래 속성을 사용해서 상대적인 위치를 나타낸다. 배치 속성을 사용하지 않을 경우 겹쳐서 출력된다.

    • layout_below
    • layout_above
    • layout_toLeftOf
    • layout_toRightOf
  • android:layout_above="@+id/image01" 와 같이 배치 속성에 기준이 되는 뷰의 id 속성을 넣는다.


  • 아래 정렬 속성을 사용해 상대뷰를 기준으로 어디에 맞춰서 정렬할 지 설정한다.

    • layout_alignTop
    • layout_alignBottom
    • layout_alignLeft
    • layout_alignRight
    • layout_alignBaseline : 텍스트 기준선을 맞춘다.
  • android:layout_alignRight="@+id/image01" 와 같이 배치 속성에 기준이 되는 뷰의 id 속성을 넣는다.


  • 아래는 부모를 기준으로 정렬하는 속성이다.

    • layout_alignParentTop
    • layout_alignParentBottom
    • layout_alignParentLeft
    • layout_alignParentRight
    • layout_centerHorizontal : 부모의 가로 방향 중앙
    • layout_centerVertical : 부모의 세로 방향 중앙
    • layout_centerInParent : 부모의 가로세로 중앙
  • android:layout_centerInParent="true" 와 같이 true, false 값을 넣는다.

(XML레이아웃 미리보기)

0개의 댓글