Android record 23-some questions about overflow menu in actionbar, 23-actionbar

Source: Internet
Author: User

Android record 23-some questions about overflow menu in actionbar, 23-actionbar
Android record 23-questions about overflow menu in actionbar


Preface

This blog record some problems that Xiao Wu encountered when using actionbar about the overflow menu:


Let's take a look at two comparison figures:

On the left is the effect of the red mi phone, and on the right is the Adaptation Effect for meizu Mx4pro.
What is an overflow menu? First, let's explain what is an overflow menu, which is called an "overflow menu" in Chinese. as its name implies, some of our mobile phones have solid keys or virtual keys, one of which is a menu key, some may not. For example, meizu only has one back returned virtual key. When you click the menu key, an overflow menu will pop up at the bottom. The ActionBar control can be added after Android 3.0, that is, after API 11, and there will be three points in the upper right corner, that is the overflow menu we are talking about. we can define the menu items in the menu to bring up the pop-up effect similar to the above Red Rice phone. There are many ways to use ActionBar. I will not talk about this blog. If you are interested, you can go to the official website to learn more.


How to forcibly display the overflow menu in ActionBar? By default, no overflow menu is displayed for mobile phones above Android 3.0. How can I forcibly display overflow menus on mobile phones below Android 4.4? You can use the following methods:
// Force the actionbar to display the overflow menu // force to show overflow menu in actionbar for android 4.4 belowprivate 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 ();}}


Compatible with solutions similar to meizu mobile phones that cannot display overflow menus the above solution has solved how to forcibly display overflow menus on mobile phones lower than Android 4.4. Below we will solve the problem that some wonderful mobile phones cannot display overflow menus. Why does meizu mobile phone fail to display? What I learned is that meizu does not have the so-called actionbar, which is called smartbar. It seems that meizu engineers have modified the official actionbar. One word! However, Xiao Wu came up with a solution. Each of our mobile phones has its own brand. We can adapt to these wonderful mobile phones. Use popupwindow instead.
Obtain mobile phone information
/*** Get IMEI, IESI, mobile phone model */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; // mobile phone MODEL String mtyb = android. OS. build. BRAND; // mobile BRAND String numer = mTm. getLine1Number (); // mobile phone number. Some are available, and some cannot get logs. I ("text", "Mobile Phone IMEI number:" + imei + "Mobile Phone IESI number:" + imsi + "Mobile Phone model:" + mtype + "mobile phone brand: "+ mtyb +" mobile phone number "+ numer );}

Get mobile phone brand
/*** Get the mobile phone brand * @ return */public static String getPhoneBrand () {return android. OS. Build. BOARD ;}

After obtaining the mobile phone brand, we can determine when creating the overflow menu: 1. for meizu mobile phones, load the custom menu 2. if not, use the system overflow menu to load the menu content.
@Overridepublic 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);}}


Overflow menu file/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>

Custom menu file/res/menu/overflow_menu.xml
<?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>
The custom menu directly sets an actionbar button. the icon is the three points.

We execute our business logic when selecting the menu
@Overridepublic 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);}

If it is meizu mx4pro, our custom menu will pop up. below is the implementation:
/*** Pop-up custom overflow menu */public void popupOverflowMenu () {// set the background translucent set0000walpha (0.5f) when the overflow menu is displayed ); // get the status bar height Rect frame = new Rect (); getActivity (). getWindow (). getDecorView (). getWindowVisibleDisplayFrame (frame); // frame of the status bar height. topint xOffset = frame. top + getActivity (). getActionBar (). getHeight ()-25; // subtract the shadow width, applicable to UIint yOffset = Dp2Dx (getActivity (), 8f); // set the offset of x to 5 dpView parentView = getActivity (). get LayoutInflater (). inflate (R. layout. fragment_portfolio, null); View popView = getActivity (). getLayoutInflater (). inflate (R. layout. action_overflow_menu, null); // popView is popupWindow layout PopupWindow popupWindow = new PopupWindow (popView, LayoutParams. WRAP_CONTENT, LayoutParams. WRAP_CONTENT, true); // It is valid only after BackgroundDrawable is designed and setOutsideTouchable (true. The background is defined in XML, so nullpopupWindow is used here. setBackgroundDrawable (new ColorDrawable (0000000000); popupWindow. setFocusable (true); popupWindow. setOutsideTouchable (true); // click the external button to close the popupWindow. setAnimationStyle (android. r. style. animation_Dialog); // set Gravity to display it in the popupWindow in the upper right corner. showAtLocation (parentView, Gravity. RIGHT | Gravity. TOP, yOffset, xOffset); popupWindow. setOnDismissListener (new OnDismissListener () {@ Overridepublic void onDismiss () {// when popupWindow disappears, set it to fully transparent setjavaswalpha (1f );}});} /*** set screen transparency ** @ param alpha */private void setpaiwalpha (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 );}

In this example, you can customize pupwindow to specify the xml layout of popupwindow. this parameter is set by yourself. The position of popupwindow is displayed based on the parent layout. When popupwindow is displayed, the screen is set to translucent, listen to its disappearance event. When it disappears, set the screen to completely transparent. Finally, it is compatible with meizu mobile phones. Here is a solution that Xiao Wu thought of. If there is a better solution, please leave a message.



Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.