Android ConstraintLayout 비율 설정하기 (layout_constraintDimensionRatio 사용법)

✨ 개요

Android의 ConstraintLayout은 강력한 제약 기반 레이아웃 시스템을 제공합니다.
그중 layout_constraintDimensionRatio 속성은 뷰의 가로:세로 비율을 설정하는 데 사용되며, 이미지, 카드뷰, 박스형 UI 등에 자주 활용됩니다.


1. ✅ 속성 구조

1.1 기본 사용법

app:layout_constraintDimensionRatio="1:1" 
app:layout_constraintDimensionRatio="W,1:2" 
app:layout_constraintDimensionRatio="H:3:1" 

2. ✅ 예시

2.1 📌 정사각형 박스 만들기

<androidx.constraintlayout.widget.ConstraintLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <View
        android:id="@+id/square"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:background="#FF8888"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintDimensionRatio="1:1" />
        
</androidx.constraintlayout.widget.ConstraintLayout>

2.2. 📌 방향 지정 예시 (W, H)

2.2.1 가로 기준으로 세로 2배 (W,1:2)

<View
    android:id="@+id/square"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:background="#FF8888"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    app:layout_constraintDimensionRatio="W,1:2" />

2.2.2 세로 기준으로 가로 3배 (H,3:1)

<View
    android:id="@+id/square"
    android:layout_width="wrap_content"
    android:layout_height="0dp"
    android:background="#FF8888"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    app:layout_constraintDimensionRatio="H,3:1" />

3.✅ 주의사항


4.🧠 결론



Related Posts