07-02 상대 위치로 배치 - RelativeLayout
RelativeLayout 배치 규칙
- RelativeLayout은 상대 뷰의 위치를 기준으로 정렬하는 레이아웃 클래스이다
○ 화면에 출력된 특정 뷰를 기준으로 방향을 지정하여 배치
○ 각 속성에 입력하는 값은 기준이 되는 부의 id
예시)
<TextView
android:layout_width="150dp"
android:layout_height="150dp"
android:background="#FF0000"
android:textSize="15dp"
android:textStyle="bold"
android:textColor="#FFFFFF"
android:text="Some random text"
android:gravity="right|bottom"
/>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@mipmap/ic_launcher"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello"
/>
- 위 예시를 보면 LinearLayout처럼 RelativeLayout은 자동으로 정열 안함
- 만약 이미지를 오른쪽으로 배치하고 싶으면 android:toRightOf 속성을 사용해야한다
<TextView
android:id="@+id/ch7testTextView"
android:layout_width="150dp"
android:layout_height="150dp"
android:background="#FF0000"
android:textSize="15dp"
android:textStyle="bold"
android:textColor="#FFFFFF"
android:text="Some random text"
android:gravity="right|bottom"
/>
<ImageView
android:id="@+id/ch7testImageView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@mipmap/ic_launcher"
android:layout_toRightOf="@+id/ch7testTextView"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello"
/>
맞춤 정렬하는 align 속성
- 다른 정렬 id들
○ android:layout_alignTop: 기존 뷰와 위쪽으로 맞춤
○ android:layout_alignBottom: 기존 뷰와 아래로 맞춤
○ android:layout_alignLeft: 기존 뷰와 왼쪽으로 맞춤
○ android:layout_alignRight: 기존 뷰와 오른쪽으로 맞춤
○ android:layout_alignBaseline: 기존 뷰와 텍스트 기준성을 맞춤
예시)
<TextView
android:id="@+id/ch7testTextView"
android:layout_width="150dp"
android:layout_height="150dp"
android:background="#FF0000"
android:textSize="15dp"
android:textStyle="bold"
android:textColor="#FFFFFF"
android:text="Some random text"
android:gravity="right|bottom"
/>
<ImageView
android:id="@+id/ch7testImageView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@mipmap/ic_launcher"
android:layout_toRightOf="@+id/ch7testTextView"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello"
android:layout_toRightOf="@+id/ch7testTextView"
android:layout_alignBottom="@+id/ch7testTextView"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="(Align pic)"
android:layout_alignBaseline="@+id/ch7testImageView"
/>
- android:layout_alignParentTop:부모의 위쪽에 맞춤
- android:layout_alignParentBottom: 부모의 아래쪽에 맞춤
- android:layout_alignParentLeft:부모의 왼쪽에 맞춤
- android:layout_alignParentRight:부모의 오른쪽에 맞춤
- android:layout_centerHorizontal: 부모의 가로 방향 중장에 맞춤
- android:layout_centerVertical: 부모의 세로 방향 중앙에 맞춤
- android:layout_centerInParent:부모의 가로 세로 중앙에 맞춤