-
ConstraintLayout 살펴보기안드로이드 2019. 4. 12. 18:24
레이아웃의 끝판왕, ConstraintLayout
RecyclerView가 ListView의 상위 호환인 것처럼
Constraint Layout은 Relative Layout의 상위 호환이라고 할 수 있습니다.
Layout간의 관계를 직관적이고 뷰간의 상대적인 위치로서 나타낼 수 있습니다.
Constraint Layout의 제약조건
Relative Positioning
Margins
Centering Positioning
Circular Positioning
Visibility behavior
Dimension Constraints
Chains
Virtual Helper Objects
Relative Positioning은 다른 위젯과의 상관관계로서 위치를 결정하는 구조입니다.수평의 관계는 left, right, start, end와 같은 상관관계가 있고,
수직의 관계는 top, bottom, text baseline과 같은 상관관계 요소들이 있습니다.
이 요소들을 적절히 사용하여서 뷰들간의 상대적인 위치를 조절할 수 있습니다.
Relative Positioning
위의 예시를 보시면 buttonB에 걸려있는
app:layout_constraintLeft_toRightOf="@+id/buttonA"
B 버튼의 constraintLeft를 A 버튼의 Right에 붙인다는 명령어에 의해 위의 사진과 같이 설정될 수 있습니다.
이처럼 위치는 상대적입니다.
사용하실 수 있는 설정은 다음과 같습니다.
layout_constraintLeft_toLeftOf
layout_constraintLeft_toRightOf
layout_constraintRight_toLeftOf
layout_constraintRight_toRightOf
layout_constraintTop_toTopOf
layout_constraintTop_toBottomOf
layout_constraintBottom_toTopOf
layout_constraintBottom_toBottomOf
layout_constraintBaseline_toBaselineOf
layout_constraintStart_toEndOf
layout_constraintStart_toStartOf
layout_constraintEnd_toStartOf
layout_constraintEnd_toEndOf
Margin
margin의 경우에는 기존의 Relative Layout과 사용법이 같습니다.
android:layout_marginStart
android:layout_marginEnd
android:layout_marginLeft
android:layout_marginTop
android:layout_marginRight
android:layout_marginBottom
Centering positioning
Centering positioning은 불가능한 제약을 처리할 수 있는 Constraint Layout을 이용한 것입니다.
위와 같은 상황이 있다고 해봅시다.
이 상황에서 Button의 수평 사이즈가 parent layout(constraint layout)과 동일하지 않다면 두 제약조건이 동시에 만족하지 않습니다. 이 경우에는 제약조건이 다음과 같이 양쪽에 스프링이 걸린 것 처럼 작동하여서 parent layout의 중앙에 놓이게 되는 것입니다.
Bias
Bias에 대해서 알아보겠습니다. 위의 결과처럼 상반된 두 제약조건은 위젯을 중앙에 배치시키게 됩니다.
하지만, 위의 제약조건을 유지한 상태로 bias의 속성을 이용해 한쪽에 더 가중치를 주게 할 수 있습니다.layout_constraintHorizontal_bias
layout_constraintVertical_bias
세계 최강을 꿈꾸는 안드로이드 Front-End 개발자입니다.
Github 주소: github.com/rkswlrbduf
이전 블로그 주소: blog.naver.com/rkswlrbduf => 블로그 이전중...
'안드로이드' 카테고리의 다른 글
안드로이드 생명주기-Android LifeCycle (0) 2019.04.12 Android Q - RoleManager (0) 2019.04.12 Linked List VS Array List (0) 2019.04.12 RxKotlin + Retrofit2 + GitHub API (0) 2019.04.12 RecyclerView Filter 사용하기 (1) 2019.04.12 댓글