본문 바로가기
Programming/Android

ConstaintLayout 내용 정리

by 코딩의성지 2021. 7. 19.

오늘은 안드로이드 프로젝트를 만들때 가장 기본적으로 만들어지는 ConstraintLayout에 대해 정리를 해 두고자 한다.

 

ConstarintLayout 의 경우 RelativeLayout과 비슷하게 동작하는데 부모나 다른 view와의 관계 설정을 통해 UI를 배치할 수 있다.

 

이러한 배치를 하기 위해 제약 조건을 이용해 배치를 할 수 있는데 이때 두가지를 사용할 수가 있다.

 

1) 실선 제약 조건  : 부모나 다른 view와의 관계설정을 실제 좌표값을 이용해서 설정하는 방법임

    <Button
        android:id="@+id/my_btn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="100dp"
        android:layout_marginTop="100dp"
        android:text="Button"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

 

2) 스프링 제약조건 방법 : 부모나 다른 view와의 관계설정을 실제 비율을 이용해서 설정하는 방법임

<Button
        android:id="@+id/my_btn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Button"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.15"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.08" />

위처럼 이렇게 비율을 가지고 UI 작업을 해주면 만약에 화면을 회전하거나 할때 알아서 뷰를 찾아서 배치를 해주는 큰 장접이 있다.

 

그리고 이런식으로 다른 뷰와의 관계를 맺어서 배치도 가능하다.

<Button
        android:id="@+id/my_btn2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="88dp"
        android:text="Button"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.283"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/" />

 

끝.

반응형

댓글