GridView一直是一个系统登录后以九宫格方式展现功能子模块的最佳选择,经过试验和网上资料的查阅,现把实现方式总结一下:

一直是通过自定义Adapter方式,在getView()方法中设置图片的显示方式,这种方式资料比较多;

另一种方式是使用Android系统自带的Adapter:

具体步骤如下:

1.新建一个安卓工程:

2.定义一个布局文件,可以命名为gridview.xml,用于显示主界面。

代码如下:

<?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" >
<GridView
android:id="@+id/GridView1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:numColumns="4"
/> </LinearLayout>

3.定义一个项布局文件items.xml,目的是为了展现GridView中具体子项item的显示方式

代码如下:

<?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/image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="10px"
android:scaleType="fitCenter" /> <TextView
android:id="@+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:padding="5px" /> </LinearLayout>

3.好了,定义好了界面,我们就可以写程序代码了~

在主Activity中定义一个数组,数组类型为HashMap<String,Object>,为的是存储资源的名称(key)和资源的值(value),这里假设资源就是图片

然后,将资源加入到这个类型为HashMap的数组中去,并使用SimpleAdapter来实现数据的展现即可,下面上代码:

具体代码如下:

package com.flysnow.sina.weibo;

import java.util.ArrayList;
import java.util.HashMap; import android.app.Activity;
import android.os.Bundle;
import android.widget.GridView;
import android.widget.SimpleAdapter; import com.flysnow.sina.weibo.R; public class MyListActivity extends Activity {
private int[] image = { R.drawable.bc2, R.drawable.bc2, R.drawable.bc2,
R.drawable.bc2, R.drawable.bc2, R.drawable.bc2, R.drawable.bc2,
R.drawable.bc2 };
private String[] text = { "a", "b", "c", "d", "e", "f", "g", "h" }; @Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
this.setContentView(R.layout.main1);
//显示GridView的界面
GridView gridview = (GridView) findViewById(R.id.GridView1);
ArrayList<HashMap<String, Object>> imagelist = new ArrayList<HashMap<String, Object>>();
// 使用HashMap将图片添加到一个数组中,注意一定要是HashMap<String,Object>类型的,因为装到map中的图片要是资源ID,而不是图片本身
// 如果是用findViewById(R.drawable.image)这样把真正的图片取出来了,放到map中是无法正常显示的
for (int i = 0; i < 8; i++) {
HashMap<String, Object> map = new HashMap<String, Object>();
map.put("image", image[i]);
map.put("text", text[i]);
imagelist.add(map);
}
// 使用simpleAdapter封装数据,将图片显示出来
// 参数一是当前上下文Context对象
// 参数二是图片数据列表,要显示数据都在其中
// 参数三是界面的XML文件,注意,不是整体界面,而是要显示在GridView中的单个Item的界面XML
// 参数四是动态数组中与map中图片对应的项,也就是map中存储进去的相对应于图片value的key
// 参数五是单个Item界面XML中的图片ID
SimpleAdapter simpleAdapter = new SimpleAdapter(this, imagelist,
R.layout.items, new String[] { "image", "text" }, new int[] {
R.id.image, R.id.title });
// 设置GridView的适配器为新建的simpleAdapter
gridview.setAdapter(simpleAdapter);
}
}

效果图:

Android中GridView的使用——使用自带的SimpleAdapter(简单适配器)的更多相关文章

  1. Android中GridView通过自定义适配器(未优化)实现图文视图排列

    Android中GridView组件用来以网格方式排列视图,与矩阵类似,当屏幕上有很多元素(文字.图片或其他元素)需要显示时,可以使用该组件.下面我们通过代码实现如下图例(为了方便截图,将事件处理(土 ...

  2. Android中GridView拖拽的效果【android进化三十六】

      最 近看到联想,摩托罗拉等,手机launcher中有个效果,进入mainmenu后,里面的应用程序的图标可以拖来拖去,所以我也参照网上给的代码,写了 一个例子.还是很有趣的,实现的流畅度没有人家的 ...

  3. Android中GridView拖拽的效果

    最 近看到联想,摩托罗拉等,手机launcher中有个效果,进入mainmenu后,里面的应用程序的图标可以拖来拖去,所以我也参照网上给的代码,写了 一个例子.还是很有趣的,实现的流畅度没有人家的那么 ...

  4. Android中GridView的实现实例

    实现效果: activity文件代码: package com.tmacsky; import android.app.Activity; import android.os.Bundle; impo ...

  5. 使用具体解释及源代码解析Android中的Adapter、BaseAdapter、ArrayAdapter、SimpleAdapter和SimpleCursorAdapter

    Adapter相当于一个数据源,能够给AdapterView提供数据.并依据数据创建相应的UI.能够通过调用AdapterView的setAdapter方法使得AdapterView将Adapter作 ...

  6. android中GridView关于间距的属性值介绍

    android:columnWidth  设置列的宽度.关联的方法为:setColumnWidth(int)  stretchMode属性值的作用是设置GridView中的条目以什么缩放模式去填充空间 ...

  7. Android中GridView、ListView 的 getChildAt() 方法返回null 问题

    开发的Android app用到了GridView或者ListView,通常使用getChildAt(int position)方法获取当前点击或者选中的View(即position对应的View). ...

  8. Android中启动页ViewPager和ViewFlipper带指示器

    版权声明:本文为博主原创文章,未经博主允许不得转载.   目录(?)[+]   首先我们来分析一下,想要实现启动页的功能,大家第一个想到的就是使用ViewPager,使用ViewPager确实是一种比 ...

  9. Android中GridView滚动到底部加载数据终极版

    之前在项目中有一个需求是需要GridView控件,滚动到底部自动加载.但是呢GridView控件并不提供诸如ListView监听滚动到底部的onScrollListener方法,为了实现这样一个效果, ...

随机推荐

  1. windows系统中的dll的作用详细解释

    什么是.DLL文件? DLL 是一个包含可由多个程序同时使用的代码和数据的库.例如,在 Windows 操作系统中,Comdlg32 DLL 执行与对话框有关的常见函数.因此,每个程序都可以使用该 D ...

  2. js基础参数获取

    1 获取浏览器中url中的参数,会自动把问号"?"去掉 function getParamsFromHref() { //调试用 var wyl_ = window.locatio ...

  3. [转]关于HTTP服务器每个客户端2个连接的限制

    这两天猫在家里搞一个多线程的断点续传得C#程序,发现同时只能开2个线程下载,其他的线程一律要等待,这样就导致下载大文件时其他线程经常超时,郁闷好久.今天回公司无意中发现了一个帖子,终于真相大白了, 现 ...

  4. HttpWebRequest使用注意(发生阻塞的解决办法)

    原文 http://www.cnblogs.com/Fooo/archive/2008/10/31/1323400.html HttpWebRequest使用注意(发生阻塞的解决办法) , count ...

  5. HDU 5724 Chess(博弈论)

    [题目链接] http://acm.hdu.edu.cn/showproblem.php?pid=5724 [题目大意] 给出一个n行,每行有20格的棋盘,棋盘上有一些棋子,每次操作可以选择其中一个棋 ...

  6. HDU 3374 String Problem (KMP+最小最大表示)

    [题目链接] http://acm.hdu.edu.cn/showproblem.php?pid=3374 [题目大意] 给出一个字符串,求出最小和最大表示是从哪一位开始的,并且输出数量. [题解] ...

  7. ubuntu安装XHProf

    1. 安装XHProf wget http://pecl.php.net/get/xhprof-0.9.2.tgz tar zxf xhprof-0.9.2.tgz cd xhprof-0.9.2 s ...

  8. hive原生和复合类型的数据载入和使用

    原生类型 原生类型包含TINYINT,SMALLINT,INT,BIGINT,BOOLEAN,FLOAT,DOUBLE,STRING,BINARY (Hive 0.8.0以上才可用),TIMESTAM ...

  9. c/c++测试程序运行时间

    算法分析中需要对各种算法进行性能测试,下面介绍两种通用的测试方法,由于只用到标准c语言函数,所以在各种平台和编译器下都能使用. 方法1: clock()函数 开始计时:start = clock() ...

  10. 关于js封装框架类库之选择器引擎(一)

    选择器模块之传统做法 var tag = function (tag){ return document.getElementsByTagName(tag); } var id = function ...