본문 바로가기
Android

[Android] Relative Layout(상대적 레이아웃)

by jane.dev 2021. 10. 22.
반응형
Relative Layout
상대 위치에 하위 뷰를 표시하는 뷰 그룹으로,
각 뷰의 위치는 동()위 요소에 상대적이거나 상(上)위 영역에 상대적인 위치로 지정

 

레이아웃 속성 예제

Relative Layout 내부에 버튼을 생성(id는 center)

<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_height="match_parent"
    android:layout_width="match_parent">
    <Button
        android:id="@+id/center"
        android:text="center"
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"></Button>
</RelativeLayout>

android:layout_centerHorizontal="true"android:layout_centerHorizontal="true" 로 해당 버튼(하위 요소)를 상위 요소 내에 세로와 가로 중앙에 배치

 

android:layout_above="@+id/center" 는 center의 위에 배치

android:layout_alignTop="@+id/center" 는 center의 위쪽 끝 라인을 맞춰 배치

android:layout_alignParentTop="true" 는 부모뷰인 layout의 위쪽에 맞춰 배치

 

 

android:layout_below="@+id/center" 는 center 아래에 배치

android:layout_alignBottom="@+id/center" 는 center의 아래쪽 끝 라인을 맞춰 배치

android:layout_alignParentBottom="true" 는 부모뷰인 layout의 아래쪽에 맞춰 배치

 

android:layout_toRightOf="@+id/center" 는 center 오른쪽에 배치

android:layout_alignRight="@+id/center" 는 center의 오른쪽 끝 라인을 맞춰 배치

android:layout_alignParentRight="true" 는 부모뷰인 layout의 오른쪽에 맞춰 배치

 

 

android:layout_toLeftOf="@+id/center" 는 center 왼쪽에 배치

android:layout_alignLeft="@+id/center" 는 center의 왼쪽 끝 라인을 맞춰 배치

android:layout_alignParentLeft="true" 는 부모뷰인 layout의 왼쪽에 맞춰 배치

 

→ 속성을 조합하면 다양한 위치에 배치가 가능

더 많은 속성은 아래 링크 참고

https://developer.android.com/reference/android/widget/RelativeLayout.LayoutParams?hl=ko 

 

RelativeLayout.LayoutParams  |  Android Developers

 

developer.android.com