본문 바로가기

programming/안드로이드 스튜디오

[안드로이드 프로그래밍 연습문제] Chapter 5 - 5번 (RelativeLayout)

반응형

1. 문제 안내, 예시 화면


연습문제 5 - 5

 오른쪽 화면을 XML 파일로 만드시오. 단 렐러티브레이아웃에 버튼을 사용하고 중앙에 있는 <기준 위젯>의 상대적인 위치로 다른 위젯을 배치한다. 기준 위젯의 크기는 가로세로 150dp이다.


2. 문제 풀이

 

 이 문제는 RelativeLayout에 대해 알마나 깊은 이해력을 가지고 있는지를 시험하는 문제이다. 중요한 점은 center버튼 즉 기준 버튼만을 이용하는 것이 아닌, 레이아웃 자체의 배치 속성(layout_centerHorizontal)도 같이 섞어 사용해야한다는 점이다. 이 두가지를 이해하려면 여러 시도밖에 없는 것 같다. 다음은 코드이다. 실행결과는 예시화면가 똑같기에 올리지 않겠다.

 

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <Button
        android:id="@+id/center"
        android:layout_width="150dp"
        android:layout_height="150dp"
        android:text="기준 위젯"
        android:layout_centerInParent="true"
        android:layout_centerVertical="true"
        />
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignTop="@id/center"
        android:text="1번"
        />
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignBottom="@+id/center"
        android:text="2번"
        />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBottom="@+id/center"
        android:layout_toLeftOf="@+id/center"
        android:layout_alignParentBottom="true"
        android:text="3번"
        />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="4번"
        android:layout_alignLeft="@+id/center"
        android:layout_alignParentTop="true"
        />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignRight="@+id/center"
        android:layout_alignParentBottom="true"
        android:text="5번"
        />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="6번"
        android:layout_alignParentRight="true"
        android:layout_above="@+id/center"
        />


</RelativeLayout>
반응형