2016-08-25 23 views
3

我試圖實現一個定製的菜單選項與文字和圖像,看起來像這個圖像enter image description here如何在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> 
+0

用這個問題是這個環節的重複問題http://stackoverflow.com/a/28953241/5305430 – sushildlh

+0

這2個其他鏈接,幫助您HTTP ://android-dev-talk.blogspot.in/2012_05_20_archive.html和http://stackoverflow.com/a/28238747/5305430 – sushildlh

回答

2

如果你的圖片是在mipmap DIR然後用

android:[email protected]/your_image" 

如果你的圖片是在drawable DIR然後用

android:[email protected]/your_image" 

圖標喜歡在

  • 繪製增加四個繪製每個-hdpi image size 36 * 36
  • drawable-mdpi image si澤24 * 24
  • 繪製-xhdpi圖像尺寸48×48
  • 繪製-xxhdpi圖像尺寸72 * 72
  • 繪製-xxxdpi圖像尺寸96 * 96

也可以使用矢量

創建罰款刪除。XML在抽拉DIR

<?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:[email protected]/your_image"

完整代碼

<item android:id="@+id/action_settings2"  
    android:title="@string/action_settings2" 
    android:[email protected]/your_image"/> 

爲了產生的PNG和載體使用本plugin

輕鬆地調整顏色,尺寸等 創建PNG載體

=>生成PNG的關上單一的點擊所有尺寸

enter image description here

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

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

您可以簡單地設置一個列表視圖中的「菜單」,然後通過Java代碼創建一個特殊的適配器吹大「的文字和圖片一起佈局」。創建一個包含imageview和textview的佈局,使用你的特殊適配器來膨脹並將結果傳遞給listview,這樣你就可以得到一個你想要的菜單。

如果你做下面這些評論中有一個錯誤..

相關問題