마우스로 텍스트 뷰의 왼쪽 동그라미 버튼을 끌어서 이미지 뷰에 놓으면 이미지 뷰의 어느쪽에 제약을 지정할 것인지 선택하는 메뉴가 나온다. 여기선 이미지 뷰 오른쪽(end)에 여백을 두고 텍스트 뷰를 지정하고 싶으므로 Start to imageView end를 선택하면 된다.
실제로 날짜 텍스트 뷰에는 아무런 제약조건을 설정하지 않고, layout_width,height를 설정하지 않고 wrap_content를 설정했기 때문에 화면에 완전히 붙어있다.
만약 마우스로 위치를 옮기거나 constraint widget에서 여백을 설정하면 당연히 바뀌긴 할 것이다.
<?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">
<!-- 여기에 뷰 추가 -->
<ImageView
android:id="@+id/imageView"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_marginStart="16dp"
android:layout_marginTop="16dp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:srcCompat="@drawable/kakaotalk" />
<TextView
android:id="@+id/titleView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginBottom="29dp"
android:text="카카오톡"
app:layout_constraintBottom_toBottomOf="@+id/imageView"
app:layout_constraintStart_toEndOf="@+id/imageView" />
<TextView
android:id="@+id/messageView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:text="[기기 로그인 알림]"
app:layout_constraintBottom_toBottomOf="@+id/imageView"
app:layout_constraintStart_toEndOf="@+id/imageView" />
<TextView
android:id="@+id/dateView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:layout_marginEnd="16dp"
android:text="9월 7일"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
GUI로 설정했는데 자동으로 코드가 완성되었다 hurray~
예시:
[ImageView] [CurrentView] // Start to imageView end
[ImageView CurrentView] // Start to imageView start
start to imageView end로만 설정했더니 "Missing Constraints in ConstraintLayout"에러가 발생했다.
ConstraintLayout에서 뷰를 올바르게 배치하려면 수평(horizontal)과 수직(vertical) 방향 모두에 대해 제약 조건을 설정해야 한다.
"Missing Constraints in ConstraintLayout" 오류가 발생한 이유:
일반적으로 뷰의 완전한 배치를 위해서는 다음과 같은 제약 조건 조합이 필요하다.
ConstraintLayout 개요
Constraint 핸들(동그라미 버튼)의 용도
Start to imageView start와 end의 차이
제약 조건 설정 시 가로와 세로 모두 설정 필요