Android UI(二)DridView的菜单
Jeff Lee blog: http://www.cnblogs.com/Alandre/ (泥沙砖瓦浆木匠),retain the url when reproduced ! Thanks
今天我们来Android UI第二讲:实现DridView的菜单样式
下载链接:
http://files.cnblogs.com/Alandre/AndroidUI2.rar
效果图:

第一步:实现Guid Item
首先确定的是,里面有四个元素。每个元素的组合为 图片+文字。所以我们先定义一个xml:
/AndroidUI2/res/layout/main_menu_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:orientation="vertical" >
<ImageView
android:id="@+id/ItemImageView"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"/>
<TextView
android:id="@+id/ItemTextView"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center"/>
</LinearLayout>
第二步:通过定义的适配器SimpleAdapter 将你需要的Item add进GridView
先在视图中定义GridView:
/AndroidUI2/res/layout/activity_main.xml
<RelativeLayout 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:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" > <LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="12dp"
android:layout_marginRight="12dp"
android:background="@color/white"
android:orientation="horizontal"
android:gravity="top"> <GridView
android:id="@+id/gridview_main_menu"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:columnWidth="90dp"
android:stretchMode="columnWidth"
android:numColumns="4"
android:horizontalSpacing="10dp"
android:gravity="center_horizontal"
>
</GridView> </LinearLayout> </RelativeLayout>
然后Activity核心代码:
package com.example.androidui2; import java.util.ArrayList;
import java.util.HashMap;
import java.util.List; import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.SimpleAdapter;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener; public class MainActivity extends Activity
{ private GridView mainMenuGridView;
private int[] mainMenuImageRes = {R.drawable.circle,R.drawable.circle,R.drawable.circle,R.drawable.circle};
private String[] mainMenuStrs = {"拨号","所有联系人","使用说明","退出"}; @Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mainMenuGridView = (GridView)findViewById(R.id.gridview_main_menu); initView();
} // init views
private void initView()
{
// init main-menu
List<HashMap<String, Object>> datas = new ArrayList<HashMap<String,Object>>();
int length = mainMenuStrs.length;
for (int i = 0; i < length; i++)
{
HashMap<String, Object> map = new HashMap<String, Object>();
map.put("ItemImageView", mainMenuImageRes[i]);
map.put("ItemTextView", mainMenuStrs[i]);
datas.add(map);
}
SimpleAdapter menuAdapter = new SimpleAdapter(
MainActivity.this,datas,
R.layout.main_menu_item,
new String[]{"ItemImageView","ItemTextView"},
new int[]{R.id.ItemImageView,R.id.ItemTextView} );
mainMenuGridView.setAdapter(menuAdapter);
mainMenuGridView.setOnItemClickListener(new MainMenuItemOnClick()); } // Main Menu Item On Click Function
public class MainMenuItemOnClick implements OnItemClickListener
{ /** arg0 : The AdapterView where the click happened
* arg1 : The view within the AdapterView that was clicked
* arg2 : The position of the view in the adapter
* arg3 : The row id of the item that was clicked
**/
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3)
{
HashMap<String, Object> item = (HashMap<String, Object>)arg0.getItemAtPosition(arg2);
if (item.get("ItemTextView").equals(mainMenuStrs[0]))
{
Toast.makeText(getApplicationContext(), mainMenuStrs[0],
Toast.LENGTH_SHORT).show();
}
if (item.get("ItemTextView").equals(mainMenuStrs[1]))
{
Toast.makeText(getApplicationContext(), mainMenuStrs[1],
Toast.LENGTH_SHORT).show();
}
if (item.get("ItemTextView").equals(mainMenuStrs[2]))
{
Toast.makeText(getApplicationContext(), mainMenuStrs[2],
Toast.LENGTH_SHORT).show();
}
if (item.get("ItemTextView").equals(mainMenuStrs[3]))
{
Toast.makeText(getApplicationContext(), mainMenuStrs[3],
Toast.LENGTH_SHORT).show();
}
} } @Override
public boolean onCreateOptionsMenu(Menu menu)
{
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
} }
解释:
定义所需要的 item 数组:
private int[] mainMenuImageRes = {R.drawable.circle,R.drawable.circle,R.drawable.circle,R.drawable.circle};
	private String[] mainMenuStrs  = {"拨号","所有联系人","使用说明","退出"};
初始化用SimpleAdapter添加
SimpleAdapter menuAdapter = new SimpleAdapter(
MainActivity.this,datas,
R.layout.main_menu_item,
new String[]{"ItemImageView","ItemTextView"},
new int[]{R.id.ItemImageView,R.id.ItemTextView} );
mainMenuGridView.setAdapter(menuAdapter);
然后添加动作的时候,我们巧妙的获取到ItemImageView的值进行判断,然后动作。
HashMap<String, Object> item = (HashMap<String, Object>)arg0.getItemAtPosition(arg2);
if (item.get("ItemTextView").equals(mainMenuStrs[0]))
总结
GridView 可以作为平面化菜单的好东西。
Android UI(二)DridView的菜单的更多相关文章
- GitHub上受欢迎的Android UI Library
		GitHub上受欢迎的Android UI Library 内容 抽屉菜单 ListView WebView SwitchButton 按钮 点赞按钮 进度条 TabLayout 图标 下拉刷新 Vi ... 
- 十二、Android UI开发专题(转)
		http://dev.10086.cn/cmdn/bbs/viewthread.php?tid=18736&page=1#pid89255Android UI开发专题(一) 之界面设计 近期很 ... 
- Android进阶(二十八)上下文菜单ContextMenu使用案例
		上下文菜单ContextMenu使用案例 前言 回顾之前的应用程序,发现之前创建的选项菜单无法显示了.按照正常逻辑来说,左图中在"商品信息"一栏中应该存在选项菜单,用户可进行分享等 ... 
- Android进阶(二十六)MenuInflater实现菜单添加
		MenuInflater实现菜单添加 前言 之前实现的Android项目中可以实现菜单的显示.但是再次调试项目时发现此功能已无法实现,很是令人费解.难道是因为自己手机Android系统的问题?尝试通过 ... 
- Android UI开发第二十八篇——Fragment中使用左右滑动菜单
		Fragment实现了Android UI的分片管理,尤其在平板开发中,好处多多.这一篇将借助Android UI开发第二十六篇——Fragment间的通信. Android UI开发第二十七篇——实 ... 
- Android UI设计规则
		Android UI技巧 1.1 不该做什么 l 不要照搬你在其他平台的UI设计,应该让用户使用感觉是在真正使用一个Android软件,在你的LOGO显示和平台总体观感之间做好平衡 l 不要过度使 ... 
- Android UI(五)云通讯录项目之联系人列表,带侧滑选择,带搜索框
		作者:泥沙砖瓦浆木匠网站:http://blog.csdn.net/jeffli1993个人签名:打算起手不凡写出鸿篇巨作的人,往往坚持不了完成第一章节.交流QQ群:[编程之美 365234583]h ... 
- Android UI相关开源项目库汇总
		最近做了一个Android UI相关开源项目库汇总,里面集合了OpenDigg 上的优质的Android开源项目库,方便移动开发人员便捷的找到自己需要的项目工具等,感兴趣的可以到GitHub上给个st ... 
- 各种Android UI开源框架 开源库
		各种Android UI开源框架 开源库 转 https://blog.csdn.net/zhangdi_gdk2016/article/details/84643668 自己总结的Android开源 ... 
随机推荐
- oracle 数据定义语言(DDL)语法
			DDL语言包括数据库对象的创建(create).删除(drop)和修改(alter)的操作 1.创建表语法 create table table_name( column_name datatype ... 
- windows kafka 环境搭建踩坑记
			版本介绍(64位): Windows 10 JDK1.8.0_171 zookeeper-3.4.8/ kafka_2.11-0.10.0.1.tgz 点击链接进行下载 1. JDK安装和环境搭建 自 ... 
- 第八周助教工作总结——NWNU李泓毅
			1.助教博客链接: https://www.cnblogs.com/NWNU-LHY/ 2.作业要求博客链接: http://www.cnblogs.com/nwnu-daizh/p/10687492 ... 
- 2019.03.26 bzoj4446: [Scoi2015]小凸玩密室(树形dp)
			传送门 题意简述: 给一棵完全二叉树,有点权aia_iai和边权,每个点有一盏灯,现在要按一定要求点亮: 任意时刻点亮的灯泡必须连通 点亮一个灯泡后必须先点亮其子树 费用计算如下:点第一盏灯不要花费 ... 
- spring深入学习(四)-----spring aop
			AOP概述 aop其实就是面向切面编程,举个例子,比如项目中有n个方法是对外提供http服务的,那么如果我需要对这些http服务进行响应时间的监控,按照传统的方式就是每个方法中添加相应的逻辑,但是这些 ... 
- Codeforces Round #485 (Div. 2) E. Petr and Permutations
			Codeforces Round #485 (Div. 2) E. Petr and Permutations 题目连接: http://codeforces.com/contest/987/prob ... 
- 基于继承的 MethodInterceptor 动态代理(换种写法)
			net.sf.cglib.proxy.Enhancer Generates dynamic subclasses to enable method interception. This class s ... 
- C#如何以管理员身份运行程序 转
			在使用winform程序获取调用cmd命令提示符时,如果是win7以上的操作系统,会需要必须以管理员身份运行才会执行成功,否则无效果或提示错误. 比如在通过winform程序执行cmd命令时,某些情况 ... 
- Borg, Omega, and Kubernetes读后笔记
			前言 最近又读了一遍 Borg, Omega, and Kubernetes 这篇文章,觉得这个文章写得很好,让我对架构设计有了进一步的认识,所以想写一篇读后笔记. 原文地址,还有篇中文翻译的,这个中 ... 
- B - Big Event in HDU
			Nowadays, we all know that Computer College is the biggest department in HDU. But, maybe you don't k ... 
