본문 바로가기

android/UI

[ Android ] RelativeLayout을 사용한 다양한 UI 예제

반응형

안드로이드의 RelativeLayout을 사용하여 다양한 UI 예제 프로젝트를 만드는 것은 앱 개발에 유용한 연습이 될 수 있습니다.
RelativeLayout은 자식 뷰의 위치를 다른 뷰와 상대적으로 배치할 수 있게 해주는 레이아웃입니다.
다음은 RelativeLayout을 사용한 몇 가지 UI 예제입니다.

1. 로그인 화면

로그인 화면 레이아웃

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="16dp">

    <TextView
        android:id="@+id/tvTitle"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Login"
        android:textSize="24sp"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="32dp"/>

    <EditText
        android:id="@+id/etUsername"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="Username"
        android:layout_below="@id/tvTitle"
        android:layout_marginTop="16dp"/>

    <EditText
        android:id="@+id/etPassword"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="Password"
        android:layout_below="@id/etUsername"
        android:layout_marginTop="8dp"
        android:inputType="textPassword"/>

    <Button
        android:id="@+id/btnLogin"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Login"
        android:layout_below="@id/etPassword"
        android:layout_marginTop="16dp"/>

</RelativeLayout>

2. 사용자 프로필 화면

프로필 화면 레이아웃

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="16dp">

    <ImageView
        android:id="@+id/ivProfilePicture"
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:src="@drawable/ic_profile"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="32dp"
        android:scaleType="centerCrop"
        android:background="?attr/selectableItemBackgroundBorderless"/>

    <TextView
        android:id="@+id/tvName"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="John Doe"
        android:textSize="18sp"
        android:layout_below="@id/ivProfilePicture"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="16dp"/>

    <TextView
        android:id="@+id/tvBio"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Android Developer | Tech Enthusiast"
        android:layout_below="@id/tvName"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="8dp"
        android:textColor="@android:color/darker_gray"/>

    <Button
        android:id="@+id/btnEditProfile"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Edit Profile"
        android:layout_below="@id/tvBio"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="16dp"/>

</RelativeLayout>

3. 상품 카드 레이아웃

상품 카드 레이아웃

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:padding="16dp"
    android:background="@drawable/card_background"
    android:elevation="4dp">

    <ImageView
        android:id="@+id/ivProductImage"
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:src="@drawable/ic_product"
        android:layout_alignParentStart="true"
        android:layout_centerVertical="true"
        android:scaleType="centerCrop"/>

    <TextView
        android:id="@+id/tvProductName"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Product Name"
        android:textSize="18sp"
        android:layout_toEndOf="@id/ivProductImage"
        android:layout_marginStart="16dp"
        android:layout_alignTop="@id/ivProductImage"/>

    <TextView
        android:id="@+id/tvProductDescription"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Product description goes here. It should be brief and informative."
        android:layout_toEndOf="@id/ivProductImage"
        android:layout_marginStart="16dp"
        android:layout_below="@id/tvProductName"
        android:layout_alignBottom="@id/ivProductImage"
        android:textColor="@android:color/darker_gray"/>

    <TextView
        android:id="@+id/tvProductPrice"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="₩99,000"
        android:textSize="16sp"
        android:layout_alignParentEnd="true"
        android:layout_centerVertical="true"
        android:textColor="@android:color/holo_red_dark"/>

</RelativeLayout>

4. 채팅 메시지 레이아웃

채팅 메시지 레이아웃

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:padding="8dp">

    <ImageView
        android:id="@+id/ivProfilePic"
        android:layout_width="40dp"
        android:layout_height="40dp"
        android:src="@drawable/ic_profile"
        android:layout_alignParentStart="true"
        android:layout_centerVertical="true"
        android:scaleType="centerCrop"
        android:background="?attr/selectableItemBackgroundBorderless"/>

    <TextView
        android:id="@+id/tvMessage"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello, how are you?"
        android:background="@drawable/bg_message_bubble"
        android:padding="8dp"
        android:layout_toEndOf="@id/ivProfilePic"
        android:layout_marginStart="8dp"
        android:layout_alignTop="@id/ivProfilePic"/>

    <TextView
        android:id="@+id/tvTimestamp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="10:30 AM"
        android:textSize="12sp"
        android:layout_below="@id/tvMessage"
        android:layout_toEndOf="@id/ivProfilePic"
        android:layout_marginStart="8dp"
        android:layout_marginTop="4dp"
        android:textColor="@android:color/darker_gray"/>

</RelativeLayout>

5. 로그인 성공 화면

로그인 성공 화면 레이아웃

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="16dp">

    <TextView
        android:id="@+id/tvWelcome"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Welcome!"
        android:textSize="24sp"
        android:layout_centerInParent="true"/>

    <Button
        android:id="@+id/btnLogout"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Logout"
        android:layout_below="@id/tvWelcome"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="24dp"/>

</RelativeLayout>

참고 사항

  • 각 예제는 RelativeLayout을 사용하여 자식 뷰를 다른 뷰에 상대적으로 배치합니다.
  • 다양한 속성(layout_below, layout_alignParentStart, layout_centerInParent 등)을 사용하여 뷰의 위치를 지정합니다.
  • XML 파일의 경로는 보통 res/layout 디렉터리 아래에 저장됩니다.

위의 예제들을 통해 RelativeLayout을 사용하여 다양한 UI를 구현할 수 있습니다. 이러한 예제를 바탕으로 더 복잡한 레이아웃을 설계하고 개발해 보세요.

반응형