Over the limit
Android Studio - 레이아웃2 , 렐러티브 레이아웃 본문
렐러티브 레이아웃 (Relative Layout)
위젯과 부모와의 위치 관계 또는 위젯끼리의 관계를 지정함으로써 뷰를 배치
자식 뷰의 기본 위치는 부모 뷰의 좌측 상단이지만 다양한 속성을 이용해 자유롭게 위치를 지정할 수 있다.
렐러티브 레이아웃을 이용하여 상, 하, 좌, 우, 중 에 button을 구현하고 싶다면 아래와 같은 방식으로 코드를
작성하면 된다.
<RelativeLayout xmlns:android ="http://~~"
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:text="위쪽"/>
<Button
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:text="좌측"/>
<Button
android:layout_centerInParent="true"
android:text="중앙"/>
<Button
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:text="우측"/>
<Button
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:text="아래"/>
</RelativeLayout>
다른 위젯의 상대 위치에 배치
다른 위젯의 특정한 곳에 배치하는 방법도 있음.
각 속성의 값에 다른 위젯의 아이디를 "@+id/식별자"와 같은 형식으로 사용하면 됨.
<RealtiveLayout xmlns:android="http://~~"
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:id=@+id/baseBtn"
android:layout_width="150dp"
android:layout_height="150dp"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:text="기준위젯"/>
<Button
android:layout_alignTop="@+id/baseBtn"
android:layout_toLeftOf="@+id/baseBtn"
android:text="1번"/>
~~~생략~~~
<Button
android:layout_above="@id+baseBtn"
android:layout_alignLeft="@id/baseBtn"
android:text="4번"/>
<Button
android:layout_alignRight="@id+baseBtn"
android:layout_below="@id/baseBtn"
android:text="5번"/>
<Button
android:layout_above="@id+baseBtn"
android:layout_toRightOf="@id/baseBtn"
android:text="6번"/>
</RelativeLayout>
'Framework > Android Studio' 카테고리의 다른 글
[Kotlin] BMI 측정기 만들기 (0) | 2021.07.15 |
---|---|
[Kotlin] 버튼을 누르면 문자가 출력되게 (0) | 2021.07.13 |
Android Studio - 레이아웃, 리니어 레이아웃 사용하기 (0) | 2021.07.11 |
[Kotiln] Kotiln 기본 문법4 - 함수 (0) | 2021.07.10 |
[Kotlin] Kotlin 기본 문법3 - 배열 (0) | 2021.07.06 |