2016-05-15 9 views
1

私はアンドロイドで新しいです。私は単純なアイコンをポップアップメニューに追加することはできません。 xmlファイルにアイコンを追加しましたが、表示できません。アイコン付きポップアップメニュー

これは私のメニューである - これはメニュー

を作成することshowPopUpMenuあるmoreActionsButton

moreActionsButton.setOnClickListener(new View.OnClickListener() { 
    @Override 
    public void onClick(View v) { 
     showPopUpMenu(us); 
    } 
}); 

をクリックした後showPopUpMenu関数を呼び出しpop_up_menu.xml

<?xml version="1.0" encoding="utf-8"?> 
<menu xmlns:android="http://schemas.android.com/apk/res/android"> 
    <item 
     android:id="@+id/action_email" 
     android:icon="@mipmap/email" 
     android:title="E-Mail" 
     /> 
    <item 
     android:id="@+id/action_messenger" 
     android:icon="@mipmap/messenger" 
     android:title="Messenger" 
     /> 
    <item 
     android:id="@+id/action_skype" 
     android:icon="@mipmap/skype" 
     android:title="Skype" 
     /> 
    <item 
     android:id="@+id/action_whatsapp" 
     android:icon="@mipmap/whatsapp" 
     android:title="Whatsapp" 
     /> 
</menu> 

私はこのOnClickListenerを持っています

public void showPopUpMenu(final User user) { 
    View menuItemView = getView().findViewById(R.id.groupLeave); 
    PopupMenu popUpMenu = new PopupMenu(getActivity(), menuItemView); 
    popUpMenu.getMenuInflater().inflate(R.menu.pop_up_menu, popUpMenu.getMenu()); 
    popUpMenu.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() { 
     public boolean onMenuItemClick(MenuItem item) { 
      switch (item.getItemId()) { 
       case R.id.action_messenger: 
        onMessengerClick(user); 
        break; 

       case R.id.action_skype: 
        onSkypeClick(user); 
        break; 

       case R.id.action_whatsapp: 
        onWhatsappClick(user); 
        break; 

       case R.id.action_email: 
        onEmailClick(user); 
        break; 

       default: 
        break; 

      } 
      return true; 
     } 
    }); 
    popUpMenu.show(); 
} 
+0

どのポップアップメニューを作成しようとしていますか?私はこれらのアイコンがメニューではなく、ツールバーでのみ使用されると思います。ここでメニュータイプを参照してください。 http://developer.android.com/guide/topics/ui/menus.html –

答えて

3

デフォルトでは、PopupMenuは隣にアイコンを表示しませんあなたのタイトル。あなたがする必要がどのような

は、独自のPopupMenuの実装を作成し、デフォルトコンストラクタをオーバーライドすることで、アイコンの使用を強制するのいずれかである:

public CustomPopupMenu(Context context, View anchor) { 
     ... 
     mPopup.setForceShowIcon(true); 
    } 

OR

ちょうどあなたのPopupMenu、使用リフレクションを示す前に、目的のメソッドを呼び出すには

 //... initialization of your PopupMenu 
     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. 
     } 
     popUpMenu.show(); 
+0

ありがとうございました!あなたの2番目の選択肢は解決策でした – user3077648

関連する問題