안드로이드 스튜디오
[Android] GridLayout
냠냠쿠
2025. 6. 11. 09:28
728x90
GridLayout
- 그리드를 설정하여 뷰를 배치하는 레이아웃
- TableLayout을 보완하기 위해 제공되는 레이아웃
- rowCount : 그리드 레이아웃 줄의 개수
- columncount : 그리드 레이아웃의 칸의 개수
지정한 rowCount와 columncount를 넘으면 Error 발생하므로 유의
- Table layout과 비슷 해보이지만 다른 부분이 있다.
Table layout과 다른점
- layout_column : 뷰가 배치될 칸의 위치 (0부터 시작)
- layout_row : 뷰가 배치될 줄의 위치 (0부터 시작)
* 똑같은 layout_column, layout_row 가 있으면 겹쳐진다
- layout_columnSpan : 뷰가 차지할 칸의 수
- layout_rowSpan : 뷰가 차지할 줄의 수
* wrap_content로 설정 되어 있는 경우 layout_columnWeight/layout_columnHeight 가 설정 되어 있어야 적용 됨
- layout_columnWeight : 남은 공간을 차지할 가로 비율
- layout_columnHeight : 남은 공간을 차지할 세로 비율
<?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:id="@+id/main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity" >
<androidx.gridlayout.widget.GridLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
app:columnCount="4"
app:rowCount="4">
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button"
app:layout_column="0"
app:layout_columnWeight="1"
app:layout_row="0"
app:layout_rowWeight="1" />
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button"
app:layout_column="1"
app:layout_columnWeight="1"
app:layout_row="0"
app:layout_rowWeight="1" />
<Button
android:id="@+id/button3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button"
app:layout_column="2"
app:layout_columnWeight="1"
app:layout_row="0"
app:layout_rowWeight="1" />
<Button
android:id="@+id/button4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button"
app:layout_column="3"
app:layout_columnWeight="1"
app:layout_row="0"
app:layout_rowSpan="2"
app:layout_rowWeight="1" />
<Button
android:id="@+id/button6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button"
app:layout_column="0"
app:layout_columnWeight="1"
app:layout_row="1"
app:layout_rowWeight="1" />
<Button
android:id="@+id/button7"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button"
app:layout_column="1"
app:layout_columnWeight="1"
app:layout_row="1"
app:layout_rowWeight="1" />
<Button
android:id="@+id/button8"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button"
app:layout_column="2"
app:layout_columnWeight="1"
app:layout_row="1"
app:layout_rowWeight="1" />
<Button
android:id="@+id/button9"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button"
app:layout_column="0"
app:layout_columnSpan="2"
app:layout_columnWeight="1"
app:layout_row="2"
app:layout_rowWeight="1" />
<Button
android:id="@+id/button10"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button"
app:layout_column="2"
app:layout_columnWeight="1"
app:layout_row="2"
app:layout_rowWeight="1" />
</androidx.gridlayout.widget.GridLayout>
</LinearLayout>
728x90