불순분자 Kotlin [10] - LinearLayout

불순분자들·2022년 7월 18일
0

List App 만들기

목록 보기
10/18

Kotiln으로 개발하기 전에 앞서 Layout에 대해서 지식을 배우고 넘어가려고 한다.
우리는 LinearLayout, RelativeLayout, ConstraintLayout, FrmaeLayout으로 4가지의 Layout에 대해서 알아볼 것이다.

LinearLayout

LinearLayout은 선형 레이아웃이라고 말하며, 세로 또는 가로의 단일 방향으로 모든 하위 요소를 정렬하는 뷰 그룹이다.

정렬

LinearLayout은 android:orientation 속성을 사용하여 레이아웃 방향을 지정할 수 있다.

android:orientation="vertical"


위와 같이 vertical 로 선언하면 "세로로 정렬하겠다" 라는 뜻이다.

android:orientation="horizontal"


반대로 horizontal 로 선언하면 "가로로 정렬하겠다"라는 뜻이 된다.

width & height ( 가로와 세로 )

화면에서 위젯으로 얼마만큼 사용할지에 대해 안드로이드에서 width & height 속성을 제공해준다.

android:layout_width="match_parent"
android:layout_height="match_parent"

위와 같은 식으로 사용하는데, match_parent 속성은 화면의 ( 가로 or 세로 )전체를 사용할 것이라는 의미이다.

android:layout_width="wrap_content"
android:layout_height="wrap_content"

반면에 wrap_content"위젯의 크기만큼 감싼다"라는 의미로 해석하면 쉽게 이해할 수 있다.

weight ( 균등 분포 )

하위 요소가 화면에서 동일한 크기의 비율로 공간을 사용하게 하고싶다면, weight를 사용하면 된다.

<TextView
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:text="No1. TextView"
        android:layout_weight="1"/>


사용법은 비교적 간단하게 생각할 수 있다.
가로의 비율을 조정하고 싶다면 width를 0dp로, 세로의 비율을 조정하고 싶다면 height를 0dp로 두고 본인이 원하는 비율값을 weight에 넣어주면 끝이다.

비균등 분포

비균등 분포는 더욱 더 간단하다. 그저 그냥 자신이 원하는 크기를 dp든 px이든 그 값만 넣어주면 끝이다.

<TextView
        android:layout_width="100dp"
        android:layout_height="150dp"
        android:text="No1. TextView" />


하지만, 사용시 주의할 점이 있다.

  • 기기마다 화면의 크기가 다르기 때문에 화면 전체를 사용할 경우 위치가 달라질 수 있다.

또한, 우리는 LinearLayout안에 또 다른 LinearLayout을 넣어서 vertical과 horizontal을 자유롭게 사용할 수 있다는 것을 알아두자.

profile
장래희망 : 침대 위 녹아든 치즈

0개의 댓글