本节将实现以下效果

Ps:看起来很不错的样子吧,而且很像九宫格/se

-----------------------------------------------------------------------

下面进入正题[s1]

Step 1:新建Layout,里面创建GridView

<GridView

android:id="@+id/gridView"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_marginTop="10dp"

android:numColumns="3"

android:horizontalSpacing="10dp"

android:verticalSpacing="10dp" >

</GridView>[s2]

Step 2:创建Java代码

实现这一效果使用的是ListView

所以在需要涉及ListView的有关知识

ListView需要数据源,适配器,监听器

① 考虑到GridView中含有图片,文字,所以事先加入图片,文字

private int[] icon={R.drawable.address_book, R.drawable.calendar,R.drawable.camera, R.drawable.clock, R.drawable.games_control,R.drawable.messenger, R.drawable.ringtone, R.drawable.settings, R.drawable.speech_balloon, R.drawable.weather,

R.drawable.world, R.drawable.youtube};

private String[] iconName={"联系人", "日历", "照相机", "时钟", "游戏", "短信", "铃声", "设置","语音", "天气", "浏览器", "Youtube"};

② 新建适配器

---由于含有图片,文字,所以使用的是SimpleAdapter适配器

Ps:SimpleAdapter的参数讲解

SimpleAdapter(Context context, List<? extends Map<String, ?>> data, int resource, String[] from, int[] to)

* SimpleAdapter()

* ---->>context:上下文

* ---->data:数据源 List<? extends Map<String, ?>> data,一个Map所组成的List集合

* 每一个Map都会对应ListView列表中的一行

* 每一个Map(键~值对)中的键必须包含所有在from中所指定的键

* ---->resource:列表项布局文件的ID

* ---->from:Map中的键名

* ---->to:绑定数据视图中的ID,与from成对应关系

adapter=new SimpleAdapter(this, getData(),R.layout.view_grid[s3] , new String[]{"image","text"}, new int[]{R.id.image,R.id.text});//新建适配器

gridView.setAdapter(adapter);

ps:R.layout.view_grid的样式代码

<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">

<ImageView

android:id="@+id/image"

android:src="@drawable/ic_launcher"

android:layout_width="60dp"

android:layout_height="60dp"/>

<TextView

android:id="@+id/text"

android:layout_marginTop="5dp"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:textColor="#ffffff"

android:text="文字"/>

</LinearLayout>

③ 添加数据源

private List<Map<String,Object>>dataList;//声明

dataList=new ArrayList<Map<String,Object>>();

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;

}

④ 创建监听器

实现接口

public class View extends Activity implements OnItemClickListener

gridView.setOnItemClickListener(this);

⑤ 实现监听事件

public void onItemClick(AdapterView<?> arg0, android.view.View arg1,

int position, long id) {

Toast.makeText(this, "我是"+iconName[position],Toast.LENGTH_SHORT).show(); }


[s1] /*

* 1.准备数据源

* 2.新建适配<SimpleAdapter>

* 3.GridView加载适配器

* 4.GridView配置事件监听器(onItemClickListener)

*/

[s2] android:numColumns="" 每一行显示多少列android:horizontalSpacing="" 两列之间的间距

android:verticalSpacing="" 两行之间的间距

[s3]这是新建的Layout

(也就是显示的文字和图片的样式)

Android成长日记-使用GridView显示多行数据的更多相关文章

  1. 使用自连接、for xml path('')和stuff合并显示多行数据到一行中(转)

    原文: http://njm.iteye.com/blog/795881 --使用 自连接.for xml path('')和stuff合并显示多行数据到一行中 --注 --1.计算列可以不用包含在聚 ...

  2. Android成长日记-仿跑马灯的TextView

    在程序设计中有时候一行需要显示多个文字,这时候在Android中默认为分为两行显示,但是对于必须用一行显示的文字需要如何使用呢? ----------------------------------- ...

  3. label正确显示多行数据

    label显示多行文字时会遇到文字中包含换行符“\n”,这时需要对字符进行全部替换, if(labelContent.indexOf('\\n')>0){labelContent=labelCo ...

  4. Android成长日记-使用ViewFlipper实现屏幕切换动画效果

    (一) ViewFlipper介绍 Android系统自带的一个多页面管理控件,它可以实现子界面的自动切换 (二) 为ViewFlipper加入View 1. 静态导入:在Layout布局文件中直接导 ...

  5. Android成长日记-ProgressBar的设计

    ProgressBar的关键属性 Android:max=”100” - ---最大显示进度 Android:progress=”50”----第一显示进度 Android:secondaryProg ...

  6. Android成长日记-Android布局优化

    Android常用布局 1. LinearLayout(线性布局) 2. RelativeLayout(相对布局) 3. TableLayout(表格布局) 4. AbsoluteLayou(绝对布局 ...

  7. Android成长日记-Activity

    ① Activity是一个应用程序组件,提供用户与程序交互的界面 ② Android四大组件 ---Activity ---Service ---BroadcastReceiver ---Conten ...

  8. GridControl gridView显示筛选行,设置条件为包含

    public static void SetFilter(GridView gdv) {     gdv.OptionsView.ShowAutoFilterRow = true; //设置筛选行  ...

  9. Android成长日记-Android四大组件之Service组件的学习

    1.什么是Service? Service是Android四大组件中与Activity最相似的组件,它们都代表可执行的程序,Service与Activity的区别在于:Service一直在后台运行,它 ...

随机推荐

  1. Html5的一些引擎使用感触

    记得在2011年的时候,51CTO曾经采访我对H5的看法,因为当时Html5小组和雷友的关系,感觉是一片大火的形式,当时我的看法是:第一盈利模式不清晰,第二硬件跟不上,第三技术不成熟. 第一和第二点很 ...

  2. checkbox页面全选

    http://pan.baidu.com/s/1tfzSa

  3. node基础07:写文件

    1.writeFile //server.js var http = require("http"); var writefile = require("./writef ...

  4. hadoop1.2.1伪分布模式配置

    1.修改core-site.xml,配置hdfs <configuration> <property> <name>fs.default.name</name ...

  5. 用Map-Reduce的思维处理数据

    在很多人的眼里,Map-Reduce等于Hadoop,没有Hadoop谈Map-Reduce犹如自上谈兵,实则不然,Map-Reduce是一种计算模型,只是非常适合在并行的环境下运行,Hadoop是M ...

  6. SQLServer(MSSQL)、MySQL、SQLite、Access相互迁移转换工具 DB2DB v1.2

    最近公司有一个项目,需要把原来的系统从 MSSQL 升迁到阿里云RDS(MySQL)上面.为便于测试,所以需要把原来系统的所有数据表以及测试数据转换到 MySQL 上面.在百度上找了很多方法,有通过微 ...

  7. (十一)外观模式详解(Service第三者插足,让action与dao分手)

    作者:zuoxiaolong8810(左潇龙),转载请注明出处,特别说明:本博文来自博主原博客,为保证新博客中博文的完整性,特复制到此留存,如需转载请注明新博客地址即可. 各位好,LZ今天给各位分享一 ...

  8. Bootstrap系列 -- 10. 网格布局

    一. 实现原理 网格布局是通过容器的大小,平均分为12份(可以修改),再调整内外边距,和表格布局有点类似但是也存在区别. 实现步骤如下: (1) 数据行.row 必须包含在容器.container 中 ...

  9. 也来山寨一版Flappy Bird (js版)

    随着Flappy Bird的火爆,各种实现的版也不断出现,于是也手痒简单实现了一版. 其实本来只是想实现一下这只笨鸟的飞翔运动的,后来没忍住,就直接实现一个完整游戏了…… 因为这个游戏本身实现起来就没 ...

  10. bootstrap点滴

    1.nav-stacked 这个属性可以决定 tab的变为竖的,不添加的话为横向的. 2.tab  横向的 ul中必须含有nav nav-tabs ul li a 中必须有data-toggle=&q ...