안드로이드 스튜디오/Java

안드로이드 스튜디오 : 레이아웃 (Constraint Layout 기본)

류창 2021. 10. 8. 17:09
반응형

 

기존에는 LinearLayout 과 RelativeLayout이 주로 사용되었지만, 

업그레이드 버전인 ConstraintLayout이 새로 나왔다.

 

 

ConstraintLayout은 무엇일까?

 

infer Conatraint 버튼

위젯들의 위치를 자동으로 맞춰주는 기능이 infer Constraint다.

 

즉, 위젯들의 위치를 상대적으로 맞춰주는 레이아웃이 ConstraintLayout이다.

 

ConstraintLayout 기본적인 위치 설정 메소드

• 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

 

이 메소드들이 위젯들의 위치를 설정해준다.

 

Left,Right,Top,Bottom을 부모클래스 위젯으로 맞추었다. 

 

버튼 위젯의 왼쪽을  부모의 왼쪽으로, 오른쪽을 부모의 오른쪽으로 맞추면,

가운데 정렬이일어난다

 

같은 원리로, 버튼의 Top을 부모의 Top으로,  Bottom을 부모의 Botoom으로 맞추면,

세로 기준으로 가운데 정렬이 일어납니다.

 

이런 현상이 일어나는이유는, 왼쪽에서 당기는 힘과 오른쪽에서 당기는힘이 합쳐져

가운데로 위치가 설정되기 때문입니다.

 

ConstraintLayout 여백(Margin) 설정 메소드

 

• android:layout_marginStart
• android:layout_marginEnd
• android:layout_marginLeft
• android:layout_marginTop
• android:layout_marginRight
• android:layout_marginBottom

 

레이아웃의 여백을 맞춰주는 메소드입니다. 이 메소드를통해 위젯들의 간격을 맞춰줄수있습니다.

 

1번 을기준으로 margin_left 30dp로 위치를 설정

 

 

ConstraintLayout 원형배치 설정 메소드

 

• app:layout_constraintCircle
• app:layout_constraintCircleAngle
• app:layout_constraintCircleRadius

 

위젯을 원형배치하기

app:layout_constraintCircle: 원형배치를할 위젯설정

app:layout_constraintCircleAngle: 각도를 설정

app:layout_constraintCircleRadius: 반지름을 설정

 

이 메소드들은 시계와 같이 원형으로 배치하고 싶을때 사용합니다.

 

원형배치를했는데도, MissingConstraints가 일어납니다. 

StackOverflow를 찾아보았는데, Ignore로 해결을 추천합니다.

 

 

ConstraintLayout 비율로 위치 설정 메소드

• app:layout_constraintVertical_bias
• app:layout_constraintHorizontal_bias

 

가로 3:7 세로 7:3 비율로 위치설정

 

 app:layout_constraintVertical_bias: 세로 비율 설정

 app:layout_constraintHorizontal_bias: 가로 비율 설정

 

이 메소드들은 위젯의 위치를 비율로 설정하고 싶을때 사용합니다.

 

 

 

여기까지 ConstraintLayout의 기본적인 메소드들을 소개했습니다.

 

다음 포스팅엔 더욱 다양한 기능을 알아보도록 하겠습니다. 감사합니다.

반응형