问题
In my application, I am using ActionBarSherlock 4.4.
Since ForcedOverflow has been removed from the latest version, I used the following XML code to replicate the OverflowMenu.
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="@+id/menu_overflow"
android:icon="@drawable/abs__ic_menu_moreoverflow_holo_dark"
android:orderInCategory="100"
android:showAsAction="always">
<menu>
<item
android:id="@+id/action_settings"
android:showAsAction="never"
android:title="@string/action_settings" />
<item
android:id="@+id/recycleBin"
android:showAsAction="never"
android:title="Recycle Bin" />
</menu>
</item>
</menu>
Overflow is working perfectly for me now, both for Android 4.3 and 2.3.3.
My problem :
The 2.3.3 device has a hardware Menu key. I want that when the Menu Key is pressed, the Overflow OptionsMenu should be opened.
I used the following code in Activity, but it doesn't work. (I am getting the messages in my LogCat though)
@Override
public boolean onKeyUp(int keyCode, KeyEvent event) {
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.HONEYCOMB) {
if (event.getAction() == KeyEvent.ACTION_UP &&
keyCode == KeyEvent.KEYCODE_MENU) {
Log.d("HomeActivity", "OpenOptionsMenu");
openOptionsMenu();
return true;
}
}
return super.onKeyUp(keyCode, event);
}
I looked around a lot, and tried all suggested solutions. But nowhere anyone is talking about opening a custom overflow menu using openOptionsMenu()
.
Am I missing something here?
Is there a way by which I can make it seem as if the parent overflow icon item android:id="@+id/menu_overflow"
has been clicked ?
It would be great if someone pointed out what I am missing out here.
回答1:
I was having the same issue as you today. I think this is because ActionBarSherlock has its own implementation of the options menu which overrides Android's one, disabling the openOptionsMenu()
method in the process. Maybe it's a bug in the library?
Anyway, I solved this by overriding onKeyUp
in the activity containing the menu as follows:
@Override
public boolean onKeyUp(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_MENU) {
View v = findViewById(R.id.YOUR_MENU_VIEW_HERE);
v.performClick();
return true;
}
return super.onKeyUp(keyCode, event);
}
Hope it helps.
回答2:
This is how I solved the issue but Ricardo's answer works as well.
private Menu optionsMenu;
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getSupportMenuInflater().inflate(R.menu.display_all, menu);
optionsMenu = menu;
return true;
}
@Override
public boolean onKeyUp(int keyCode, KeyEvent event) {
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.HONEYCOMB) {
if (event.getAction() == KeyEvent.ACTION_UP &&
keyCode == KeyEvent.KEYCODE_MENU) {
//openOptionsMenu();
optionsMenu.performIdentifierAction(R.id.menu_overflow, 0);
return true;
}
}
return super.onKeyUp(keyCode, event);
}
来源:https://stackoverflow.com/questions/18937711/openoptionsmenu-not-working-with-actionbarsherlock-custom-submenu