\res\anim\menu_in.xml    资源文件(动画)

<?xml version="1.0" encoding="UTF-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:interpolator="@android:anim/decelerate_interpolator" >
<translate
android:duration="@android:integer/config_shortAnimTime"
android:fromYDelta="25.0%"
android:toYDelta="0.0" />
<alpha
android:duration="@android:integer/config_shortAnimTime"
android:fromAlpha="0.0"
android:toAlpha="1.0" />
</set>

\res\anim\menu_out.xml   资源文件(动画)

<?xml version="1.0" encoding="UTF-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:interpolator="@android:anim/accelerate_interpolator" >
<translate
android:duration="@android:integer/config_shortAnimTime"
android:fromYDelta="0.0"
android:toYDelta="50.0%" />
<alpha
android:duration="@android:integer/config_shortAnimTime"
android:fromAlpha="1.0"
android:toAlpha="0.0" />
</set>

\res\color\menu_text_color.xml    资源文件(颜色)

<?xml version="1.0" encoding="UTF-8"?>
<selector
xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_enabled="true" android:color="#ff031e3a" />
<item android:state_enabled="false" android:color="#ff778797" />
</selector>

\res\values\colors.xml 资源文件(颜色)

<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="discuss_blue">#295782</color>
<color name="white">#FFFFFF</color>
<color name="black">#000000</color>
<color name="discuss_writer">#778190</color>
<color name="gray">#ff888888</color>
<color name="photo_preview_select_album_layout_bg">#ff3a3a3a</color>
</resources>

\res\values\styles.xml 资源文件(样式)

<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="menuTextStyle" parent="@android:style/TextAppearance.Small">
<item name="android:textSize">12.0sp</item>
<item name="android:textColor">@color/menu_text_color</item>
</style>
<style name="menushow">
<item name="android:windowEnterAnimation">@anim/menu_in</item>
<item name="android:windowExitAnimation">@anim/menu_out</item>
</style>
</resources>

\res\layout\gridview_menu.xml  布局文件(gridview控件)

<?xml version="1.0" encoding="utf-8"?>
<GridView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/gvmenu"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:horizontalSpacing="12.0dip"
android:listSelector="@drawable/menu_selectitem"
android:numColumns="4"
android:stretchMode="columnWidth"
android:verticalSpacing="8.0dip" />

\res\layout\menu_item.xml   布局文件(gridview中的Item)

<?xml version="1.0" encoding="UTF-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/RelativeLayout_Item"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:paddingTop="6.0dip"
android:saveEnabled="false" > <ImageView
android:id="@+id/item_image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true" /> <TextView
android:id="@+id/item_text"
style="@style/menuTextStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/item_image"
android:layout_centerHorizontal="true"
android:layout_marginTop="7.0dip"
android:text="" /> </RelativeLayout>

\src\cn\mxgsa\menu\MenuInfo.java  实体类

public class MenuInfo {
// 标题
public String title;
public int imgsrc;
// 是否隐藏
public boolean ishide;
// menuId
public int menuId;
public MenuInfo(int menuId, String title,int imgsrc,Boolean ishide){
this.menuId=menuId;
this.title=title;
this.imgsrc=imgsrc;
this.ishide=ishide;
}
}

\src\cn\mxgsa\menu\MenuUtils.java  常量资源类

import java.util.ArrayList;
import java.util.List;
import cn.mxgsa.menu.MenuInfo; public class MenuUtils {
public static final int MENU_SETTING=1;
public static final int MENU_LOGOUT=2;
public static final int MENU_HELP=3;
public static final int MENU_EXIT=4;
public static final int MENU_LOGIN=5;
public static final int MENU_SERCH_FRIEND=6;
public static final int MENU_GROUP_ACCURATE=7;
public static final int MENU_GROUP_CATEGORY=8;
public static final int MENU_CLEAR_LIST=9;
public static final int MENU_ADD_FRIEND=10;
public static final int MENU_ADD_GROUP=11;
public static final int MENU_CHAT_HISTORY=12;
public static final int MENU_ONE_CLOSE=13;
public static final int MENU_MULITE_CLOSE=14; private static List<MenuInfo> initMenu(){
List<MenuInfo> list=new ArrayList<MenuInfo>();
list.add(new MenuInfo(MENU_SETTING,"设置",R.drawable.menu_ic_setting,false));
list.add(new MenuInfo(MENU_LOGOUT,"切换用户",R.drawable.menu_ic_logout,false));
list.add(new MenuInfo(MENU_HELP,"检查更新",R.drawable.menu_ic_help,false));
list.add(new MenuInfo(MENU_EXIT,"退出应用",R.drawable.menu_ic_exit,false));
return list;
} /**
* 获取当前菜单列表
* @return
*/
public static List<MenuInfo> getMenuList(){
List<MenuInfo> list=initMenu();
list.add(0,new MenuInfo(MENU_SERCH_FRIEND,"搜索好友",R.drawable.menu_ic_search_friend,false));
list.add(0,new MenuInfo(MENU_ADD_GROUP,"添加分组",R.drawable.menu_ic_addgroup,false));
list.add(0,new MenuInfo(MENU_ADD_FRIEND,"添加好友",R.drawable.menu_ic_addfriend,false));
list.add(0,new MenuInfo(MENU_SERCH_FRIEND,"搜索好友",R.drawable.menu_ic_search_friend,false)); return list;
} }

\src\cn\mxgsa\menu\MenuAdapter.java

import java.util.List;
import cn.mxgsa.menu.MenuInfo;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView; public class MenuAdapter extends BaseAdapter { private final List<MenuInfo> list;
private final LayoutInflater inflater;
public MenuAdapter(Context context,List<MenuInfo> list){
this.list=list;
inflater=LayoutInflater.from(context);
} public int getCount() {
// TODO Auto-generated method stub
return list.size();
} public Object getItem(int arg0) {
// TODO Auto-generated method stub
return list.get(arg0);
} public long getItemId(int arg0) {
// TODO Auto-generated method stub
return arg0;
} public View getView(int arg0, View arg1, ViewGroup arg2) {
// TODO Auto-generated method stub
View view=arg1;
if (view==null) {
view=inflater.inflate(R.layout.menu_item, null);
}
MenuInfo mInfo=list.get(arg0);
ImageView iView=(ImageView)view.findViewById(R.id.item_image);
TextView tView=(TextView)view.findViewById(R.id.item_text);
iView.setImageResource(mInfo.imgsrc);
tView.setText(mInfo.title);
if (mInfo.ishide) {
iView.setAlpha(80);
}
return view;
} }

\src\cn\mxgsa\menu\MenuLikeQQActivity.java

import java.util.List;
import android.app.Activity;
import android.os.Bundle;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.GridView;
import android.widget.PopupWindow;
import android.widget.Toast; public class MenuLikeQQActivity extends Activity { // 定义popupwindow
private PopupWindow popup;
// 定义适配器
private MenuAdapter menuAdapter;
//菜单项列表
private List<MenuInfo> menulists;
//定义gridview
private GridView menuGridView; @Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
initPopuWindows();
} @Override
public boolean onCreateOptionsMenu(Menu menu) {
return true;
} /**
* 设置PopupWindows
*/
private void initPopuWindows() {
//初始化gridview
menuGridView=(GridView)View.inflate(this, R.layout.gridview_menu, null);
//初始化PopupWindow,LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT控制显示
popup = new PopupWindow(menuGridView, LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);
// 设置menu菜单背景
popup.setBackgroundDrawable(getResources().getDrawable(R.drawable.menu_background));
// menu菜单获得焦点 如果没有获得焦点menu菜单中的控件事件无法响应
popup.setFocusable(true);
//设置显示和隐藏的动画
popup.setAnimationStyle(R.style.menushow);
popup.update();
//设置触摸获取焦点
menuGridView.setFocusableInTouchMode(true);
//设置键盘事件,如果按下菜单键则隐藏菜单
menuGridView.setOnKeyListener(new android.view.View.OnKeyListener() {
public boolean onKey(View v, int keyCode, KeyEvent event) {
// TODO Auto-generated method stub
if ((keyCode == KeyEvent.KEYCODE_MENU) && (popup.isShowing())) {
popup.dismiss();
return true;
}
return false;
} });
//添加菜单按钮事件
menuGridView.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
// TODO Auto-generated method stub
MenuInfo mInfo = menulists.get(arg2);
popup.dismiss();
if (mInfo.ishide) {
return;
}
switch (mInfo.menuId) {
case MenuUtils.MENU_ADD_FRIEND:
Toast.makeText(MenuLikeQQActivity.this, "添加好友", 1).show();
break;
case MenuUtils.MENU_ADD_GROUP:
Toast.makeText(MenuLikeQQActivity.this, "添加分组", 1).show();
break;
case MenuUtils.MENU_EXIT:
Toast.makeText(MenuLikeQQActivity.this, "退出应用", 1).show();
break;
case MenuUtils.MENU_GROUP_ACCURATE: break;
case MenuUtils.MENU_GROUP_CATEGORY: break;
case MenuUtils.MENU_HELP:
Toast.makeText(MenuLikeQQActivity.this, "检查更新", 1).show(); break;
case MenuUtils.MENU_LOGOUT:
Toast.makeText(MenuLikeQQActivity.this, "切换用户", 1).show();
break;
case MenuUtils.MENU_SERCH_FRIEND:
Toast.makeText(MenuLikeQQActivity.this, "搜索好友", 1).show();
break;
case MenuUtils.MENU_SETTING:
Toast.makeText(MenuLikeQQActivity.this, "设置", 1).show();
break;
}
}
});
} @Override
public boolean onMenuOpened(int featureId, Menu menu) {
if (popup != null) {
menulists = MenuUtils.getMenuList();
menuAdapter = new MenuAdapter(this, menulists);
menuGridView.setAdapter(menuAdapter);
popup.showAtLocation(this.findViewById(R.id.linearlayout), Gravity.BOTTOM, 0, 0);
}
return false;// 返回为true 则显示系统menu
} @Override
public boolean onPrepareOptionsMenu(Menu menu) {
menu.add("menu");
return super.onPrepareOptionsMenu(menu);
}
}

DEMO完整源码:http://download.csdn.net/detail/androidsj/5597723

Android 按Menu弹出菜单的更多相关文章

  1. 【转】android创建Popwindow弹出菜单的两种方式

    方法一的Activity package com.app.test02; import android.app.Activity; import android.os.Bundle; import a ...

  2. android 长按弹出菜单,复制,粘贴,全选

    <!-- 定义基础布局LinearLayout --> <LinearLayout xmlns:android="http://schemas.android.com/ap ...

  3. Android 使用PopupWindow实现弹出菜单

    在本文当中,我将会与大家分享一个封装了PopupWindow实现弹出菜单的类,并说明它的实现与使用. 因对界面的需求,android原生的弹出菜单已不能满足我们的需求,自定义菜单成了我们的唯一选择,在 ...

  4. Android开发技巧——使用PopupWindow实现弹出菜单

    在本文当中,我将会与大家分享一个封装了PopupWindow实现弹出菜单的类,并说明它的实现与使用. 因对界面的需求,android原生的弹出菜单已不能满足我们的需求,自定义菜单成了我们的唯一选择,在 ...

  5. Android ListView两种长按弹出菜单方式

    转自:http://www.cnblogs.com/yejiurui/p/3247527.html package com.wyl.download_demo; import java.util.Ar ...

  6. 【转】 教你如何创建类似QQ的android弹出菜单

    原文地址:http://www.apkbus.com/android-18034-1-1.html 大家可能看到android的自带的系统菜单比较难看,如图: 2011-12-4 23:13 上传 下 ...

  7. 【Android】5.6 弹出菜单(PopUp Menus)

    分类:C#.Android.VS2015: 创建日期:2016-02-07 一.简介 功能描述:用户单击按钮弹出菜单.当用户选择一个菜单项,会触发MenuItemClick事件并让弹出的菜单消失:如果 ...

  8. Android ListView 长按列表弹出菜单

    Android ListView 长按列表弹出菜单 设置长按菜单 listView.setOnCreateContextMenuListener(new View.OnCreateContextMen ...

  9. Android的Toolbar(含溢出菜单设置[弹出菜单的使用])的使用PopMenu的样式

    http://blog.csdn.net/yingtian648/article/details/52432438(转载) 1.在Toolbar.xml中设置弹出菜单的风格(app:popupThem ...

随机推荐

  1. Bootstrap 的模态框类

    事件类型 描述 show.bs.modal show 方法调用之后立即触发该事件.如果是通过点击某个作为触发器的元素,则此元素可以通过事件的relatedTarget 属性进行访问. shown.bs ...

  2. python 并发和线程

    并发和线程 基本概念 - 并行.并发 并行, parallel 互不干扰的在同一时刻做多件事; 如,同一时刻,同时有多辆车在多条车道上跑,即同时发生的概念. 并发, concurrency 同时做某些 ...

  3. Zepto源代码分析一~核心方法

    今天抽出时间复习了一下Zepto的源代码,依照自己的理解进行凝视. 欢迎大家拍砖. 源代码版本号:v1.1.4 源代码下载地址:http://zeptojs.com/ 分析总体代码之后,整理出架构图: ...

  4. 基于Bootstrap的Asp.net Mvc 分页的实现(转)

    最近写了一个mvc 的 分页,样式是基于 bootstrap 的 ,提供查询条件,不过可以自己写样式根据个人的喜好,以此分享一下.首先新建一个Mvc 项目,既然是分页就需要一些数据,我这 边是模拟了一 ...

  5. Lintcode---克隆二叉树

    深度复制一个二叉树. 给定一个二叉树,返回一个他的 克隆品 . 您在真实的面试中是否遇到过这个题? Yes 样例 给定一个二叉树: 1 / \ 2 3 / \ 4 5 返回其相同结构相同数值的克隆二叉 ...

  6. J2EE学术交流感悟——分层

        学术交流进行了一周,是关于J2EE的学术报告. 目的是让我们在学习的时候对"所学知识"有一个宏观的认识. 開始是以为环绕"J2EE"进行解说,怕自己没有 ...

  7. Intel 5 6 7 8系列芯片组介绍

    Intel 5 6 7 8系列芯片组介绍 Iknow.2015-11-05 22:40|知识编号:122257 操作步骤: [Inetl 5.6.7.8系列芯片组介绍] 芯片组是主板电路的核心.一定意 ...

  8. PL/SQL developer连接oracle出现“ORA-12154:TNS:could not resolve the connect identifier specified”问题的解决

    转载请注明出处:http://blog.csdn.net/dongdong9223/article/details/50728536 本文出自[我是干勾鱼的博客] 使用PL/SQL developer ...

  9. CSS学习笔记(8)--纯CSS绘制三角形(各种角度)

    纯CSS绘制三角形(各种角度) CSS三角形绘制方法,学会了这个,其它的也就简单.   我们的网页因为 CSS 而呈现千变万化的风格.这一看似简单的样式语言在使用中非常灵活,只要你发挥创意就能实现很多 ...

  10. 基于HTML5自定义文字背景生成QQ签名档

    分享一款利用HTML5实现的自定义文字背景应用,首先我们可以输入需要显示的文字,并且为该文字选择一张背景图片,背景图片就像蒙版一样覆盖在文字上.点击生成QQ签名档即可将文字背景融为一体生成另外一张图片 ...