esanimmenu_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>
esanimmenu_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>
escolormenu_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>
esvaluescolors.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>
esvaluesstyles.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>
eslayoutgridview_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" />
eslayoutmenu_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>
srccnmxgsamenuMenuInfo.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; } }
srccnmxgsamenuMenuUtils.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; } }
srccnmxgsamenuMenuAdapter.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; } }
srccnmxgsamenuMenuLikeQQActivity.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); } }