Android ConstraintLayout 예제 자습서
이 튜토리얼에서는 안드로이드 ConstraintLayout의 복잡성에 대해 논의할 것입니다. Google은 Google I/O Conference 2016에서 Android Constraint 레이아웃 편집기를 소개했습니다. 새로운 Layout Editor에는 개발자가 복잡한 레이아웃을 위한 평면 UI 계층을 만들 수 있는 강력한 도구 세트가 있습니다.
안드로이드 ConstraintLayout
Android Constraint 레이아웃 개요
크기 조정 핸들 - 네 모서리에 있으며 보기 크기를 조정하는 데 사용되지만 제약 조건은 그대로 유지됩니다. 측면 손잡이 - 양쪽 중앙에 있는 원형 손잡이입니다. 뷰의 상, 좌, 하, 우 제약 조건을 설정하는 데 사용됩니다. 기준선 핸들 - 기준선을 레이아웃의 다른 텍스트 보기와 정렬하는 데 사용됩니다.
위 레이아웃의 xml 코드는 sample.xml
과 같습니다.
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
xmlns:android="https://schemas.android.com/apk/res/android"
xmlns:app="https://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:text="TextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/textView"
android:layout_marginStart="16dp"
android:layout_marginTop="16dp"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toTopOf="parent"
android:layout_marginLeft="16dp"
android:layout_marginEnd="16dp"
app:layout_constraintRight_toRightOf="parent"
android:layout_marginRight="16dp" />
</android.support.constraint.ConstraintLayout>
app:layout_constraintLeft_toLeftOf=parent
- constraintLeft는 뷰 왼쪽의 기준점입니다. toLeftOf는 이 경우 "부모\인 할당된 보기의 왼쪽에 보기를 정렬하는 것을 의미합니다.\n뷰에 절대 위치가 설정된 경우 사용되는 xml 속성은 -
tools:layout_editor_absoluteY="48dp"
tools:layout_editor_absoluteX="40dp"
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
xmlns:android="https://schemas.android.com/apk/res/android"
xmlns:app="https://schemas.android.com/apk/res-auto"
xmlns:tools="https://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:text="TextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/textView"
android:layout_marginStart="16dp"
app:layout_constraintLeft_toLeftOf="parent"
android:layout_marginLeft="16dp"
android:layout_marginEnd="16dp"
app:layout_constraintRight_toRightOf="parent"
android:layout_marginRight="16dp"
android:layout_marginTop="16dp"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:text="TextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/textView2"
app:layout_constraintBaseline_toBaselineOf="@+id/textView"
android:layout_marginStart="16dp"
app:layout_constraintLeft_toLeftOf="parent"
android:layout_marginLeft="16dp"
app:layout_constraintRight_toLeftOf="@+id/textView"
android:layout_marginEnd="8dp"
android:layout_marginRight="8dp" />
</android.support.constraint.ConstraintLayout>
제약 조건을 자동으로 생성하는 데 사용되는 두 가지 다른 도구 자동 연결 및 추론이 있습니다.
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
xmlns:android="https://schemas.android.com/apk/res/android"
xmlns:app="https://schemas.android.com/apk/res-auto"
xmlns:tools="https://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:srcCompat="@mipmap/ic_launcher"
android:id="@+id/imageView7"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.58000004"
app:layout_constraintHorizontal_bias="0.47" />
</android.support.constraint.ConstraintLayout>
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
xmlns:android="https://schemas.android.com/apk/res/android"
xmlns:app="https://schemas.android.com/apk/res-auto"
xmlns:tools="https://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:text="TextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/textView22"
tools:layout_constraintRight_creator="1"
tools:layout_constraintBottom_creator="1"
app:layout_constraintBottom_toTopOf="@+id/button2"
app:layout_constraintRight_toRightOf="parent"
tools:layout_constraintLeft_creator="1"
android:layout_marginBottom="59dp"
app:layout_constraintLeft_toLeftOf="parent" />
<Button
android:text="Button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/button2"
tools:layout_constraintTop_creator="1"
tools:layout_constraintRight_creator="1"
android:layout_marginEnd="31dp"
app:layout_constraintRight_toRightOf="@+id/textView22"
android:layout_marginTop="178dp"
app:layout_constraintTop_toTopOf="parent"
android:layout_marginRight="31dp" />
</android.support.constraint.ConstraintLayout>
tools:layout_constraintTop_creator=\1\
: 제약 조건을 만든 사람을 추적하기 위한 생성자 속성입니다. 특히 추론 엔진에서 만든 경우 1로 할당됩니다.