투케이2K

794. (Android/Xml) [레이아웃] GridLayout 그리드 레이아웃 사용해 컴포넌트 배치 방법 본문

Android

794. (Android/Xml) [레이아웃] GridLayout 그리드 레이아웃 사용해 컴포넌트 배치 방법

투케이2K 2024. 5. 9. 20:42

[개발 환경 설정]

개발 툴 : AndroidStudio

개발 언어 : Java / Kotlin

 

[소스 코드]

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:focusable="true"
    android:focusableInTouchMode="true"
    android:orientation="vertical">

    <GridLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="#2a222222"
        android:columnCount="3">

        <!-- ============================================================== -->

        <!-- [GridLayout 속성 설명]
            1 : android:columnCount = 부모 그리드 레이아웃에서 생성하려는 컬럼 개수 (세로)
            2 : android:layout_rowWeight = 자식 컴포넌트가 가질 row (가로) 크기
            2 : android:layout_columnWeight = 자식 컴포넌트가 가질 column (세로) 크기
        -->

        <!-- ============================================================== -->

        <TextView
            android:id="@+id/view1"
            android:layout_rowWeight="1"
            android:layout_columnWeight="1"
            android:background="#ff00ff"
            android:text="view1"
            android:gravity="center"
            android:textColor="#000000"
            android:textStyle="bold"/>

        <TextView
            android:id="@+id/view2"
            android:layout_rowWeight="1"
            android:layout_columnWeight="1"
            android:background="#77aa00"
            android:text="view2"
            android:gravity="center"
            android:textColor="#000000"
            android:textStyle="bold"/>

        <TextView
            android:id="@+id/view3"
            android:layout_rowWeight="1"
            android:layout_columnWeight="1"
            android:background="#FF7AA0"
            android:text="view3"
            android:gravity="center"
            android:textColor="#000000"
            android:textStyle="bold"/>

        <!-- ============================================================== -->

        <TextView
            android:id="@+id/view4"
            android:layout_row="2"
            android:layout_rowSpan="2"
            android:layout_rowWeight="1"
            android:layout_columnWeight="1"
            android:background="#FFE080"
            android:text="view4"
            android:gravity="center"
            android:textColor="#000000"
            android:textStyle="bold"/>

        <TextView
            android:id="@+id/view5"
            android:layout_rowWeight="1"
            android:layout_column="1"
            android:layout_columnSpan="2"
            android:layout_columnWeight="1"
            android:background="#7CF1D1"
            android:text="view5"
            android:gravity="center"
            android:textColor="#000000"
            android:textStyle="bold"/>

        <TextView
            android:id="@+id/view6"
            android:layout_row="4"
            android:layout_rowWeight="1"
            android:layout_column="1"
            android:layout_columnSpan="2"
            android:layout_columnWeight="1"
            android:background="#CBE886"
            android:text="view6"
            android:gravity="center"
            android:textColor="#000000"
            android:textStyle="bold"/>

        <!-- ============================================================== -->

        <TextView
            android:id="@+id/view7"
            android:layout_rowWeight="1"
            android:layout_columnWeight="1"
            android:background="#999999"
            android:text="view7"
            android:gravity="center"
            android:textColor="#000000"
            android:textStyle="bold"/>

        <TextView
            android:id="@+id/view8"
            android:layout_rowWeight="1"
            android:layout_columnWeight="1"
            android:background="#a015bb"
            android:text="view8"
            android:gravity="center"
            android:textColor="#000000"
            android:textStyle="bold"/>

        <TextView
            android:id="@+id/view9"
            android:layout_rowWeight="1"
            android:layout_columnWeight="1"
            android:background="#113366"
            android:text="view9"
            android:gravity="center"
            android:textColor="#000000"
            android:textStyle="bold"/>

        <!-- ============================================================== -->

    </GridLayout>

</LinearLayout>

[결과 출력]

 

 

반응형
Comments