본문 바로가기
Android

[안드로이드/Layout] weightSum, layout_weight 화면 분배

by nanee_ 2021. 12. 16.
728x90
반응형
SMALL

화면을 분배해서 같은 비율로 분배하는 방법

 

부모 요소가 사용하는 속성

weightSum = "부모 요소에서 자식에게 나누어줄 공간의 갯수"

 

자식 요소가 사용하는 속성

layout_weight = "부모 요소에서 자식이 차지할 공간의 갯수"

 


 

 

<LinearLayout> 을 부모 요소로 잡고

orientation="horizontal" 수평정렬로 잡고

weightSum="3" 3칸의 공간을 만들겠다고 설정한다.

 

그 안에 <TextView> 를 자식 요소로 잡고

layout_weight="1" 씩 주고

layout_width를 match_parent로 주거나, 0dp로 주면 부모요소에서 분배된 비율만큼 부여된다

 

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal"
    android:weightSum="3">

    <TextView
        android:layout_weight="1"
        android:background="#FF0000"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

    <TextView
        android:layout_weight="1"
        android:background="#FF5100"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

    <TextView
        android:layout_weight="1"
        android:background="#FFD900"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

</LinearLayout>

 

좀 더 확장해서 만들어보자면

부모요소를 <LinearLayout>로 또 감싸서 부모요소로 만들고

weightSum="3"

orientation="vertical" 수직방향

이렇게 설정을 해 주고

 

자식요소인 <LinearLayout> 3개에

layout_weight="1" 

설정을 해 준다면

 

아래와 같이 된다!!

 

 

 

 

 

 

 

 

728x90
반응형
LIST