출처: https://3months.tistory.com/307 [Deep Play]

안드로이드/연습

[Android] xml layout_weight

코딩하는 랄뚜기 2021. 8. 11. 20:00
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".MainActivity">
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:gravity="center"
        android:layout_weight="33"
        android:orientation="vertical">
        <Button
            android:layout_height="wrap_content"
            android:layout_width="wrap_content"
            android:text="버튼1">
        </Button>
        <Button
            android:layout_height="wrap_content"
            android:layout_width="wrap_content"
            android:text="버튼2">
        </Button>
    </LinearLayout>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:gravity="center"
        android:background="#00ff00"
        android:layout_weight="33"
        android:orientation="horizontal">
        <Button
            android:layout_height="wrap_content"
            android:layout_width="wrap_content"
            android:text="버튼3">
        </Button>
        <Button
            android:layout_height="wrap_content"
            android:layout_width="wrap_content"
            android:text="버튼4">
        </Button>
    </LinearLayout>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:gravity="center"
        android:background="#0000ff"
        android:layout_weight="33"
        android:orientation="vertical">
        <Button
            android:layout_height="wrap_content"
            android:layout_width="wrap_content"
            android:text="버튼5">
        </Button>
        <Button
            android:layout_height="wrap_content"
            android:layout_width="wrap_content"
            android:text="버튼6">
        </Button>
    </LinearLayout>

</LinearLayout>

layout_weight는 중복 레이아웃을 사용할 때 layout을 화면에 꽉차게 하고 싶을 때 사용된다. dp를 이용하지 않고 상대적인 비율 개념으로 들어가기 때문에 기기의 크기가 다르더라도 화면을 꽉차게 해서 출력해 줄 수 있다. 주의 할 점은 꼭 layout_height를 0으로 해줘야 한다는 것이다.

 

실행화면