Gridlayout과 Tablelayout 차이점

Jang Seok Woo·2020년 4월 21일
0

Android Studio

목록 보기
7/20

TableLayout과 GridLayout
이 두 레이아웃의 경우, 모두가 행과 열을 사용하여 레이아웃을 분할하는 기능을 한다.
한가지 차이점은 TableLayout은 분할된 column을 통합할 수 있지만 row는 통합이 불가능하다. 이를 가능하게하는 것이 GridLayout이다.

링크텍스트

The first thing that strikes you if you look at the GridLayout API is that it offers similar functionality to TableLayout although it goes about it in a rather different way.
In fact, it is somewhat more like a hybrid of TableLayout and LinearLayout.
Let’s start with a simple example which is similar to what we did when we covered TableLayout:

<TextView
    android:text="1,1" />

<TextView
    android:text="1,2" />

<TextView
    android:text="1,3" />

<TextView
    android:text="1,4" />

<TextView
    android:text="2,1" />

<TextView
    android:text="2,2" />

<TextView
    android:text="2,3" />

<TextView
    android:text="2,4" />

<TextView
    android:text="3,1" />

<TextView
    android:text="3,2" />

<TextView
    android:text="3,3 longer" />

<TextView
    android:text="3,4" />

<TextView
    android:text="4,1" />

<TextView
    android:text="4,2" />

<TextView
    android:text="4,3" />

<TextView
    android:text="4,4" />

gridvstable1

Normally gravity is used to align the content within a control, but GridLayout uses this to determine the size of cells.

<TextView
    android:layout_gravity="fill_horizontal"
    android:text="1,3" />



<TextView
    android:layout_gravity="fill_horizontal"
    android:text="2,3" />



<TextView
    android:layout_gravity="fill_horizontal"
    android:text="3,3 longer" />



<TextView
    android:layout_gravity="fill_horizontal"
    android:text="4,3" />

이렇게 했을 때 결과는.!

girdvstable2

The first thing to point out is that we are not specifying android:layout_width or android:layout_height attributes for any of the TextView widgets within the GridLayout.

This is because GridLayout uses a different layout model to other layouts as we shall see.

첫번째로 우리가 주목해야 할 점은 그리드 레이아웃내에서 우리는 layout_width나 layout_height의 속성값을 지정하지 않는 다는 점이다.
여타 다른 layout과는 다르게. GridLayout은 width,height를 지정하지 않는다.

While this is pretty close to what we got with TableLayout, there is a difference which is highlighted by the longer text in row 3, cell 3: The cells are not automatically stretched to fill the column as they are in TableLayout.

테이블 레이아웃과는 다르게 (3,3)을 보면 늘어나있는데 나머지 (1,3)(2,3)(4,3)은 자동적으로 늘어나 있지 않다.

Normally we would look at android:layout_width in conjunction with android:layout_weight to control to cell sizes, however GridLayout bases it’s layout model upon the android:layout_gravity of the cells.

일반적으로 다른 layout에서는 weight와 width를 조합해서 사용해 셀의 크기를 조종하는 반면, GridLayout에서는 Gravity를 사용해서 셀의 크기를 조종한다.

This is easy enough to fix, but it demonstrates a key aspect of how GridLayout works, which we see more of later : How GridLayout sizes each cell is dependent on the layout_gravity of both the cell itself and its siblings in the same row or column.
->같은얘기
Let’s demonstrate this by adding android:layout_gravity=”fill_horizontal” to each cell in the third column:
->한번 증명해보자,GridLayout은 gravity로 인해 크기를 조종하는지

<TextView
    android:layout_gravity="fill_horizontal"
    android:text="1,3" />



<TextView
    android:layout_gravity="fill_horizontal"
    android:text="2,3" />



<TextView
    android:layout_gravity="fill_horizontal"
    android:text="3,3 longer" />



<TextView
    android:layout_gravity="fill_horizontal"
    android:text="4,3" />

gridvstable

It is important to remember that GridLayout does still support android:layout_width and android:layout_height, but default to WRAP_CONTENT for both.

GridLayout이 여전히 android:width와 height를 지원한다는 걸 기억해라. (그러나..강조) default 서부터 Wrap_content까지 둘다(width랑 height) 된다.

However, you can get in to all kinds of problems if you start mixing your layout metaphors, so I would strongly advise leaving android:layout_width and android:layout_height set to their defaults, and control cell widths using android:layout_gravity instead.
그러나 여전히 layout의 이해를 하는데 혼란을 가져올 것이다. 내가해줄 조언은 width와 height의 값을 default로 두고, width를 gravity를 이용해 조정해라.

  • 하고자하는 말이 뭐지?

This shows some of the parallels with TableLayout, but what about LinearLayout?
Similarly to LinearLayout GridLayout supports an android:orientation attribute which controls the order in which GridLayout populates the cells.
Linearlayout과 GridLayout은 비슷하게도 orientation기능이 있다.

The default orientation is horizontal, which means that it fills the cells in the first row with the first n children, the cells in the second row with the next n children, and so on (where n is the number of columns).

If we change this to “vertical” it will fill the first column with the first m children, the second column with the next m children, and so on (where m is the number of rows). So, by simply changing our GridLayout declaration:

gridvstable4

profile
https://github.com/jsw4215

2개의 댓글

comment-user-thumbnail
2020년 10월 23일

Thank you very much for the nice tutorial

1개의 답글