Android ActionbarSherlock show menu button at top

孤街浪徒 提交于 2019-12-11 03:53:52

问题


I am trying to implement ActionBarSherlock in my application and I need to show the menu at the top of my application, integrated in action bar. The problem is that it's not working properly. I am using it like this :

public boolean onCreateOptionsMenu(Menu menu) {
    menu.add("Refresh")
        .setIcon(R.drawable.ic_action_refresh)
        .setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS);

    menu.add("Settings")
        .setIcon(R.drawable.ic_action_settings)
        .setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS);
    return true;
}

So the thing which I want to achieve is to show at the right side of my action bar a refresh icon and a menu icon, which should open the default menus when clicked. I checked ForcedOverflowItem example in ActionbarSherlock Demos, but it's not working as I want. I need to look the same as in Android 2.+ and in Android 4.+.

Any advices / helps / suggestions how can I get this to work?


回答1:


From what I read in ActionbarSherlock's documentation you cannot force the menu icon to appear in Android 4.+. When the device has a menu button the menu icon does not appear. I guess the guy who wrote ActionbarSherlock knows the subject matter well ;-)




回答2:


You can do an xml file line this. I saw this in some other post but I cannot track it rigth now.

<menu xmlns:android="http://schemas.android.com/apk/res/android" >

    <item
        android:id="@+id/more"
        android:showAsAction="always"
        android:icon="@drawable/abs__ic_menu_moreoverflow_holo_dark">
        <menu>
            <item
                android:id="@+id/remove"
                android:showAsAction="withText|never"
                android:title="@string/remove">
            </item>
        </menu>
    </item>

</menu>

Then just inflate it like a normal menu.

public boolean onCreateOptionsMenu(Menu menu) {
        getSupportMenuInflater().inflate(R.menu.overlow, menu);
        return true;
    }



回答3:


Please read the dorjeduck's answer. If you want have same experience on all devices you have to add custom menu with his submenus. Here this the code sumple:

SubMenu sub = menu.addSubMenu("More");
sub.setIcon(R.drawable.abs__ic_menu_moreoverflow_holo_dark);
sub.add(0, MENU_SETTINGS, 0, "Settings");


来源:https://stackoverflow.com/questions/12776752/android-actionbarsherlock-show-menu-button-at-top

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