Menu Items are not showing on Action Bar

2019-01-07 05:59发布

I have made a completely new project. I have added items to the menu layout file. Those items do not show up on the action bar's right side. I remember that an icon with three dots shows up which opens up the menu.

enter image description here

Here is my Activity

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ActionBar actionBar = getActionBar();
        actionBar.setBackgroundDrawable(new ColorDrawable(Color.BLUE));     
        actionBar.show();
    }

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

}

And here is my main.xml

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

    <item
        android:id="@+id/action_settings"
        android:orderInCategory="100"
        android:showAsAction="never"
        android:title="@string/action_option1"/>
    <item
        android:id="@+id/action_settings34"
        android:orderInCategory="100"
        android:showAsAction="never"
        android:title="@string/action_option2"/>
    <item
        android:id="@+id/action_settings3"
        android:orderInCategory="100"
        android:showAsAction="never"
        android:title="@string/action_option3"/>


</menu>

11条回答
劳资没心,怎么记你
2楼-- · 2019-01-07 06:43

I think you want to show the overflow menu icon on the top right always and want your menu to open from there. Try something like following to force it there:

Inside your onCreate() do:

  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
    }

It will force the App to show the overflow menu. The menu button will still work, but it will open the menu in the top right corner. Taken from this answer at How to force use of overflow menu on devices with menu button . This might be a kind of 'hack' but it works.

Edit:

Based on useful comments from @gunar, I request not to use this method. Following are the reasons:

  • The private field sHasPermanentMenuKeycan be refactored in a future releases. Your app will stop working then.

  • Also If you force moving action items to action bar because you're used to them, you'll end up confusing users that are used to how apps run on their device. Those with hardware menu key will be used to have the options showed from menu hardware key. If you intentionally show them the three dots of overflow menu, it might confuse them.

So let all the apps be uniform in that sense. Those devices with no hardware key for menu items will show those automatically.

查看更多
Evening l夕情丶
3楼-- · 2019-01-07 06:43

OR you could create a structure like:

<?xml version="1.0" encoding="utf-8"?>
<menu >
<item
    android:id="@+id/menu_main"
    android:icon="@drawable/icon_menu"
    android:showAsAction="always"
    android:title="@string/menu_A"
    android:visible="true">
    <menu>
        <item
            android:id="@+id/menu_settings1"
            android:showAsAction="never"
            android:title="@string/menu_A1"/>
        <item
            android:id="@+id/menu_settings2"
            android:showAsAction="never"
            android:title="@string/menu_A2"/>
        <item
            android:id="@+id/menu_settings3"
            android:showAsAction="never"
            android:title="@string/menu_A3"/>
    </menu>
</item>
</menu>

Where the icon_menu you can create a new icon set with the three points icon clipart.

查看更多
我想做一个坏孩纸
4楼-- · 2019-01-07 06:45

Add

Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);

in the onCreate method.

查看更多
不美不萌又怎样
5楼-- · 2019-01-07 06:49

You need to call this function in onCreate function of the activity.

RequestWindowFeature (Window.FEATURE_ACTION_BAR);
查看更多
一夜七次
6楼-- · 2019-01-07 06:51

After searching and reading a lot of answer, I found that there is another little thing that you should pay attention to.

At least it was my problem and I didn't find an answer of any above. On your .XML file. Your android:showAsAction= shouldn't be form android package, it should be from app package like in the example below:

app:showAsAction=

Don't forget to import the app package.

查看更多
登录 后发表回答