# 1. Language/🔰 Kotlin

Kotlin # ConstraintLayout 실습

둥굴둥굴둥굴레차 2022. 7. 12. 14:09

ConstraintLayout을 사용하여 위와 같이 만들기

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

    <TextView
        android:id="@+id/center"
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:background="@color/purple_200"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"></TextView>

    <TextView
        android:id="@+id/box1"
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:background="@color/purple_700"
        app:layout_constraintBottom_toTopOf="@id/center"
        app:layout_constraintRight_toRightOf="@+id/center"></TextView>

    <TextView
        android:id="@+id/box2"
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:background="@color/purple_500"
        app:layout_constraintBottom_toTopOf="@id/center"
        app:layout_constraintRight_toLeftOf="@+id/center"></TextView>

    <TextView
        android:id="@+id/box3"
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:background="@color/purple_500"
        app:layout_constraintBottom_toTopOf="@id/center"
        app:layout_constraintLeft_toRightOf="@+id/center"></TextView>

    <TextView
        android:id="@+id/box4"
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:background="@color/purple_700"
        app:layout_constraintRight_toLeftOf="@id/center"
        app:layout_constraintTop_toBottomOf="@id/box2"></TextView>

    <TextView
        android:id="@+id/box5"
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:background="@color/purple_700"
        app:layout_constraintLeft_toRightOf="@id/center"
        app:layout_constraintTop_toBottomOf="@id/box2"></TextView>

    <TextView
        android:id="@+id/box6"
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:background="@color/purple_500"
        app:layout_constraintLeft_toRightOf="@id/center"
        app:layout_constraintTop_toBottomOf="@id/center"></TextView>

    <TextView
        android:id="@+id/box7"
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:background="@color/purple_500"
        app:layout_constraintRight_toLeftOf="@id/center"
        app:layout_constraintTop_toBottomOf="@id/center"></TextView>

    <TextView
        android:id="@+id/box8"
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:background="@color/purple_700"
        app:layout_constraintLeft_toRightOf="@id/box7"
        app:layout_constraintTop_toBottomOf="@id/center"></TextView>

</androidx.constraintlayout.widget.ConstraintLayout>

 

 

ConstraintLayout을 사용하여 위와 같이 만들기

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TextView
        android:id="@+id/text1"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:background="@color/purple_700"
        app:layout_constraintBottom_toTopOf="@id/text4"
        app:layout_constraintEnd_toStartOf="@id/text2"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"></TextView>

    <TextView
        android:id="@+id/text2"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:background="@color/purple_500"
        app:layout_constraintBottom_toTopOf="@id/text5"
        app:layout_constraintEnd_toStartOf="@id/text3"
        app:layout_constraintStart_toEndOf="@id/text1"
        app:layout_constraintTop_toTopOf="parent"></TextView>

    <TextView
        android:id="@+id/text3"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:background="@color/purple_200"
        app:layout_constraintBottom_toTopOf="@id/text6"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toEndOf="@id/text2"
        app:layout_constraintTop_toTopOf="parent"></TextView>

    <TextView
        android:id="@+id/text4"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:background="@color/purple_200"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toStartOf="@id/text5"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@id/text1"></TextView>

    <TextView
        android:id="@+id/text5"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:background="@color/black"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toStartOf="@id/text6"
        app:layout_constraintStart_toEndOf="@id/text4"
        app:layout_constraintTop_toBottomOf="@id/text2"></TextView>

    <TextView
        android:id="@+id/text6"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:background="@color/purple_700"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toEndOf="@id/text5"
        app:layout_constraintTop_toBottomOf="@id/text3"></TextView>

</androidx.constraintlayout.widget.ConstraintLayout>