본문 바로가기
안드로이드

안드로이드 - Table Layout

by minsol Kim 2022. 2. 15.

테이블 레이아웃 뷰를 격자형태로 배치해 여러개의 뷰를 동일한 크기로 배치할 때 유용하게 쓰인다.

격자의 바깥쪽 가장자리에 해당하는 부분을 TableLayout 태그로 감싼다. 테이블에 들어가는 한 줄에 해당하는 

row는 TableRow 태그로 감싼다. TableRow 아래에 view 위젯을 선언하여 열을 추가한다. 

테이블 레이아웃의 전체너비를 고르게 사용할 수 있도록 만든것이 stretchColumns 속성이다. 

android:stretchColumns -> 늘이고자 하는 열의 인덱스 지정한다.

열의 인덱스는 0부터 시작

하나이상의 열을 ,(콤마)로 분리하여 지정한다.

android:stretchColumns ="*"  //모든 열을 지정하려면 *기호를 사용한다. 

android:layout_span = '3' //셀을 합칠 때 사용한다. 3개의 셀을 합친다는 말이다. 

<TableLayout
    android:id="@+id/tableLayout"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@drawable/table_outside"
    android:stretchColumns="*"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    app:layout_constraintVertical_bias="0.58000004"
    tools:layout_editor_absoluteX="8dp">

    <TableRow
        android:layout_width="match_parent"
        android:layout_height="match_parent">


        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_span="3"
            android:background="@drawable/table_inside_blue"
            android:gravity="center"
            android:text="2020학년도 1학기"
            android:textSize="25sp" />


    </TableRow>

댓글