Menu icon is not displaying in action bar

丶灬走出姿态 提交于 2019-12-10 13:18:50

问题


I need to inflate custom menu in Fragment.

I have only one menu item.But the icon is not displaying.

Can someone tell what is wrong with my code

My menu.xml

<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"  >

<item
    android:id="@+id/search"
    android:icon="@android:drawable/ic_search_category_default"
    app:showAsAction="always"
    android:title="Search"/></menu>

And I set in onCreateView()

    setHasOptionsMenu(true);
    getActivity().invalidateOptionsMenu();

And inflating the menu

@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
    // TODO Auto-generated method stub

    inflater = getActivity().getMenuInflater();

    inflater.inflate(R.menu.menu, menu);

}

Resulting screen attached below. I need to have the search icon instead of the menu overflow icon.


回答1:


I know I'm a little late for the party, but hope I can help someone else. Today I was facing this SAME problem.

I fixed using android:showAsAction="always" instead of app:showAsAction="always"

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

<item android:id="@+id/bluetooth_status_off"
    android:orderInCategory="0"
    android:icon="@drawable/bluetooth_red"
    android:title="@string/app_name"
    android:showAsAction="always" />
</menu>

on the showAsAction is underlined with red (warning), but works fine.




回答2:


You have to add the following property app:showAsAction="ifRoom"




回答3:


I Think You should use

<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"  >

<item
android:id="@+id/search"
android:icon="@drawable/ic_search_category_default"
app:showAsAction="always"
android:title="Search"/></menu>

and

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.mymenu, menu);
    return true;
}



回答4:


Sorry to late but put this code in your onCreate()

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



回答5:


make a menu with the code below...

<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_settings"/>

   <item 
    android:id="@+id/action_search"
    android:title="Search"
    android:icon="@android:drawable/ic_menu_search"
    android:showAsAction="always"
    android:actionViewClass="android.widget.SearchView" />
 </menu>

and set it as your menu..




回答6:


This is default behaviour of the Overflow menu from the ActionBar. If you want to show the Icons in the overflow menu, override the onMenuOpened method in your activity with this code snippit:

@Override
public boolean onMenuOpened(int featureId, Menu menu) {
    if(featureId == Window.FEATURE_ACTION_BAR && menu != null){
        if(menu.getClass().getSimpleName().equals("MenuBuilder")){
            try{
                Method m = menu.getClass().getDeclaredMethod(
                        "setOptionalIconsVisible", Boolean.TYPE);
                m.setAccessible(true);
                m.invoke(menu, true);
            }
            catch(NoSuchMethodException e){
                Log.e("MyActivity", "onMenuOpened", e);
            }
            catch(Exception e){
                throw new RuntimeException(e);
            }
        }
    }
    return super.onMenuOpened(featureId, menu);
}



回答7:


If you're running your code on Android 3.0+, the icons in the menu are not shown by design. This is a design decision by Google. You can read more about this on Android developers blog.




回答8:


  1. Copy your menu xml code.
  2. Delete your menu file
  3. Now create a new menu file in res->menu directory
  4. And name it with different name
  5. Now Run



回答9:


Make sure, you've added a menu item without an icon. Example:

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

    <item
        android:id="@+id/next_btn"
        android:checked="true"
        android:icon="@android:drawable/ic_input_add"
        android:title="Item"
        app:showAsAction="ifRoom" />

    <item
        android:id="@+id/action_settings"
        android:title="Settings" />

</menu>

Thanks to action_settings, it'll automatically add an overflow icon.



来源:https://stackoverflow.com/questions/27562565/menu-icon-is-not-displaying-in-action-bar

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