问题
So, I'm trying to mimic something like Pandora's menu:
Where you have the choice to hit the menu button or icon in the top right corner, either way, it will show the same drop down menu list. I've tried creating an 'overflow' menu but on older devices I found out that the menu appears at the bottom still (which I do not want). So I created a Spinner
and used:
getSupportActionBar().setNavigationMode(getSupportActionBar().NAVIGATION_MODE_LIST);
The problem with this approach is that it displays the name of an item rather than an icon (I assume I would have to provide an icon which is not a problem) and the spinner displays more towards the middle (where as I want it all the way to the right).
So, to sum it all up, is there a way to create a menu, similar to that of Pandora's, that displays an icon to the far right of the ActionBar
and will open by both a press of the icon or menu button? Any advice, tips, or help would be greatly appreciated, thanks!
回答1:
If I understood you want to create a popup menu and show they when the user click on her icon on in some hardware button.
Well, here is a very good explanation of popup menus and if you want to get some device key try this:
public boolean onKeyDown(int keyCode, KeyEvent event) {
switch (keyCode) {
case KeyEvent.KEYCODE_DPAD_CENTER:
Toast.makeText(this, "The Center key was pressed",
Toast.LENGTH_SHORT).show();
return true;
case KeyEvent.KEYCODE_DPAD_RIGHT:
Toast.makeText(this, "The Right key was pressed",
Toast.LENGTH_SHORT).show();
return true;
case KeyEvent.KEYCODE_DPAD_LEFT:
Toast.makeText(this, "The Left key was pressed", Toast.LENGTH_SHORT)
.show();
return true;
case KeyEvent.KEYCODE_BACK:
Toast.makeText(this, "The Back key was pressed", Toast.LENGTH_SHORT)
.show();
//---this event has been handled---
return true;
}
//---this event has not been handled---
return false;
}
I hope this will help you.
回答2:
With some experimentation I managed to get part of what I wanted accomplished. I'm not sure if this is the best approach but it did work.
I found out that the reason why the spinner would not display at the far right of the screen is because that space is reserved for menu items. If you remember, the original reason why I didn't use menu items is because on older devices when you hit the menu button it would display the items at the bottom of the screen (not what I wanted). But I found a work around.
I first created my menu in XML:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="@+id/spinner_menu_item"
android:showAsAction="always"
android:actionLayout="@layout/spinner" />
</menu>
and "@layout/spinner"
:
<?xml version="1.0" encoding="utf-8"?>
<com.actionbarsherlock.internal.widget.IcsSpinner xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/spinner_menu"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
then in the onCreateOptionsMenu:
getSupportMenuInflater().inflate(R.menu.basic_menu, menu);
MenuItem item = menu.findItem(R.id.spinner_menu_item);
spinnerMenu = (IcsSpinner)item.getActionView();
ArrayAdapter<String> adapter = new ArrayAdapter<String>(MyActivity.this, android.R.layout.simple_dropdown_item_1line, menuItems);
spinnerMenu.setAdapter(adapter);
spinnerMenu.setOnItemSelectedListener(new IcsAdapterView.OnItemSelectedListener() {}
and finally I overrided the menu button, like so:
@Override
public boolean onKeyDown(int keyCode, KeyEvent event){
if (keyCode == KeyEvent.KEYCODE_MENU){
spinnerMenu.performClick();
return true;
}
return super.onKeyDown(keyCode, event);
}
The only thing this doesn't do is display an icon rather than the words. But half way there! I hope this will help someone faced with a similar problem.
来源:https://stackoverflow.com/questions/14106399/how-to-create-custom-drop-down-menu-with-actionbarsherlock