best way to show the three dot menu item in action bar without using “a hack” method [closed]

断了今生、忘了曾经 提交于 2019-12-22 10:43:10

问题


I am a little confused. I want to show the remaining items of the action bar inside the three dot item. But i always lose it when the number of items increases ! after googling a little, i found this "useful" method : ASMUIRTI ANSWER

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

I tested it and that work as i want. Unfortunately, triing to better understand what does this method do, i found that it's an awful hack that breaks consistency with the rest of the apps on the platform (adamp's comment)

Can somebody tell me why it's a hack, and what is the best solution ?


回答1:


It's simple. Some devices have menu buttons others don't. On devices with menu buttons the overflowing action items are shown when pressing the menu button and on devices without they are shown when pressing the three dots. That's how the device manufacturer designed their devices and any attempt to force them to do it differently is regarded a "hack". It's inconsistent because other apps on the same device do it differently.

It all comes down to the question whether you want to have consistency for a single app across different devices or consistency for all apps on a single device. Because app development should be about users in the end, IMO the consistency on a single device (for one user) is what really counts.

The best way to deal with this is to use the android:showAsAction tags and let Android decide, how to display the actions in the ActionBar, whether an overflow menu is needed or not and if yes how the user accesses the overflowing action items.




回答2:


The Overflow menu is not shown for devices with a menu button.

The code you used will work fine on a more recent device and the overflow (three dots) will be displayed. As intented, these are not shown when there is a menu button

There is no way without a dirty hack that will break soon or later to force these dots to be dsiplayed on all devices




回答3:


So, turns out it's pretty simple, I recently implemented in my app.

The items that need to be shown in the overflow menu, nest them under one menu item as follows:

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

    <item
        android:id="@+id/empty"
        android:orderInCategory="101"
        android:showAsAction="always"
        android:icon="@drawable/ic_action_overflow">

        <menu>        

            <item
                android:id="@+id/action_settings"
                android:orderInCategory="96"
                android:showAsAction="never"
                android:title="@string/menu_settings"
                android:icon="@drawable/ic_action_settings"/>

            <item
                android:id="@+id/action_share"
                android:orderInCategory="97"
                android:showAsAction="never"
                android:title="@string/menu_share"
                android:icon="@drawable/ic_action_share"/>

            <item
                android:id="@+id/action_rate"
                android:orderInCategory="98"
                android:showAsAction="never"
                android:title="@string/menu_rate"
                android:icon="@drawable/ic_action_important"/>

            <item
                android:id="@+id/action_feedback"
                android:orderInCategory="99"
                android:showAsAction="never"
                android:title="@string/menu_feedback"
                android:icon="@drawable/ic_action_edit"/>

            </menu>         
        </item>
</menu>

Now, edit the main activity file as follows:

package com.example.test;
//all your import statements go here

Menu mainMenu=null;

@Override
protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState); }

@Override
public boolean onCreateOptionsMenu(Menu menu) {     
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
mainMenu=menu;
return true; }


//Menu press should open 3 dot menu
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
    if (keyCode==KeyEvent.KEYCODE_MENU) {
        mainMenu.performIdentifierAction(R.id.empty, 0);
        return true; }
    return super.onKeyDown(keyCode, event); }


来源:https://stackoverflow.com/questions/18237450/best-way-to-show-the-three-dot-menu-item-in-action-bar-without-using-a-hack-me

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