Android GridView简单实例

 更新时间:2017年01月25日 09:56:46   作者:stevefat  
这篇文章主要为大家详细介绍了Android GridView简单实例,简单实现九宫格效果,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

也是今天用到的一个东西,就是简单实现九宫格的Demo

1.就是定义各种layout 和对应的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:background="#fff"
  android:orientation="vertical" >

  <LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal" >

    <GridView 
      android:id="@+id/gridView"
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:numColumns="3"
      android:background="#fff"></GridView>


  </LinearLayout>

</LinearLayout>

itme的

<?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:padding="10dp"
  android:layout_gravity="center"
  android:background="#fff"
  android:orientation="vertical" >

  <ImageView
    android:id="@+id/iv"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" />

  <TextView
    android:id="@+id/tv"
    android:paddingTop="10dp"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textColor="#000"
    android:text="管线" />

</LinearLayout>

开始准备数据:

  /**
   * 准备显示的数据
   */
  public void initData() {
    // 生成动态数组,并且转入数据 ,暂时就这样来处理
    lstImageItem = new ArrayList<HashMap<String, Object>>();
    for (int i = 0; i < 3; i++) {
      HashMap<String, Object> map = new HashMap<String, Object>();
      map.put("ItemImage", R.drawable.osg);// 添加图像资源的ID
      map.put("ItemText", "各种管线" + String.valueOf(i));// 按序号做ItemText
      lstImageItem.add(map);
    }
  }

设置显示

gv = (GridView) view.findViewById(R.id.gridView);
    SimpleAdapter adapter = new SimpleAdapter(this, lstImageItem, R.layout.gridview_item, new String[] { "ItemImage", "ItemText" },
        new int[] { R.id.iv, R.id.tv });
gv.setAdapter(adapter);

最后扔一张效果图

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

相关文章

  • Android实现圆形图片或者圆角图片

    Android实现圆形图片或者圆角图片

    这篇文章主要为大家详细介绍了Android实现圆形图片或者圆角图片的代码,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2018-06-06
  • 读取android根目录下的文件或文件夹实例

    读取android根目录下的文件或文件夹实例

    本篇文章主要介绍了读取android根目录下的文件或文件夹,具有一定的参考价值,感兴趣的小伙伴们可以参考一下。
    2016-12-12
  • Android activity堆栈及管理实例详解

    Android activity堆栈及管理实例详解

    这篇文章主要介绍了Android activity堆栈及管理实例详解的相关资料,非常不错,具有参考借鉴价值,对android activity堆栈相关知识感兴趣的朋友一起学习吧
    2016-09-09
  • Android中给fragment写入参数的轻量开发包FragmentArgs简介

    Android中给fragment写入参数的轻量开发包FragmentArgs简介

    这篇文章主要介绍了Android中给fragment写入参数的轻量开发包FragmentArgs简介,需要的朋友可以参考下
    2014-10-10
  • Android Activity被回收的情况分析

    Android Activity被回收的情况分析

    Activity作为Android四大组件之一,他的启动绝对没有那么简单。这里涉及到了系统服务进程,启动过程细节很多,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值
    2022-11-11
  • Android录音应用实例教程

    Android录音应用实例教程

    这篇文章主要介绍了Android录音应用实例,是Android应用程序开发中非常重要的一个功能,需要的朋友可以参考下
    2014-09-09
  • Android开发之AppWidget详解

    Android开发之AppWidget详解

    这篇文章主要介绍了Android开发之AppWidget详解,想了解桌面程序AppWidget的同学可以参考下
    2021-04-04
  • Android实现Service在前台运行服务

    Android实现Service在前台运行服务

    这篇文章主要为大家详细介绍了Android中实现Service在前台运行服务,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2017-11-11
  • Android实现左右滑动切换图片

    Android实现左右滑动切换图片

    这篇文章主要为大家详细介绍了Android实现左右滑动切换图片,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2022-05-05
  • 使用RoundedBitmapDrawable生成圆角图片的方法

    使用RoundedBitmapDrawable生成圆角图片的方法

    由于RoundedBitmapDrawable类没有直接提供生成圆形图片的方法,所以生成圆形图片首先需要对原始图片进行裁剪,将图片裁剪成正方形,最后再生成圆形图片,具体实现方法,可以参考下本文
    2016-09-09

最新评论