본문 바로가기
android

화면 크기에 상관없이 View 비율 일정하게 하기

by arirang_ 2023. 1. 13.

1. LinearLayout 사용

 

맞추고 싶은 비율이 vertical인지 horizontal인지 선택

vertical일 경우: android:layout_height = 0으로 두고 android:layout_weight = 1 이런식으로 비율 맞추기

(세로로 view 비율이 맞춰진다)

horizontal일 경우: android:layout_weitght=0으로 두고 android:layout_weight = 1 이런식으로 비율 맞추기

(가로로 view 비율이 맞춰진다)

 

2. ConstraintLayout 사용

예를 들어 디자인 부분에 버튼 크기가 24dp가 되어있었다고 치자. 따라서 버튼의 weight와 height 값을 설정하고 margin 값을 주면 화면의 크기에 따라 버튼의 위치가 조금씩 달라진다. 이런 부분을 막기 위해서,

 app:layout_constraintStart_toStartOf="parent"
 app:layout_constraintEnd_toEndOf="parent"

이런식으로 constraint를 넣어주고, margin 값으로 크기를 조정하면 어떤 화면 크기든간에 자연스럽게 비율이 맞춰진다!!