简体   繁体   English

如何在选项菜单中添加图标?

[英]How to add icon into option menu?

I try to add icon into the option menu in my app.我尝试在我的应用程序的选项菜单中添加图标。 I add icon attribute to the items But icons don't show.我向项目添加图标属性但图标不显示。

    <menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto" >
    <item
        android:id="@+id/exit"
        android:icon="@drawable/abc_ic_go"
        android:title="@string/exit"/>
    <item
        android:id="@+id/about_us"
        android:title="@string/about_us"/>
    <item
        android:id="@+id/help"
        android:icon="@drawable/abc_ic_go"
        android:title="@string/help"/>
    <item
        android:id="@+id/setting"
        android:icon="@drawable/abc_ic_go"
        android:title="@string/setting"/>
    <item
        android:id="@+id/search_collection"
        android:icon="@drawable/search"
        android:title="@string/search"
        app:actionViewClass="android.support.v7.widget.SearchView"
        app:showAsAction="always"/>
</menu>

I want my menu like this image.我想要我的菜单像这张图片。

在此处输入图片说明

Use android:showAsAction="always|withText" in all your Items在所有项目中使用android:showAsAction="always|withText"

   <item
        android:id="@+id/help"
        android:icon="@drawable/abc_ic_go"
        android:showAsAction="always|withText"
        android:title="@string/help"/>

Use app:showAsAction="withText|always" in all your Items在所有项目中使用app:showAsAction="withText|always"

  <item
    android:id="@+id/action_settings"
    android:orderInCategory="100"
    android:title="Settings"
    app:showAsAction="withText|always"
    android:icon="@drawable/ic_launcher"/>

I think in your application no sufficient place available for showing icon in the action bar..Rotate the emulator to landscape mode and see it is working or not ?我认为在您的应用程序中没有足够的位置可用于在操作栏中显示图标..将模拟器旋转到横向模式并查看它是否正常工作?

menu/menu_main.xml 菜单/menu_main.xml
@Override
public boolean onCreateOptionsMenu(Menu menu) {
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.menu_main,menu);
    return true;
}

in Your MainActivity 在您的主要活动中
@Override public boolean onCreateOptionsMenu(Menu menu) { MenuInflater inflater = getMenuInflater(); inflater.inflate(R.menu.menu_main,menu); return true; }
result 结果

结果

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM