Cannot Display Icon In The Options Menu

2020-06-29 09:40发布

问题:

Hello guys I am really frustrated this is what I want to get

but I what ever I do I cannot get my icon bonded to my Options Menu. I have tried

@Override
public boolean onCreateOptionsMenu(Menu menu)
{
    menu.add(1, 1, 0, "Blu-Ray").setIcon(R.drawable.ic_launcher);
     menu.add(1, 2, 1, "DVD").setIcon(R.drawable.ic_launcher);
     menu.add(1, 3, 2, "Hard Disk").setIcon(R.drawable.ic_launcher);
     menu.add(1, 4, 3, "Sites").setIcon(R.drawable.ic_launcher);
     menu.add(1, 5, 4, "USB").setIcon(R.drawable.ic_launcher);
 return true;
}

but with no luck

I then tried to go to my menu then main.xml and did

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

<item
    android:id="@+id/action_settings"
    android:icon="@drawable/ic_launcher"
    android:showAsAction="never"
    android:title="Lolly"/>

And in my Class I did

    @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;
}

回答1:

Cannot Display Icon In The Options Menu

Note that there is no more "options menu" as of Android 3.0. There is the action bar overflow. "Options menu" is still used in some method names (e.g., onCreateOptionsMenu()) for backwards compatibility, but the "options menu" itself is an Android 1.x/2.x construct.

Hello guys I am really frustrated this is what I want to get

That is not an options menu, nor the action bar overflow. It is some UI that the developer of that app created independently.

but I what ever I do I cannot get my icon bonded to my Options Menu

The action bar overflow does not have icons. The old options menu had icons, but with a different rendering than is depicted in your screenshot.

I would like to achieve the layout as above

You are welcome to create some UI for this yourself. Or, you can stick to the standard action bar/options menu approach, so that your application will behave consistently with the other applications on the users' devices.