Android中GridView解析
1,GridView是Android中的网络视图控件,按照行列的形式来显示内容,一般用于图片、图形的显示,比如实现九宫格图,用GridView是首选,也是最简单的。GridView也可以像ListView一样,以列表的形式来显示内容。
2,GridView关键属性:
a) android:numColumns=" " 每行显示几列
b) android:horizonntalSpacing=" " 设置水平之间的间距
c) android:verticalSpacing=" " 设置垂直之间的间距
d) android:columnWidth=" " GridView中每一列的宽度
3,需要两个xml视图布局,一个用作总布局文件(main.xml),一个用作每一个列表项所所对应的布局文件(item.xml):
a,总布局文件main.xml中的代码:
<span style="font-size:18px;"><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="match_parent"android:layout_height="match_parent"android:orientation="vertical" ><GridViewandroid:id="@+id/gridView"android:layout_width="match_parent"android:layout_height="wrap_content"android:layout_marginTop="10dp"android:numColumns="3" android:horizontalSpacing="10dp"android:verticalSpacing="10dp" ></GridView></LinearLayout></span>
b,每一个列表项所所对应的布局文件item.xml中的代码:
<span style="font-size:18px;"><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="match_parent"android:layout_height="match_parent"android:orientation="vertical" android:gravity="center"android:background="#000000" ><ImageViewandroid:id="@+id/image"android:layout_width="70dp"android:layout_height="70dp"android:src="@drawable/ic_launcher"/> <TextView android:id="@+id/text"android:layout_width="wrap_content"android:layout_height="wrap_content"android:textColor="#ffffff"android:layout_marginTop="5dp"android:text="演示"/></LinearLayout></span>
4,java实现类中的代码:
<span style="font-size:18px;">public class MainActivity extends Activity implements OnItemClickListener {private GridView grideView;private List<Map<String, Object>> dataList;private SimpleAdapter adapter;// 把图片资源封装在数组中private int[] icon = { R.drawable.address, R.drawable.camera,R.drawable.clock, R.drawable.games, R.drawable.messenger,R.drawable.ringtone, R.drawable.youtube, R.drawable.weather,R.drawable.world, R.drawable.logo, R.drawable.shezhi,R.drawable.friends };// 同样把图片对应的名字封装到数组中,和上面的图片一一对应private String[] iconName = { "电子邮件", "相机", "时钟", "游戏", "拨号", "音乐", "视频","天气", "浏览器", "无线上网", "设置", "微聊" };@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.main);// 这里不能设置(不知什么原因,程序崩溃):requestWindowFeature(Window.FEATURE_NO_TITLE);grideView = (GridView) findViewById(R.id.gridView);/** 1,准备数据源 2,新建适配器(SimpleAdapter) 3,GridView加载适配器* 4,GridView配置事件监听器(OnItemClickListener)*/dataList = new ArrayList<Map<String, Object>>();// 关于适配器(SimpleAdapter)的知识在上一篇(ListView)中已详细分析,这里不再冗余adapter = new SimpleAdapter(this, getData(), R.layout.item,new String[] { "image", "text" }, new int[] { R.id.image,R.id.text });grideView.setAdapter(adapter);// 设置监听事件grideView.setOnItemClickListener(this);}// 通过getData方法来获取数据源private List<Map<String, Object>> getData() {for (int i = 0; i < icon.length; i++) {Map<String, Object> map = new HashMap<String, Object>();map.put("image", icon[i]);map.put("text", iconName[i]);dataList.add(map);}return dataList;}@Overridepublic void onItemClick(AdapterView<?> parent, View view, int position,long id) {// TODO Auto-generated method stubToast.makeText(this, "你点击的是" + iconName[position], Toast.LENGTH_SHORT).show();}
}</span>
5,实现效果: