How to always show Android settings button on ActionBar?

半城伤御伤魂 提交于 2019-12-11 02:22:54

问题


How can I always show Android settings button on ActionBar? (4.0+)

I would like to show it even if the device has an hardware button for settings, so it would be same with devices with and without hardware buttons.

This is what I'm talking about: http://oi48.tinypic.com/2j104l0.jpg


回答1:


There is one awful hack which generally have been answered on many questions. Call this from your onCreate():

private 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();
    }
}

It is highly suggested not to use this hack as sHasPermanentMenuKey field can change anytime. However this has been working for me and others uptil now. See

Android action bar not showing overflow

On a second note, the hardware button is present on the phone for a reason. Just showing the addition overflow menu might confuse your users. Ideally a user would be very much accustomed to using his/her personal phone. So, if the user's phone has a hardware button for overflow menu option, then it need not have the icon on the top of action bar. Having an addition button might confuse users due to difference in behavior in different apps.

Hope that helps.

Edit:

In short, its not recommended as sHasPermanentMenuKey field in the Android code can be changed anytime, which will break your app if not found.



来源:https://stackoverflow.com/questions/18134703/how-to-always-show-android-settings-button-on-actionbar

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