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开源 ...
随机推荐
- 使用Jmeter进行http接口做功能、性能测试
在测试移动APP时,会有很多接口需要做测试,我在这里介绍一下对HTTP接口做功能.性能的测试.首先我们会从开发人员拿到接口数据. 一.测试需求描述 1. 本次测试的接口为http服务端接口 2 ...
- web安全系列4:google语法
这是web安全的第四篇,欢迎翻看前面几篇. 前面我们介绍了一些和HTTP有关知识,那么一个疑问就是黑客要做的第一件是什么?其实很简单,确定一个目标,然后搜集信息. 这很容易理解,我们无论做什么都得先有 ...
- 2019.03.29 NOIP训练 友好国度(点分治+容斥)
传送门 思路: 直接上点分治+容斥计算每个因数对应的贡献即可. 代码: #include<bits/stdc++.h> #define ri register int using name ...
- C++程序调用python3
今天想做一个简单的管理密码的小程序,由于最近了解了下Python,就想用Python来写.但是看了看Python的界面库用法有感觉有点麻烦,所以还不如直接使用MFC写写界面,关于csv的文件处理部分使 ...
- shell传递参数
简单介绍python的脚本传参 我们知道python脚本传递参数,有一个很方便的方式-sys.argv.它将脚本本身名字和后面的各项参数都放入一个列表. 使用的时候,索引这个列表就可以了.例如: py ...
- 数据库 的几种链接 join
直接demo,懒的同学可以看看效果 两个表的数据 join和inner join一样 full join报错,可有大神知道原因?
- ASP.NET代码调用SQL Server带DateTime类型参数的存储过程抛出异常问题
ASP.NET代码调用SQL Server带DateTime类型参数的存储过程,如果DateTime类型参数的值是'0001/1/1 0:00:00'时,就会抛出异常“Message: SqlDate ...
- 【Spring】SpringMVC配置文件
SpringMVC中一般会引入三个配置文件applicationContext.xml.dispatcher-servlet.xml(SpringMVC-servlet.xml).web.xml 1. ...
- 关于整数溢出和NaN的问题
当Integer i = Integer.MAX_VALUE;i + 1 < i成立, Double.NaN与任何数(包括自己)比较都为false,与js的NaN一样 如下: //整数溢出 In ...
- git 的学习使用记录
git initls -ahgit add xxxgit commit -m "some message" git statusgit loggit log --pretty=on ...