Android 按Menu弹出菜单

\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弹出菜单的更多相关文章
- 【转】android创建Popwindow弹出菜单的两种方式
方法一的Activity package com.app.test02; import android.app.Activity; import android.os.Bundle; import a ...
- android 长按弹出菜单,复制,粘贴,全选
<!-- 定义基础布局LinearLayout --> <LinearLayout xmlns:android="http://schemas.android.com/ap ...
- Android 使用PopupWindow实现弹出菜单
在本文当中,我将会与大家分享一个封装了PopupWindow实现弹出菜单的类,并说明它的实现与使用. 因对界面的需求,android原生的弹出菜单已不能满足我们的需求,自定义菜单成了我们的唯一选择,在 ...
- Android开发技巧——使用PopupWindow实现弹出菜单
在本文当中,我将会与大家分享一个封装了PopupWindow实现弹出菜单的类,并说明它的实现与使用. 因对界面的需求,android原生的弹出菜单已不能满足我们的需求,自定义菜单成了我们的唯一选择,在 ...
- Android ListView两种长按弹出菜单方式
转自:http://www.cnblogs.com/yejiurui/p/3247527.html package com.wyl.download_demo; import java.util.Ar ...
- 【转】 教你如何创建类似QQ的android弹出菜单
原文地址:http://www.apkbus.com/android-18034-1-1.html 大家可能看到android的自带的系统菜单比较难看,如图: 2011-12-4 23:13 上传 下 ...
- 【Android】5.6 弹出菜单(PopUp Menus)
分类:C#.Android.VS2015: 创建日期:2016-02-07 一.简介 功能描述:用户单击按钮弹出菜单.当用户选择一个菜单项,会触发MenuItemClick事件并让弹出的菜单消失:如果 ...
- Android ListView 长按列表弹出菜单
Android ListView 长按列表弹出菜单 设置长按菜单 listView.setOnCreateContextMenuListener(new View.OnCreateContextMen ...
- Android的Toolbar(含溢出菜单设置[弹出菜单的使用])的使用PopMenu的样式
http://blog.csdn.net/yingtian648/article/details/52432438(转载) 1.在Toolbar.xml中设置弹出菜单的风格(app:popupThem ...
随机推荐
- Ajax异步打开新页面弹框被拦截,无法将参数值传递到后台
一.Form提交,打开新页面被拦截 手动触发Form提交打开新页面是不会被拦截的,但是如果通过Ajax异步处理回调后再程序自动触发Form提交的话,就会被浏览器当成广告弹框拦截 1.暂时的解决办法:如 ...
- MockServer 入门
忽略元数据末回到原数据开始处 MockServer介绍及文档 借鉴公司的文档 http://mock-server.com github:https://github.com/jamesdbloom/ ...
- Python 爬虫实例(3)—— 爬取今日头条as cp 算法 解密
关于今日头条的 as cp 算法,只是对时间进行了加密,他们的js代码是压缩处理的,正常格式化就可以了 url = "http://www.toutiao.com/api/pc/feed/& ...
- MySQL数据约束和关联查询
1 默认值deafult:在建表的时候字段后使用 default ,默认值字段允许为null. 2 非空 not null:在建表的时候字段后使用 not null. 非空字段必须赋值,并且不能是n ...
- C编程测试存储格式为大段还是小段
目前,计算机存储系统有2种存储格式,大端和小端.数据在内存中存储时以字节为单位,一个int类型有4个字节,这就导致是高字节对应低地址(大端模式),高字节对应高地址(小端模式).大端和小端模式本身没有对 ...
- 相似微信的ChattingUi
先看主页面布局 activity_imitate_weixin_main.xml <RelativeLayout xmlns:android="http://schemas.andro ...
- 17. Subsets【medium】
Given a set of distinct integers, return all possible subsets. Notice Elements in a subset must be i ...
- C++之虚析构函数
代码一. #include <iostream> using namespace std; class Base { public: Base(){}; ~Base() { cout &l ...
- linux的RMP命令(rmp包的安装与反安装)
RMP 是 LINUX 下的一种软件的可执行程序,你只要安装它就可以了.这种软件安装包通常是一个RPM包(Redhat Linux Packet Manager,就是Redhat的包管理器),后缀是. ...
- 关于Safe DOG的文件上传bypass
Author:倾旋payloads@aliyun.com本文由科拉实验室成员倾旋原创文章 Part 1 分析 此文主要研究安全狗的数据包分析功能,由于很多人都认为安全狗是通过正则去匹配的,那么暂且那么 ...