import android.app.Activity;
import android.os.Bundle;
import android.widget.GridView;
import android.widget.SimpleAdapter;
public class GridActivity extends Activity {
private GridView grid_test;
private List<Map<String,Object>> dataList;
private SimpleAdapter simpleAdapter;
private int[] img = {R.drawable.ic_launcher,R.drawable.ic_launcher,R.drawable.ic_launcher, R.drawable.ic_launcher,
R.drawable.ic_launcher,R.drawable.ic_launcher,R.drawable.ic_launcher,R.drawable.ic_launcher,
R.drawable.ic_launcher,R.drawable.ic_launcher,R.drawable.ic_launcher,R.drawable.ic_launcher,
R.drawable.ic_launcher,R.drawable.ic_launcher,R.drawable.ic_launcher,R.drawable.ic_launcher,
R.drawable.ic_launcher,R.drawable.ic_launcher,R.drawable.ic_launcher,R.drawable.ic_launcher,
R.drawable.ic_launcher,R.drawable.ic_launcher,R.drawable.ic_launcher,R.drawable.ic_launcher};
private String[] imgName = {"1","2","3","4",
"5","6","7","8",
"9","10","11","12",
"13","14","15","16",
"17","18","19","20",
"21","22","23","24"};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_grid);
grid_test = (GridView) findViewById(R.id.grid_test);
dataList = new ArrayList<Map<String, Object>>();
simpleAdapter = new SimpleAdapter(this,getData(),R.layout.grid_item,
new String[] {"img","txt"},new int[]{R.id.img_item,R.id.txt_item});
grid_test.setAdapter(simpleAdapter);
}
private List<Map<String,Object>> getData() {
for (int i=0; i<img.length; i++) {
Map<String,Object> map = new HashMap<String, Object>();
map.put("img", img[i]);
map.put("txt", imgName[i]);
dataList.add(map);
}
return dataList;
}
}
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<GridView
android:id="@+id/grid_test"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:columnWidth="80dp"
android:padding="10dp"
android:numColumns="3"
android:text="@string/hello_world" />
</LinearLayout>
<?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:orientation="vertical" >
<ImageView
android:id="@+id/img_item"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"/>
<TextView
android:id="@+id/txt_item"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"/>
</LinearLayout>