如何在Android菜单选项中添加带文本的图像?

时间:2016-08-25 07:05:10

标签: java android android-layout android-menu android-icons

我尝试使用文本和图片实现自定义菜单选项,如下图所示:

Menu Screenshot

不幸的是,我不知道如何实现这一点。

我的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"
    tools:context="com.example.admin.ddcart.LIST">

    <item android:id="@+id/action_settings1"
        android:title="@string/action_settings1"
        android:orderInCategory="100" 
        app:showAsAction="never" />

    <item android:id="@+id/action_settings2"    
        android:title="@string/action_settings2"
        android:orderInCategory="100" 
        app:showAsAction="never" />

     <item android:id="@+id/action_settings3" 
         android:title="@string/action_settings3"
         android:orderInCategory="100" 
         app:showAsAction="never" />

</menu>

6 个答案:

答案 0 :(得分:2)

将属性中的android:icon=@drawable/your_image"添加到商品标记

 <item android:id="@+id/action_settings2"    
       android:title="@string/action_settings2"
       android:icon=@drawable/your_image"/>

您需要使用微调器参考这些链接http://www.androidhive.info/2013/11/android-working-with-action-bar/

https://developer.android.com/guide/topics/ui/controls/spinner.html

答案 1 :(得分:2)

如果您的图片位于mipmap目录中,请使用

android:icon=@mipmap/your_image"

如果您的图片位于drawable目录中,请使用

android:icon=@drawable/your_image"

图标更喜欢在

中添加四个drawable
  • drawable-hdpi图片大小36 * 36
  • drawable-mdpi图片大小24 * 24
  • drawable-xhdpi图片大小48 * 48
  • drawable-xxhdpi图片大小72 * 72
  • drawable-xxxdpi图片大小96 * 96

您也可以使用矢量

在drawable目录中创建精美的名为 delete.xml

    <?xml version="1.0" encoding="UTF-8" standalone="no"?>
<vector xmlns:android="http://schemas.android.com/apk/res/android" android:height="24dp" android:viewportHeight="24.0" android:viewportWidth="24.0" android:width="24dp">
    <path android:fillColor="#FFFFFF" android:pathData="M6,19c0,1.1 0.9,2 2,2h8c1.1,0 2,-0.9 2,-2V7H6v12zM19,4h-3.5l-1,-1h-5l-1,1H5v2h14V4z"/>
</vector>

并在商品标记中使用android:icon=@drawable/your_image"

完整代码

<item android:id="@+id/action_settings2"    
   android:title="@string/action_settings2"
   android:icon=@drawable/your_image"/>

为了生成 png 和向量,请使用此plugin

轻松调整颜色,大小等 创建 png 向量

=&GT;单击

关闭所有尺寸的 png&#39>

enter image description here

答案 2 :(得分:2)

这样您就可以将图标设置为菜单

http://keepsafe.github.io/2014/11/19/building-a-custom-overflow-menu.html

public void onClick(View v) {
    PopupMenu popupMenu = new PopupMenu(mContext, v);
    popupMenu.inflate(R.menu.album_overflow_menu);

    // Force icons to show
    Object menuHelper;
    Class[] argTypes;
    try {
        Field fMenuHelper = PopupMenu.class.getDeclaredField("mPopup");
        fMenuHelper.setAccessible(true);
        menuHelper = fMenuHelper.get(popupMenu);
        argTypes = new Class[] { boolean.class };
        menuHelper.getClass().getDeclaredMethod("setForceShowIcon", argTypes).invoke(menuHelper, true);
    } catch (Exception e) {
        // Possible exceptions are NoSuchMethodError and NoSuchFieldError
        //
        // In either case, an exception indicates something is wrong with the reflection code, or the 
        // structure of the PopupMenu class or its dependencies has changed.
        //
        // These exceptions should never happen since we're shipping the AppCompat library in our own apk, 
        // but in the case that they do, we simply can't force icons to display, so log the error and
        // show the menu normally.

        Log.w(TAG, "error forcing menu icons to show", e);
        popupMenu.show();
        return;
    }

    popupMenu.show();
}

<强>输出 enter image description here

答案 3 :(得分:0)

只需将图像作为icon变量添加到菜单项中。

 <?xml version="1.0" encoding="utf-8"?>
    <menu xmlns:android="http://schemas.android.com/apk/res/android">
        <group
            android:id="@+id/menu_top"
            android:checkableBehavior="single"
            >
            <item
                android:checked="true"
                android:id="@+id/drawer_gmail"
                android:icon="@drawable/gmail"
                android:title="Gmail"/>
            <item
                android:id="@+id/bluetooth"
                android:icon="@drawable/bluetooth"
                android:title="Bluetooth" >
            </item>
...
</group>


</menu>

答案 4 :(得分:0)

您可以在“菜单”中设置列表视图,然后通过java代码创建一个特殊的适配器来“膨胀”文本和图像一起布局“。创建一个包含imageview和textview的布局,使用特殊的适配器进行膨胀并将结果传输到listview,这样你就可以得到一个菜单。

如果您在执行以下评论时遇到错误..

答案 5 :(得分:0)

<?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/setting"
        android:title="Setting"
        android:icon="@drawable/ic_settings_black_24dp"/>

    <item android:id="@+id/profile"
        android:title="Profile"
        android:icon="@drawable/ic_account_circle_black_24dp"/>

    <item android:id="@+id/chat"
        android:title="Chat"
        android:icon="@drawable/ic_chat_black_24dp"/>
</menu>