상대 레이아웃은 부모 컨테이너, 그리고 뷰 사이의 상대적 위치관계를 정의하여 화면을 배치하는 레이아웃이다.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:id="@+id/button1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="Button1"
android:layout_below="@+id/button3"
android:layout_above="@+id/button2"
/>
<Button
android:id="@+id/button2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Button2"
android:layout_alignParentBottom="true"/>
<Button
android:id="@+id/button3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Button3"/>
</RelativeLayout>
layout_width와 layout_height 속성이 모두 match_parent인 button1은 layout_below 속성과 layout_above 속성값으로 각각 button3과 button2의 id를 넣어주어 두 버튼의 사이에 위치하게 된다. 다만, 뷰를 만들면 default로 화면의 윗쪽에 자리잡으므로 button2는 layout_alignParentBottom 속성의 값을 true로 해주어 부모 컨테이너의 아랫쪽에 붙여야 한다.
앱의 화면은 아래와 같이 나타난다.
'안드로이드' 카테고리의 다른 글
(안드로이드) 9 - 스크롤 뷰(ScrollView) (0) | 2020.05.18 |
---|---|
(안드로이드) 8 - 테이블 레이아웃(TableLayout) (0) | 2020.05.18 |
(안드로이드) 6 - 리니어 레이아웃(LinearLayout) (0) | 2020.05.18 |
(안드로이드) 5 - 레이아웃과 기본 위젯 (0) | 2020.05.18 |
(안드로이드) 4 - 제약 레이아웃(Constraint Layout) (0) | 2020.05.18 |