Android:控件GridView的使用实例

所属分类: 软件编程 / Android 阅读数: 53
收藏 0 赞 0 分享

如果是列表(单列多行形式)的使用ListView,如果是多行多列网状形式的优先使用GridView。

<?xml version="1.0" encoding="utf-8"?>
<GridView xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="match_parent"
  android:layout_height="match_parent" >
  

</GridView>

GirdView的一些属性:

  • android:numColumns="auto_fit" --------列数设置为自动
  • android:columnWidth="90dp",----------每列的宽度,也就是Item的宽度
  • android:stretchMode="columnWidth"------缩放与列宽大小同步
  • android:verticalSpacing="10dp"----------垂直边距
  • android:horizontalSpacing="10dp"-------水平边距

1、准备数据源

2、新建适配器

3、加载适配器

GridView(网格视图)是按照行列的方式来显示内容的,一般用于显示图片,图片等内容,比如实现九宫格图,用GridView是首选,也是最简单的,下面来个实例,

实例下载:demo

效果图:

MainActivity.java

package com.example.testgridview;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.app.Activity;
import android.os.Bundle;
import android.widget.GridView;
import android.widget.SimpleAdapter;

public class MainActivity extends Activity {
  private GridView gview;
  private List<Map<String, Object>> data_list;
  private SimpleAdapter sim_adapter;
  // 图片封装为一个数组
  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 = { "通讯录", "日历", "照相机", "时钟", "游戏", "短信", "铃声",
      "设置", "语音", "天气", "浏览器", "视频" };

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.test);
    gview = (GridView) findViewById(R.id.gview);
    //新建List
    data_list = new ArrayList<Map<String, Object>>();
    //获取数据
    getData();
    //新建适配器
    String [] from ={"image","text"};
    int [] to = {R.id.image,R.id.text};
    sim_adapter = new SimpleAdapter(this, data_list, R.layout.item, from, to);
    //配置适配器
    gview.setAdapter(sim_adapter);
  }

  
  
  public List<Map<String, Object>> getData(){    
    //cion和iconName的长度是相同的,这里任选其一都可以
    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]);
      data_list.add(map);
    }
      
    return data_list;
  }
  

}

test.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" 
  android:background="#000"
  >
  
<GridView 
    android:id="@+id/gview"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:numColumns="auto_fit"  
    android:columnWidth="80dp"
    android:stretchMode="columnWidth"
    ></GridView>
</LinearLayout>

item.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:orientation="vertical" 
  android:gravity="center"
  android:padding="10dp"
  >
  
  
<ImageView 
  android:src="@drawable/ic_launcher"
  android:id="@+id/image"
  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>

监听接口: onItemClickListener

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

更多精彩内容其他人还在看

Android样式和主题之选择器的实例讲解

今天小编就为大家分享一篇关于Android样式和主题之选择器的实例讲解,小编觉得内容挺不错的,现在分享给大家,具有很好的参考价值,需要的朋友一起跟随小编来看看吧
收藏 0 赞 0 分享

Android应用动态修改主题的方法示例

今天小编就为大家分享一篇关于Android应用动态修改主题的方法示例,小编觉得内容挺不错的,现在分享给大家,具有很好的参考价值,需要的朋友一起跟随小编来看看吧
收藏 0 赞 0 分享

Flutter 网络请求框架封装详解

这篇文章主要介绍了Flutter 网络请求框架封装详解,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
收藏 0 赞 0 分享

Flutter倒计时/计时器的实现代码

这篇文章主要介绍了Flutter倒计时/计时器的实现代码,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
收藏 0 赞 0 分享

Android使用google breakpad捕获分析native cash

这篇文章主要介绍了Android使用google breakpad捕获分析native cash 的相关知识,非常不错,具有一定的参考借鉴价值,需要的朋友可以参考下
收藏 0 赞 0 分享

详解Flutter WebView与JS互相调用简易指南

这篇文章主要介绍了详解Flutter WebView与JS互相调用简易指南,分为JS调用Flutter和Flutter调用JS,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
收藏 0 赞 0 分享

Android开发实现ListView和adapter配合显示图片和文字列表功能示例

这篇文章主要介绍了Android开发实现ListView和adapter配合显示图片和文字列表功能,涉及Android使用ListView结合adapter适配器实现图文显示功能相关的布局、解析、权限控制等操作技巧,需要的朋友可以参考下
收藏 0 赞 0 分享

Android文字基线Baseline算法的使用讲解

今天小编就为大家分享一篇关于Android文字基线Baseline算法的使用讲解,小编觉得内容挺不错的,现在分享给大家,具有很好的参考价值,需要的朋友一起跟随小编来看看吧
收藏 0 赞 0 分享

Flutter自定义实现神奇动效的卡片切换视图的示例代码

这篇文章主要介绍了Flutter自定义实现神奇动效的卡片切换视图的示例代码,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
收藏 0 赞 0 分享

Android实现自定义滑动刻度尺方法示例

这篇文章主要给大家介绍了关于Android实现自定义滑动刻度尺的相关资料,文中通过示例代码介绍的非常详细,对各位Android开发者们具有一定的参考学习价值,需要的朋友们下面来一起学习学习吧
收藏 0 赞 0 分享
查看更多