In this tutorial, we will learn how to arrange the components in two-dimensional gridView which is shown as Figure 10.1.
Figure 10.1 |
Same as the previous tutorial, we will show you in two ways (e.g. normal and customize adapter) to build up the gridView layout in Android application.
1. Normal Grid View
In the first part, we would like to display number from 1 to 25 in grid view layout. It is quite simple to implement grid view layout by using <GridView /> tag in XML layout coding (Figure 10.2). There are few settings can be set for the grid view layout such as android:numColumns="..." to set the number of column, android:columnWidth="..." to set the width of the column, android:gravity="..." to set the position of the content and other settings.
<?xml version="1.0" encoding="utf-8"?>
<GridView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/gridView1"
android:numColumns="5"
android:gravity="center"
android:columnWidth="50dp"
android:stretchMode="columnWidth"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
</GridView>
<GridView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/gridView1"
android:numColumns="5"
android:gravity="center"
android:columnWidth="50dp"
android:stretchMode="columnWidth"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
</GridView>
Figure 10.2