GridView和ListView一样,是Android中比较常见的布局控件,譬如我们的手机桌面其实就是一个GridView。

效果:

实现过程和ListView类似,也是通过Adapter将数据源展示在GridView中去,代码去下:

MainActivity.java

package cn.lixyz.gridviewtest;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.SimpleAdapter;
import android.widget.Toast; import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map; public class MainActivity extends AppCompatActivity implements AdapterView.OnItemClickListener { private GridView gridView;
private List<Map<String, Object>> dataList;
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 = {"通讯录", "日历", "照相机", "时钟", "游戏", "短信",
"铃声", "设置", "语音", "天气", "浏览器", "视频"};
private SimpleAdapter adapter; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); gridView = (GridView) findViewById(R.id.gridView); //1. 准备数据源
//2. 新建适配器SimpleAdapter
//3. GridView加载适配器
//4. GridView配置监听器
dataList = new ArrayList<Map<String, Object>>();
adapter = new SimpleAdapter(this, getData(), R.layout.item,
new String[]{"image", "text"}, new int[]{R.id.image, R.id.text}); gridView.setAdapter(adapter);
gridView.setOnItemClickListener(this); } 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;
} @Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Toast.makeText(this, "我是" + iconName[position], Toast.LENGTH_SHORT).show(); }
}

activity_main.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity"> <!--
android:numColumns 每一行显示多少列
android:horizontalSpacing 两列之间的间距
android:verticalSpacing 两行之间的间距
--> <GridView
android:id="@+id/gridView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:horizontalSpacing="10dp"
android:numColumns="3"
android:verticalSpacing="10dp" /> </LinearLayout>

item.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:gravity="center"
android:orientation="vertical"> <ImageView
android:id="@+id/image"
android:layout_width="120dp"
android:layout_height="120dp"
android:src="@drawable/ic_launcher" /> <TextView
android:id="@+id/text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:text="文字"
android:textColor="#000000" /> </LinearLayout>

GridView还有共有以下属性可供开发时使用

属性名称

描述

android:columnWidth

设置列的宽度。关联的方法为:setColumnWidth(int)

android:gravity

设置此组件中的内容在组件中的位置。可选的值有:top、bottom、left、right、center_vertical、fill_vertical、center_horizontal、fill_horizontal、center、fill、clip_vertical 可以多选,用“|”分开。关联方法:setGravity (int gravity)

android:horizontalSpacing

两列之间的间距。关联方法:setHorizontalSpacing(int)

android:numColumns

列数。关联方法:setNumColumns(int)

android:stretchMode

缩放模式。关联方法:setStretchMode(int)

android:verticalSpacing

两行之间的间距。关联方法:setVerticalSpacing(int)

Android笔记(二十二) Android中的GridView的更多相关文章

  1. Android笔记(七十二) Style和Theme

    我们尝尝需要使用setText.setColor.setTextSize等属性来设置控件的样式,但是每个控件都需要设置这些属性,工作量无疑是巨大的,并且后期维护起来也不方便. Style Androi ...

  2. Android笔记(六十二)网络框架volley

    什么是Volley 很多时候,我们的APP都需要用到网络技术,使用HTTP协议来发送接收数据,谷歌推出了一个网络框架——volley,该框架适合进行数据量不大,但通信频繁的网络操作. 它的优点: (1 ...

  3. Android笔记(十二)AndroidManiFest.xml

    AndroidManiFest.xml清单文件是每个Android项目所必须的,它是整个Android应用的全局描述文件.AndroidManiFest.xml清单文件说明了该应用的名称.所使用的图标 ...

  4. Android笔记(七十五) Android中的图片压缩

    这几天在做图记的时候遇第一次遇到了OOM,好激动~~ 追究原因,是因为在ListView中加载的图片太大造成的,因为我使用的都是手机相机直接拍摄的照片,图片都比较大,所以在加载的时候会出现内存溢出,那 ...

  5. Android笔记(六十六) android中的动画——XML文件定义属性动画

    除了直接在java代码中定义动画之外,还可以使用xml文件定义动画,以便重用. 如果想要使用XML来编写动画,首先要在res目录下面新建一个animator文件夹,所有属性动画的XML文件都应该存放在 ...

  6. Android笔记(六十五) android中的动画——属性动画(propertyanimation)

    补间动画只能定义起始和结束两个帧在“透明度”.“旋转”.“倾斜”.“位移”4个方面的变化,逐帧动画也只能是播放多个图片,无法满足我们日常复杂的动画需求,所以谷歌在3.0开始,推出了属性动画(prope ...

  7. Android笔记(十) Android中的布局——表格布局

    TableLayout运行我们使用表格的方式来排列控件,它的本质依然是线性布局.表格布局采用行.列的形式来管理控件,TableLayout并不需要明确的声明包含多少行多少列,而是通过添加TableRo ...

  8. Android笔记(六十四) android中的动画——补间动画(tweened animation)

    补间动画就是只需要定义动画开始和结束的位置,动画中间的变化由系统去补齐. 补间动画由一下四种方式: 1.AplhaAnimation——透明度动画效果 2.ScaleAnimation ——缩放动画效 ...

  9. Android群英传笔记——第十二章:Android5.X 新特性详解,Material Design UI的新体验

    Android群英传笔记--第十二章:Android5.X 新特性详解,Material Design UI的新体验 第十一章为什么不写,因为我很早之前就已经写过了,有需要的可以去看 Android高 ...

  10. python3.4学习笔记(二十二) python 在字符串里面插入指定分割符,将list中的字符转为数字

    python3.4学习笔记(二十二) python 在字符串里面插入指定分割符,将list中的字符转为数字在字符串里面插入指定分割符的方法,先把字符串变成list然后用join方法变成字符串str=' ...

随机推荐

  1. C#操作Access时Parameters集合的使用方法(转)

    按照C#操作Sql Server数据库的方式是不一样的,使用时发现占位符数据会混乱. 查阅相关资料及测试后,总结出这样一套使用方法. 方法: public bool TsqlExecute(strin ...

  2. Java合并(连接)多个音频

    java sound resource 合并的说法有歧义,为了方便大家搜索到这里,所以用这个标题,实际上是连接(concat),可以理解为字符串concat方法所指定的含义. AudioConcat. ...

  3. k8s版jenkins--master/slave模式实现CI/CD---带solo开源博客项目--带maven、djk、git工具

    k8s环境: 192.168.0.91 master 192.168.0.92 node 192.168.0.96 gitlab 192.168.0.98 harbor k8s集群安装请参照:http ...

  4. k8s、jenkins集成

    参考连接 http://www.uml.org.cn/jchgj/201804252.asp https://huanqiang.wang/2018/03/30/Jenkins-Gitlab-Kube ...

  5. LODOP指定window默认打印机和临时默认打印机

    通过以下语句,可指定windows默认打印机LODOP.SET_PRINT_MODE("WINDOW_DEFPRINTER",某打印机名或序号);这种默认打印机是指的windows ...

  6. C-LODOP回调多个返回值On_Return_Remain

    c-lodop默认一次只能回调一个值,回调后会销毁,如果想回调多个值,可加该语句:LODOP.On_Return_Remain=true;之前有博文:lodop和c-lodop通过打印状态和任务不在队 ...

  7. angular2-cookie 如何升级到 ngx-cookie

    angular2-cookie 如何升级到  ngx-cookie https://github.com/salemdar/angular2-cookie#readme

  8. Ubuntu18使用netplan设置网络

    参考:https://my.oschina.net/u/2306127/blog/2877106 https://blog.csdn.net/peyte1/article/details/805090 ...

  9. node不要使用最新版本,使用LTS版本

    错误现象 const { Math, Object, Reflect } = primordials; 原因 使用了最新的node版本 解决 使用稳定版本,参考官网说明,目前10.x的版本是稳定版本( ...

  10. 《构建之法》——GitHub和Visual Studio的基础使用

    git地址 https://github.com/microwangwei git用户名 microwangwei 学号后五位 62214 博客地址 https://www.cnblogs.com/w ...