- app:layout_constraintTop_toTopOf="parent"
parent 속성으로 ConstraintLayout에 배치된 뷰는 최 상단을 기준으로 배치.
간단하게 자석처럼 최상단에 View을 붙는다.
예1 ) 모든 방향을 적용하면
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
android:layout_width="wrap_content", android:layout_height="wrap_content"
크기로 배치된 View는 레이아웃 가운데 위치하게 된다.
android:layout_width="0dp", android:layout_height="0dp"
크기로 배치된 View는 레이아웃 모든 영역에서 레이아웃을 잡아 당겨지므로 전체화면으로 표시된다.
예2 ) 다른 레이아웃에 붙일 경우
app:layout_constraintTop_toBottomOf="@id/textView"
> 간단하게 Android 텍스트 뷰를 textView 아래 배치 하는 의미.
자주 사용하는 속성은
app:layout_constraintTop_toBottomOf="@id/textView"
app:layout_constraintLeft_toRightOf="@id/textView"
app:layout_constraintRight_toLeftOf="@id/textView"
app:layout_constraintBottom_toTopOf="@id/textView"
- 레이아웃 샘플코드
<?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/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#d1c4e9"
android:gravity="center"
android:text="Hello World!"
android:textSize="30dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#d1c4e9"
android:gravity="center"
android:text="Android"
android:layout_marginTop="8dp"
android:textSize="30dp"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="@id/textView" />
</androidx.constraintlayout.widget.ConstraintLayout>
2. layout_constraintDimensionRatio
레이아웃을 16:9, 1:1, 4:3 형태로 배치, 화면비.
예) 16:9 배치
android:layout_width="match_parent"
android:layout_height="0dp"
app:layout_constraintDimensionRatio="16:9"
가로가 match_parent 크기 일 때 세로는 0dp 크기, Ratio 값의 영향으로 16:9로 배치.
가로가 160dp라면 세로는 90dp로 배치된다.
주의1 : 가로 세로 크기가 모두 고정된 값으로 지정된 경우 ratio는 적용되지 않는다.
주의2 : 1번의 layout_constraint을 적용하지 않으면 경고창이 표시된다.
This view is not constrained. It only has designtime positions, so it will jump to (0,0) at runtime unless you add the constraints less... (Ctrl+F1)
Inspection info:The layout editor allows you to place widgets anywhere on the canvas, and it records the current position with designtime attributes (such as layout_editor_absoluteX). These attributes are not applied at runtime, so if you push your layout on a device, the widgets may appear in a different location than shown in the editor. To fix this, make sure a widget has both horizontal and vertical constraints by dragging from the edge connections. Issue id: MissingConstraints
- 레이아웃 샘플코드
<?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/textView"
android:layout_width="160dp"
android:layout_height="0dp"
app:layout_constraintDimensionRatio="16:9"
android:background="#d1c4e9"
android:gravity="center"
android:text="Hello World!"
android:textSize="30dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
3. 레이아웃 예제
- XML
<?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">
<androidx.appcompat.widget.AppCompatTextView
android:id="@+id/topImage"
android:layout_width="match_parent"
android:layout_height="0dp"
android:background="#d1c4e9"
android:gravity="center"
android:text="16:9"
app:autoSizeTextType="uniform"
app:layout_constraintDimensionRatio="16:9"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<androidx.constraintlayout.widget.ConstraintLayout
android:id="@+id/userPanel"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="@id/topImage">
<androidx.appcompat.widget.AppCompatTextView
android:id="@+id/userImage"
android:layout_width="0dp"
android:layout_height="100dp"
android:background="@android:color/holo_blue_dark"
android:gravity="center"
android:text="1:1"
app:autoSizeTextType="uniform"
app:layout_constraintDimensionRatio="1:1"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<LinearLayout
android:layout_width="0dp"
android:layout_height="0dp"
android:orientation="vertical"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toRightOf="@id/userImage"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent">
<androidx.appcompat.widget.AppCompatTextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:padding="12dp"
android:text="UserId, autoSizeText"
android:textColor="@android:color/black"
app:autoSizeTextType="uniform" />
<androidx.appcompat.widget.AppCompatTextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:padding="8dp"
android:text="UserName, autoSizeText"
app:autoSizeTextType="uniform" />
</LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
<androidx.appcompat.widget.AppCompatTextView
android:layout_width="match_parent"
android:layout_height="0dp"
android:background="@android:color/holo_green_dark"
android:gravity="center"
android:text="Text Field"
android:textSize="30dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="@id/userPanel" />
</androidx.constraintlayout.widget.ConstraintLayout>