簡體   English   中英

如何在Android菜單選項中添加帶文本的圖像?

[英]How to add an image with text in menu options in Android?

我正在嘗試使用文本和圖像實現自定義菜單選項,如下圖所示:

菜單截圖

不幸的是我不知道如何實現這一點。

我的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>

在屬性android:icon=@drawable/your_image"添加到item標記中

 <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

如果您的圖像在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和vector,請使用此插件

輕松調整顏色,大小等創建png矢量

=>單擊即可生成各種尺寸的png

在此輸入圖像描述

這樣您就可以將圖標設置為菜單

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();
}

產量 在此輸入圖像描述

只需將圖像作為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>

你可以簡單地在“菜單”中設置一個列表視圖,然后通過java代碼創建一個特殊的適配器來膨脹“文本和圖像一起布局”。 創建一個包含imageview和textview的布局,使用特殊適配器進行膨脹並將結果傳輸到listview,這樣您就可以按照自己的意願使用菜單。

如果您在執行以下評論時遇到錯誤..

<?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>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM