北京网站优化厂家,总结什么是网络营销,网络优化的意义,现在做个网站大概多少钱GridView跟ListView都是比较常用的多控件布局#xff0c;而GridView更是实现九宫图的首选!本文就是介绍如何使用GridView实现九宫图。GridView的用法很多#xff0c;网上介绍最多的方法就是自己实现一个ImageAdapter继承BaseAdapter#xff0c;再供GridView使用#xff0c;… GridView跟ListView都是比较常用的多控件布局而GridView更是实现九宫图的首选!本文就是介绍如何使用GridView实现九宫图。GridView的用法很多网上介绍最多的方法就是自己实现一个ImageAdapter继承BaseAdapter再供GridView使用类似这种的方法本文不再重复本文介绍的GridView用法跟前文ListView的极其类似。。。。也算是我偷懒一下嘻嘻嘻嘻。。。。 先来贴出本文代码运行的结果 本文需要添加/修改3个文件main.xml、night_item.xml、JAVA源代码。 main.xml源代码如下本身是个GirdView用于装载Item ?xml version1.0 encodingutf-8? GridView xmlns:androidhttp://schemas.android.com/apk/res/android android:idid/gridview android:layout_widthfill_parent android:layout_heightfill_parent android:numColumnsauto_fit android:verticalSpacing10dp android:horizontalSpacing10dp android:columnWidth90dp android:stretchModecolumnWidth android:gravitycenter / 介绍一下里面的某些属性 android:numColumnsauto_fit GridView的列数设置为自动 android:columnWidth90dp每列的宽度也就是Item的宽度android:stretchModecolumnWidth缩放与列宽大小同步android:verticalSpacing10dp两行之间的边距如行一(NO.0~NO.2)与行二(NO.3~NO.5)间距为10dpandroid:horizontalSpacing10dp两列之间的边距。 接下来介绍 night_item.xml这个XML跟前面ListView的ImageItem.xml很类似 ?xml version1.0 encodingutf-8? RelativeLayout xmlns:androidhttp://schemas.android.com/apk/res/android android:layout_heightwrap_content android:paddingBottom4dip android:layout_widthfill_parent ImageView android:layout_heightwrap_content android:idid/ItemImage android:layout_widthwrap_content android:layout_centerHorizontaltrue /ImageView TextView android:layout_widthwrap_content android:layout_belowid/ItemImage android:layout_heightwrap_content android:textTextView01 android:layout_centerHorizontaltrue android:idid/ItemText /TextView /RelativeLayout 最后就是JAVA的源代码了也跟前面的ListView的JAVA源代码很类似不过多了“选中”的事件处理 public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); GridView gridview (GridView) findViewById(R.id.gridview); //生成动态数组并且转入数据 ArrayListHashMapString, Object lstImageItem new ArrayListHashMapString, Object(); for(int i0;i10;i) { HashMapString, Object map new HashMapString, Object(); map.put(ItemImage, R.drawable.icon);//添加图像资源的ID map.put(ItemText, NO.String.valueOf(i));//按序号做ItemText lstImageItem.add(map); } //生成适配器的ImageItem 动态数组的元素两者一一对应 SimpleAdapter saImageItems new SimpleAdapter(this, //没什么解释 lstImageItem,//数据来源 R.layout.night_item,//night_item的XML实现 //动态数组与ImageItem对应的子项 new String[] {ItemImage,ItemText}, //ImageItem的XML文件里面的一个ImageView,两个TextView ID new int[] {R.id.ItemImage,R.id.ItemText}); //添加并且显示 gridview.setAdapter(saImageItems); //添加消息处理 gridview.setOnItemClickListener(new ItemClickListener()); } //当AdapterView被单击(触摸屏或者键盘)则返回的Item单击事件 class ItemClickListener implements OnItemClickListener { public void onItemClick(AdapterView? arg0,//The AdapterView where the click happened View arg1,//The view within the AdapterView that was clicked int arg2,//The position of the view in the adapter long arg3//The row id of the item that was clicked ) { //在本例中arg2arg3 HashMapString, Object item(HashMapString, Object) arg0.getItemAtPosition(arg2); //显示所选Item的ItemText setTitle((String)item.get(ItemText)); } } 转载于:https://www.cnblogs.com/xyzlmn/archive/2009/11/30/3168339.html