Show options menu button on Galaxy Nexus (Android 4.1)

£可爱£侵袭症+ 提交于 2019-12-17 18:43:50

问题


I am using options menu in my app. But when I install the app on a Galaxy Nexus, I don't see options menu because by default it doesn't have options menu button. How can I show options menu button in my App for only Galaxy Nexus? With other phones it's fine, and I can get options Menu.


回答1:


In Android 3.0, Google switched from having a dedicated menu key to using the ActionBar for menu functionality. If your build target is 3.0 or above, your menu will default to using the ActionBar.

If there is a menu key on the phone, any run over options that are usually shown by the three dots icon in the action bar will instead show up with the menu button. If there is no menu key on the phone, all options will be shown on the action bar. Because the menu key is going away, it is encouraged that you utilize the ActionBar class for your menus from 3.0 and beyond.

Now, older apps that run on new platforms without a menu key will generate a fake menu key next to the recent apps button. This makeshift menu key will only appear if your build target is 2.3.4 or below since it's only there for backwards compatibility reasons. (edit: build target should be anything less than, or including API 13)

In your case, you should utilize the ActionBar for your menu since you're building against a newer version of Android. If you're worried about backwards compatibility then you can always utilize a library such as ActionBarSherlock to use the action bar on older platforms.




回答2:


You need to think about why you want an "options" button - this is an old, poor pattern, which has been superceded by the ActionBar which really is much nicer.

But you might need it, for client reasons (like I needed to keep it in for the client). If that's the case, then you can follow my advice below (slightly edited from original that mistakely mentioned "action bar")


You can set your build target to a version before ICS (i.e. up to and including API 13 / HoneyComb 3.2).

<uses-sdk
    android:minSdkVersion="7"
    android:targetSdkVersion="13" />

That will get the system to use a compatibility mode setting that inserts an overflow icon into your phone's bottom button bar - this should provide the functionality you are after. Pressing this button will have the same response as pressing a "menu" button on the Samsung Galaxy S2 for example.


Here you can see the compatibility "options" button, and the menu that has been shown when it was pressed:


Note:

As other commentators mention, you should consider the better choice of the ActionBar pattern. If you want backwards compatibility, then I recommend the Action Bar Sherlock library.

But if you are looking for a quick fix, or the client can't afford a big refactor to move to the Action Bar pattern, then this is the way to do it.




回答3:


If you use the Action Bar, the option menu is replaced by the overflow. If not, you have to implement a button yourself, somewhere in your own application.

The menu button (otherwise known as Compatibility Menu of Shame) is only displayed in the System bar for application targeting API 13 or lower

Here is a way to check if the device has a hardware menu key.

// Menu key
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH) {
    hasPermanentMenuKey = hasPermanentMenuKey(context.getApplicationContext());
}
else {
    // No way to know for sure...
    // All devices before Honeycomb should have a menu key. 
    // Honeycomb device usually don't have one
    hasPermanentMenuKey = Build.VERSION.SDK_INT < Build.VERSION_CODES.HONEYCOMB;
}

@TargetApi(Build.VERSION_CODES.ICE_CREAM_SANDWICH)
private boolean hasPermanentMenuKey(Context context) {
    return ViewConfiguration.get(context).hasPermanentMenuKey();
}



回答4:


Options menu is stopped from android version 3.x (Honeycomb). Therefor it is not mandatory that option menu button to be present on latest phones. So go for action bar. You can use Sherlock ActionBar library to support action bar in all platform.

Update : User ActionBarCompat



来源:https://stackoverflow.com/questions/15250024/show-options-menu-button-on-galaxy-nexus-android-4-1

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!