教你如何创建类似QQ的android弹出菜单
16lz
2021-12-04
热心推荐:
Android ListView两种长按弹出菜单方式
popupwindow实现弹出菜单效果
Android人才招聘——最新招聘
大家可能看到 android 的自带的系统菜单比较难看,如图:
很多应用都自己定义了系统菜单 , QQ 游览器手机QQ
我今天做的就是类似手机QQ的菜单,效果图如下:
这是我用PopupWindow 重写一个菜单 ,popupwindow实现弹出菜单功能非常实用,在有布局中经常出现,给用户体验非常不错 ,主要是控制菜单的显示和消失( 动画控制 ),另外对文字和图片 ( 大小最好都一样,否则最后效果有点难看 ) 分别做了适配,其中对文字的长度进行了处理。 下面不用多说 , 进行代码 .需要提一下的是 , 屏蔽一下系统菜单 , 需要下面两步1. 需要创建至少一个系统菜单选项 2. 在 onMenuOpened 方法里显示自己的菜单视图,并返回 FALSEpackage cn.mxgsa.menu;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;//定义gridviewprivate GridView menuGridView;/** Called when the activity is first created. */@Overridepublic void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.main);initPopuWindows();}@Overridepublic boolean onCreateOptionsMenu(Menu menu) {// TODO Auto-generated method stubreturn true;}/*** 设置PopupWindows*/private void initPopuWindows() {//初始化gridviewmenuGridView=(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 stubif ((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 stubMenuInfo 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;}}});}@Overridepublic boolean onMenuOpened(int featureId, Menu menu) {// TODO Auto-generated method stubif (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}@Overridepublic boolean onPrepareOptionsMenu(Menu menu) {// TODO Auto-generated method stubmenu.add("menu");return super.onPrepareOptionsMenu(menu);}}
下载地址
更多相关文章
- android Q 显示系统(一) VSync
- android TextView控件属性列表
- Android(安卓)SDK Content loader has encountered a problem
- Android中app的请求抓包工具 Fiddler 详解
- Android(安卓)Launcher 应用开发
- android:configChanges 问题
- 【Android】TextView 属性详解
- Android给控件添加默认点击效果
- ionic3中android状态栏