Android Action Bar Three Dots not displayed

2020-03-01 11:55发布

问题:

Please help, I made a custom menu (added support libraries) (name-> main_activity_actions.xml)

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:yourapp="http://schemas.android.com/apk/res-auto"  >

<item
    android:id="@id/search"
    android:icon="@drawable/search"
    android:title="@string/search"
    yourapp:showAsAction="ifRoom" />
<item
    android:id="@id/view_all"
    android:title="@string/view_all"
    yourapp:showAsAction="never"/>
<item
    android:id="@+id/action_settings"
    yourapp:showAsAction="never"
    android:title="@string/action_settings"/>

Now what should i do to put action_settings into three dots (of action bar), instead of hardware menu button (Without any hack).

MainActivity

@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_activity_actions, menu);
    return true;
}

well i have found the hack but if there is any other way then let me know,
Hack
put this code in onCreate

 try {
        ViewConfiguration config = ViewConfiguration.get(this);
        Field menuKeyField = ViewConfiguration.class.getDeclaredField("sHasPermanentMenuKey");
        if(menuKeyField != null) {
            menuKeyField.setAccessible(true);
            menuKeyField.setBoolean(config, false);
        }
    } catch (Exception ex) {
        // Ignore
    }

for this you need to import

import java.lang.reflect.Field;
import android.view.ViewConfiguration;

回答1:

Without any hack, you cannot do that on all devices. Those devices that have the hardware menu button (I'm not sure if absolutely all) will use it instead of the overflow button (...).

Which, sort of, is a good thing. Users of those devices are used to pressing the menu button to get to the menu. So for them, the lack of the overflow button is the normal behaviour.

For those devices that use the overflow button, Android will decide itself what to put where based on your hints in showAsAction tag. It depends on the screen size, orientation, among other things. This page has a table showing how many icons are displayed (the rest goes to overflow menu).



回答2:

If you want to show the three dots, irrespective of device menu button! then you can call this method in your application class' onCreate method-

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


回答3:

Please test This code for Display SherlokActionbar :

public class MainActivity extends SherlockActivity {
private com.actionbarsherlock.view.MenuItem mGoItem;
private com.actionbarsherlock.view.MenuItem mClearItem;

private static final int listSMS_ITEM_ID = 1;
private static final int Distance_ITEM_ID = 5;
private static final int About_ITEM_ID = 2;
private static final int Search_ITEM_ID = 3;
private static final int HELP_ITEM_ID = 4;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
}

public boolean onCreateOptionsMenu(Menu menu) {

    mGoItem = menu.add(0, HELP_ITEM_ID, 0, null);
    mGoItem.setIcon(R.drawable.refresh).setShowAsAction(
            MenuItem.SHOW_AS_ACTION_ALWAYS);

    mGoItem = menu.add(0, Distance_ITEM_ID, 0, null);
    mGoItem.setIcon(R.drawable.ic_launcher).setShowAsAction(
            MenuItem.SHOW_AS_ACTION_ALWAYS);

    mGoItem = menu.add(0, listSMS_ITEM_ID, 0, null);
    mGoItem.setIcon(R.drawable.refresh).setShowAsAction(
            MenuItem.SHOW_AS_ACTION_ALWAYS);

    mGoItem = menu.add(0, Search_ITEM_ID, 0, null);
    mGoItem.setIcon(R.drawable.ic_launcher).setShowAsAction(
            MenuItem.SHOW_AS_ACTION_ALWAYS);

    return true;
}
// @Override
public boolean onOptionsItemSelected(
        com.actionbarsherlock.view.MenuItem item) {
    // TODO Auto-generated method stub
    /* return super.onOptionsItemSelected(item); */

    switch (item.getItemId()) {
    case listSMS_ITEM_ID:

        Toast.makeText(getApplicationContext(), "listSMS", 1).show();


        return true;

    case Search_ITEM_ID:

        Toast.makeText(getApplicationContext(), " Search", 1).show();

        return true;




    case Distance_ITEM_ID:

        Toast.makeText(getApplicationContext(), "  Distance", 1).show();
        return true;



        case HELP_ITEM_ID:
            Toast.makeText(getApplicationContext(), "  HELP", 1).show();
            //

            return true;


    }

    return false;
}