Android记录23-关于actionbar中overflow menu(溢出菜单)中的一些问题
来源:互联网 发布:静态个人引导页源码 编辑:程序博客网 时间:2024/05/21 08:51
前言
本篇博客记录一下小巫在使用actionbar的时候遇到关于溢出菜单的一些问题:
- 什么是Overflow menu
- Android 3.0以上默认不显示overflow menu
- 如何强制在Android 4.4.4以下的手机显示overflow menu
- 经测试,魅族手机无法强制显示overflow menu,其他手机暂无发现这种问题
先来两张对比图:左边是红米手机的效果,右边是针对魅族Mx4pro做的适配效果。什么是overflow menu?
先来解释一下什么是overflow menu,中文叫做“溢出菜单”,顾名思义溢出的菜单,我们有些手机是有实体按键或虚拟按键,其中一个是菜单键,有的可能没有,例如魅族就只有一个back的返回虚拟键。点击菜单键的时候,就会在底部弹出溢出菜单,在Android 3.0之后,也就是API 11之后才可以加入ActionBar控件的,再其右上角会有三个点,那个就是我们说的overflow menu,我们在menu中定义菜单项就可以弹出类似上面红米手机弹出的效果。关于ActionBar有很多用法,本篇博客就不展开讲,有兴趣的同学可以到官网进行学习。如何在ActionBar强制显示溢出菜单?
Android 3.0以上的手机默认是不显示溢出菜单的,那如何强制在Android 4.4以下的手机显示溢出菜单呢?可以使用以下方法:
- // 强制actionbar显示overflow菜单
- // force to show overflow menu in actionbar for android 4.4 below
- private void getOverflowMenu() {
- try {
- ViewConfiguration config = ViewConfiguration.get(this);
- Field menuKeyField = ViewConfiguration.class
- .getDeclaredField("sHasPermanentMenuKey");
- if (menuKeyField != null) {
- menuKeyField.setAccessible(true);
- menuKeyField.setBoolean(config, false);
- }
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
适配类似魅族手机无法显示溢出菜单的解决方案
上面已经解决了如何在Android 4.4以下的手机强制显示溢出菜单,下面来解决一些奇葩手机无法显示溢出菜单的问题。为什么魅族手机无法显示,了解到的是魅族没有所谓的actionbar,它们称为smartbar,看来是魅族的工程师把官方的actionbar进行了修改。一个字,坑!不过小巫想到了一个解决办法,我们每部手机都有自己的手机品牌,我们可以针对这些奇葩手机进行适配,溢出菜单我们就使用popupwindow来替代。获得手机信息
- /**
- * 获取IMEI号,IESI号,手机型号
- */
- public static void getInfo(Context context) {
- TelephonyManager mTm = (TelephonyManager) context
- .getSystemService(Context.TELEPHONY_SERVICE);
- String imei = mTm.getDeviceId();
- String imsi = mTm.getSubscriberId();
- String mtype = android.os.Build.MODEL; // 手机型号
- String mtyb = android.os.Build.BRAND;// 手机品牌
- String numer = mTm.getLine1Number(); // 手机号码,有的可得,有的不可得
- Log.i("text", "手机IMEI号:" + imei + "手机IESI号:" + imsi + "手机型号:" + mtype
- + "手机品牌:" + mtyb + "手机号码" + numer);
- }
获取手机品牌
- /**
- * 得到手机品牌
- * @return
- */
- public static String getPhoneBrand() {
- return android.os.Build.BOARD;
- }
得到手机品牌之后,我们就在创建溢出菜单的时候进行判断:1. 如果是魅族手机,则加载自定义菜单2. 如果不是,则使用系统的溢出菜单,加载菜单内容
- @Override
- public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
- // Inflate the menu; this adds items to the action bar if it is present.
- super.onCreateOptionsMenu(menu, inflater);
- String brand = PhoneUtils.getPhoneBrand();
- if ("mx4pro".equals(brand)) {
- inflater.inflate(R.menu.overflow_menu, menu);
- } else {
- inflater.inflate(R.menu.main, menu);
- }
- }
溢出菜单文件/res/menu/main.xm
- <menu xmlns:android="http://schemas.android.com/apk/res/android" >
- <item
- android:id="@+id/menu_hotest"
- android:orderInCategory="100"
- android:showAsAction="never"
- android:title="@string/menu_hotest"/>
- <item
- android:id="@+id/menu_lastest"
- android:orderInCategory="100"
- android:showAsAction="never"
- android:title="@string/menu_lastest"/>
- </menu>
自定义的菜单文件/res/menu/overflow_menu.xml自定义菜单就直接给它设置一个actionbar的按钮,图标是那三个点
- <?xml version="1.0" encoding="utf-8"?>
- <menu xmlns:android="http://schemas.android.com/apk/res/android" >
- <item
- android:id="@+id/menu_overflow"
- android:icon="@drawable/ic_more"
- android:orderInCategory="100"
- android:showAsAction="always"
- android:title="@string/menu_overflow">
- </item>
- </menu>
我们在选中菜单的时候执行我们的业务逻辑
- @Override
- public boolean onOptionsItemSelected(MenuItem item) {
- switch (item.getItemId()) {
- case R.id.menu_overflow:
- popupOverflowMenu();
- return true;
- case R.id.menu_lastest:
- type = "latest";
- break;
- case R.id.menu_hotest:
- type = "hotest";
- break;
- default:
- break;
- }
- return super.onOptionsItemSelected(item);
- }
如果是魅族mx4pro手机,就弹出我们的自定义菜单,下面是实现:
- /**
- * 弹出自定义溢出菜单
- */
- public void popupOverflowMenu() {
- // 显示溢出菜单的时候设置背景半透明
- setWindowAlpha(0.5f);
- // 获取状态栏高度
- Rect frame = new Rect();
- getActivity().getWindow().getDecorView().getWindowVisibleDisplayFrame(frame);
- // 状态栏高度 frame.top
- int xOffset = frame.top + getActivity().getActionBar().getHeight() - 25; // 减去阴影宽度,适配UI
- int yOffset = Dp2Dx(getActivity(), 8f); // 设置x方向offset为5dp
- View parentView = getActivity().getLayoutInflater().inflate(R.layout.fragment_portfolio, null);
- View popView = getActivity().getLayoutInflater().inflate(R.layout.action_overflow_menu, null);
- // popView即popupWindow布局
- PopupWindow popupWindow = new PopupWindow(popView, LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, true);
- // 必须设计BackgroundDrawable后setOutsideTouchable(true)才会有效。这里在XML中定义背景,所以这里为null
- popupWindow.setBackgroundDrawable(new ColorDrawable(0000000000));
- popupWindow.setFocusable(true);
- popupWindow.setOutsideTouchable(true); // 点击外部关闭
- popupWindow.setAnimationStyle(android.R.style.Animation_Dialog);
- // 设置Gravity,让它显示在右上角
- popupWindow.showAtLocation(parentView, Gravity.RIGHT | Gravity.TOP, yOffset, xOffset);
- popupWindow.setOnDismissListener(new OnDismissListener() {
- @Override
- public void onDismiss() {
- // popupWindow消失时,设置为全透明
- setWindowAlpha(1f);
- }
- });
- }
- /**
- * 设置屏幕透明度
- * @param alpha
- */
- private void setWindowAlpha(float alpha) {
- WindowManager.LayoutParams lp = getActivity().getWindow().getAttributes();
- lp.alpha = alpha; //0.0-1.0
- getActivity().getWindow().setAttributes(lp);
- }
- public int Dp2Dx(Context context, float dp) {
- final float scale = context.getResources().getDisplayMetrics().density;
- return (int) (dp * scale + 0.5f);
- }
这里就是通过自定义pupupwindow,指定popupwindow的xml布局,这个自己来定,根据父布局来显示popupwindow的位置,当popupwindow显示的时候,设置屏幕为半透明,监听它的消失事件,消失的时候,设置屏幕为全透明效果。最终实现适配魅族手机,这里是小巫想到的一个解决方案,有更好的方法,可以留言交流一下。
http://blog.csdn.net/wwj_748/article/details/44588649
0 0
- Android记录23-关于actionbar中overflow menu(溢出菜单)中的一些问题
- Android记录23-关于actionbar中overflow menu(溢出菜单)中的一些问题
- 关于actionbar overflow menu(溢出菜单)不显示的解决办法
- android开发action bar中menu菜单action overflow问题
- 关于Android ActionBar 一直显示overflow的一些问题
- android--------关于ActionBar的Overflow问题
- 仿ActionBar overflow溢出,竖线菜单效果
- Android actionbar Menu 和 overflow的自定义
- Android 中菜单(Menu)键和ActionBar
- ActionBar中,menu 一些细节!
- Actionbar 中解决溢出菜单不显示的问题
- Actionbar 中解决溢出菜单不显示的问题
- Actionbar 中解决溢出菜单不显示的问题
- Actionbar 中解决溢出菜单不显示的问题
- Android学习记录(五) 记录一下最近遇到的坑:android actionbar menu菜单无显示。
- ActionBar上的overflow menu显示位置的问题
- 在ActionBar / ToolBar 中添加 overflow menu的方法
- actionbar强制显示overflow menu
- 关于Android文字转语音(TTS)开发
- MySQL加密和解密
- IOS应用程序多语言本地化解决方案
- 开源社区第19期JAVA架构培训班开始报名啦!每期只招十个,报名抓紧时间啦!
- PT协程简介
- Android记录23-关于actionbar中overflow menu(溢出菜单)中的一些问题
- MyBatis 错误总结
- Android Studio使用笔记
- Scalaz(2)- 基础篇:随意多态-typeclass, ad-hoc polymorphism
- ArcGIS的shapefile图层有别名和真实名称,获取真实名称的方法
- visual studio 解决方案项目结构部署和配置
- Singleton 单例模式(懒汉方式和饿汉方式)
- android 资源之 color
- 数塔(2084)