본문으로 바로가기

[Android] Infer Constraints가 뭘까?

category Study/Android 2019. 6. 25. 20:21
반응형
일단 Infer Constraints은 레이아웃 안에 있는 모든 위젯들에 관계를 맺어주는 기능이다.
이렇게 말하면 잘 알아듣기 힘들다.. 나도 그렇고..

이전 안드로이드 스튜디오가 업데이트될 때 새로 생긴 레이아웃이 있었다.
이름하야 ConstraintLayout..!


좀 더 유연하고 강력하다고 하는데.. 나는 이전 레이아웃을 써본 적이 없어서 모르겠다.

ConstraintLayout은 위젯이 연결되어 있지않으면 오류를 뱉는다. 그래서 위젯을 정렬시켜주면 오류가 발생하지 않는다.
이 정렬되지 않은 위젯들을 자동으로 정렬해주는 기능이 바로 Infer Constraints이다.

=== 추가 설명 ===

아래 사진은 디자이너 화면과 XML 코드이다.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Button1"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.145"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.374" />
 
    <Button
        android:id="@+id/button2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="235dp"
        android:layout_marginEnd="172dp"
        android:layout_marginRight="172dp"
        android:layout_marginBottom="392dp"
        android:text="Button2"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toEndOf="@+id/button1"
        app:layout_constraintTop_toTopOf="parent" />
 
    <Button
        android:id="@+id/button3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="button3"
        tools:layout_editor_absoluteX="47dp"
        tools:layout_editor_absoluteY="168dp" />
cs

Button1과 Button2는 Layout탭에서 따로 위치를 정해주었기 때문에 XML파일에 위치가 정의되어 있지만, Button3는 생성만 했기 때문에 위치가 정의되지 않았다.

즉 겉보기론 위치가 정해진 것처럼 보이지만, 실제론 위치가 정의되지 않아 에러를 뿜어내는 것이다.

이 위치를 자동으로 정해주는 것이 Infer Constraints이다.


반응형

'Study > Android' 카테고리의 다른 글

[Android] missing constraints in constraintlayout 해결과 이유  (3) 2019.06.25