[android] ConstraintLayout에서의 아이템 크기 조절

ConstraintLayout은 안드로이드 앱의 UI를 설계하는 데 매우 유용한 도구입니다. 여기서는 ConstraintLayout에서 아이템의 크기를 조절하는 방법에 대해 알아보겠습니다.

1. Wrap Content와 Match Constraints

ConstraintLayout에서 아이템의 크기를 조절하는 가장 기본적인 방법은 wrap_contentmatch_constraint를 사용하는 것입니다.

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Wrap Content" />

<Button
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    android:text="Match Constraints" />

2. Dimension Ratio

아이템의 가로 및 세로 크기의 비율을 지정하기 위해 app:layout_constraintDimensionRatio 속성을 사용할 수 있습니다.

<ImageView
    android:layout_width="0dp"
    android:layout_height="0dp"
    app:layout_constraintDimensionRatio="16:9"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    android:src="@drawable/image" />

3. Chains

Chains는 여러 아이템을 그룹화하여 함께 정렬하고 크기를 조절하는 데 사용됩니다.

<androidx.constraintlayout.widget.Guideline
    android:id="@+id/guideline"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    app:layout_constraintGuide_percent="0.5"
    android:orientation="vertical" />

<Button
    android:id="@+id/button1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Button 1"
    app:layout_constraintRight_toLeftOf="@id/guideline" />

<Button
    android:id="@+id/button2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Button 2"
    app:layout_constraintLeft_toRightOf="@id/guideline" />

이렇게 하면 가로 방향으로 두 개의 버튼이 guideline을 기준으로 자동으로 정렬되고 크기가 조절됩니다.

ConstraintLayout을 사용하여 아이템의 크기를 조절하는 방법에 대한 간단한 소개였습니다. 다양한 속성을 활용하여 UI를 원하는 대로 지정할 수 있습니다.

더 자세한 내용은 Android Developers 공식 사이트를 참조하세요.