본문 바로가기

Android

Kotlin 이슈 4 기능 별로 레이아웃 나누기, include

반응형

xml 파일을 통짜로 구성하면 화면을 한눈에 보고 수정하기에 좋지만, 기능 별로 xml 파일을 따로 만드는 편이 나중에 조합하여 새로운 레이아웃을 만들기 편하기 때문에, 나눠보았습니다.

원본

<?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.widget.CoordinatorLayout
    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">

    <androidx.constraintlayout.widget.ConstraintLayout
        android:id="@+id/main"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginBottom="80dp">

        <androidx.cardview.widget.CardView
            android:id="@+id/pastLottoCardView"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            app:layout_constraintTop_toTopOf="@+id/main">

            <androidx.constraintlayout.widget.ConstraintLayout
                android:id="@+id/lottoConstraint"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:padding="16dp">

                <Spinner
                    android:id="@+id/round"
                    android:layout_width="0dp"
                    android:layout_height="40dp"
                    android:layout_marginTop="24dp"
                    app:layout_constraintEnd_toEndOf="parent"
                    app:layout_constraintStart_toStartOf="parent"
                    app:layout_constraintTop_toTopOf="parent" />

                <TextView
                    android:id="@+id/num1"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginTop="28dp"
                    android:width="40dp"
                    android:height="40dp"
                    android:gravity="center|center_horizontal|center_vertical"
                    android:textColor="@android:color/background_light"
                    android:textStyle="bold"
                    app:layout_constraintEnd_toStartOf="@+id/num2"
                    app:layout_constraintHorizontal_bias="0.5"
                    app:layout_constraintStart_toStartOf="parent"
                    app:layout_constraintTop_toBottomOf="@+id/round" />

                <TextView
                    android:id="@+id/num2"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:width="40dp"
                    android:height="40dp"
                    android:gravity="center|center_horizontal|center_vertical"
                    android:textColor="@android:color/background_light"
                    android:textStyle="bold"
                    app:layout_constraintBottom_toBottomOf="@+id/num1"
                    app:layout_constraintEnd_toStartOf="@+id/num3"
                    app:layout_constraintStart_toEndOf="@+id/num1" />

                <TextView
                    android:id="@+id/num3"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:width="40dp"
                    android:height="40dp"
                    android:gravity="center|center_horizontal|center_vertical"
                    android:textColor="@android:color/background_light"
                    android:textStyle="bold"
                    app:layout_constraintBottom_toBottomOf="@+id/num2"
                    app:layout_constraintEnd_toStartOf="@+id/num4"
                    app:layout_constraintStart_toEndOf="@+id/num2" />

                <TextView
                    android:id="@+id/num4"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:width="40dp"
                    android:height="40dp"
                    android:gravity="center|center_horizontal|center_vertical"
                    android:textColor="@android:color/background_light"
                    android:textStyle="bold"
                    app:layout_constraintBottom_toBottomOf="@+id/num3"
                    app:layout_constraintEnd_toStartOf="@+id/num5"
                    app:layout_constraintStart_toEndOf="@+id/num3" />

                <TextView
                    android:id="@+id/num5"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:width="40dp"
                    android:height="40dp"
                    android:gravity="center|center_horizontal|center_vertical"
                    android:textColor="@android:color/background_light"
                    android:textStyle="bold"
                    app:layout_constraintBottom_toBottomOf="@+id/num4"
                    app:layout_constraintEnd_toStartOf="@+id/num6"
                    app:layout_constraintStart_toEndOf="@+id/num4" />

                <TextView
                    android:id="@+id/num6"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:width="40dp"
                    android:height="40dp"
                    android:gravity="center|center_horizontal|center_vertical"
                    android:textColor="@android:color/background_light"
                    android:textStyle="bold"
                    app:layout_constraintBottom_toBottomOf="@+id/num5"
                    app:layout_constraintEnd_toEndOf="parent"
                    app:layout_constraintStart_toEndOf="@+id/num5" />

                <TextView
                    android:id="@+id/prize"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginTop="16dp"
                    android:gravity="right"
                    android:text="원"
                    app:layout_constraintBottom_toBottomOf="parent"
                    app:layout_constraintEnd_toEndOf="@+id/round"
                    app:layout_constraintTop_toBottomOf="@+id/num1" />

            </androidx.constraintlayout.widget.ConstraintLayout>

        </androidx.cardview.widget.CardView>

    </androidx.constraintlayout.widget.ConstraintLayout>

</androidx.coordinatorlayout.widget.CoordinatorLayout>

수정

<!-- activity_main.xml -->
<?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.widget.CoordinatorLayout
    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">

    <androidx.constraintlayout.widget.ConstraintLayout
        android:id="@+id/main"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginBottom="80dp">

                <!-- 이렇게 바꿀 수 있습니다. -->
                <include
                    layout="@layout/content_lotto_round"
                    android:layout_width="match_parent"
                    android:layout_height="0dp"
                    android:layout_marginTop="16dp"
                    android:layout_marginBottom="16dp"
                    app:layout_constraintBottom_toTopOf="@+id/include_radio_group"
                    app:layout_constraintTop_toBottomOf="@+id/include_lotto_round"
                    tools:layout_editor_absoluteX="16dp" />

        </androidx.constraintlayout.widget.ConstraintLayout>

</androidx.coordinatorlayout.widget.CoordinatorLayout>

<!-- content_lotto_round.xml -->
<?xml version="1.0" encoding="utf-8"?>
<androidx.cardview.widget.CardView 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:id="@+id/pastLottoCardView"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <androidx.constraintlayout.widget.ConstraintLayout
        android:id="@+id/lottoConstraint"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:padding="16dp">

        <Spinner
            android:id="@+id/round"
            android:layout_width="0dp"
            android:layout_height="40dp"
            android:layout_marginTop="24dp"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent" />

        <TextView
            android:id="@+id/num1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="28dp"
            android:width="40dp"
            android:height="40dp"
            android:gravity="center|center_horizontal|center_vertical"
            android:textColor="@android:color/background_light"
            android:textStyle="bold"
            app:layout_constraintEnd_toStartOf="@+id/num2"
            app:layout_constraintHorizontal_bias="0.5"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/round" />

        <TextView
            android:id="@+id/num2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:width="40dp"
            android:height="40dp"
            android:gravity="center|center_horizontal|center_vertical"
            android:textColor="@android:color/background_light"
            android:textStyle="bold"
            app:layout_constraintBottom_toBottomOf="@+id/num1"
            app:layout_constraintEnd_toStartOf="@+id/num3"
            app:layout_constraintStart_toEndOf="@+id/num1" />

        <TextView
            android:id="@+id/num3"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:width="40dp"
            android:height="40dp"
            android:gravity="center|center_horizontal|center_vertical"
            android:textColor="@android:color/background_light"
            android:textStyle="bold"
            app:layout_constraintBottom_toBottomOf="@+id/num2"
            app:layout_constraintEnd_toStartOf="@+id/num4"
            app:layout_constraintStart_toEndOf="@+id/num2" />

        <TextView
            android:id="@+id/num4"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:width="40dp"
            android:height="40dp"
            android:gravity="center|center_horizontal|center_vertical"
            android:textColor="@android:color/background_light"
            android:textStyle="bold"
            app:layout_constraintBottom_toBottomOf="@+id/num3"
            app:layout_constraintEnd_toStartOf="@+id/num5"
            app:layout_constraintStart_toEndOf="@+id/num3" />

        <TextView
            android:id="@+id/num5"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:width="40dp"
            android:height="40dp"
            android:gravity="center|center_horizontal|center_vertical"
            android:textColor="@android:color/background_light"
            android:textStyle="bold"
            app:layout_constraintBottom_toBottomOf="@+id/num4"
            app:layout_constraintEnd_toStartOf="@+id/num6"
            app:layout_constraintStart_toEndOf="@+id/num4" />

        <TextView
            android:id="@+id/num6"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:width="40dp"
            android:height="40dp"
            android:gravity="center|center_horizontal|center_vertical"
            android:textColor="@android:color/background_light"
            android:textStyle="bold"
            app:layout_constraintBottom_toBottomOf="@+id/num5"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toEndOf="@+id/num5" />

        <TextView
            android:id="@+id/prize"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="16dp"
            android:gravity="right"
            android:text="원"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="@+id/round"
            app:layout_constraintTop_toBottomOf="@+id/num1" />

    </androidx.constraintlayout.widget.ConstraintLayout>

</androidx.cardview.widget.CardView>

아무래도 짧은 코드가 맥락을 파악하기 더 좋겠죠?

반응형