当前位置: 首页 > backend >正文

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,实现效果:



http://www.xdnf.cn/news/11462.html

相关文章:

  • perl下载与安装教程【工具使用】
  • perl 5.10.0安装包下载
  • Spring AOP全面详解(超级详细)
  • 网络负载平衡(NLB)详解!
  • 虚拟机-安装与使用(详细教程)
  • 智能dns调研及bind9搭建
  • FlashFXP 4.0注册码key 及教程
  • MFC内存映射文件
  • 软件工程专业值得考的8个证书
  • 使用C#判断字符串中是否包含中文字符
  • 【目标检测】58、目标检测中的正负样本分配策略总结
  • GaussDB数据类型转换介绍
  • pornstars java,Pornstar - QQ音乐-千万正版音乐海量无损曲库新歌热歌天天畅听的高品质音乐平台!...
  • 分享25个适合上班族的副业路子
  • Round Robin 算法
  • 常用的错误码介绍
  • 深入解析数码相机CCD坏点及噪点检测!【图解教程】
  • 容器化之-可视化镜像仓库管理(Harbor+阿里云oss)
  • 扒一扒那些叫欧拉的定理们(四)——平面几何欧拉定理美学鉴赏
  • PrepareStatement用法(附源码解析)
  • ubuntu使用VNC实现远程桌面
  • android drawtext 方法,8.2.13 drawText方法:绘制字符串
  • Windows系统字体优化方案(系统字体替换)
  • AAV相关研究最新进展(2023年5月)
  • 针对centos(Linux)多网卡bond绑定模式及其操作
  • LinearLayout
  • 29 | 分布式高可靠之流量控制:大禹治水,在疏不在堵
  • 【Android OpenGL开发】OpenGL ES与EGL介绍
  • ContentProvider的相关知识总结
  • 新加坡云服务器推荐 - 适合跨境外贸等业务