更改弹出菜单中的文本颜色

Change Text Color in Popup Menu

我创建了一个 popup-menu 并想将 menu 中每个项目的文本更改为蓝色。我还没有为 popup-menu 创建样式。

菜单项

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">

<item
    android:id="@+id/Meaning"
    android:title="TOOL 29 - MEANING"/>

<item
    android:id="@+id/ShiftState"
    android:title="TOOL 30 - SHIFT STATE"/>

<item
    android:id="@+id/Recreate"
    android:title="TOOL 31 - RECREATE"/>

<item
    android:id="@+id/Rules"
    android:title="TOOL 32 - RULES"/>

</menu>

菜单代码

btnEmotionTools = (ImageView) findViewById(R.id.btnEmotionTools);
    btnEmotionTools.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            PopupMenu popup = new PopupMenu(Screen2_Witnessing.this,btnEmotionTools);
            popup.getMenuInflater().inflate(R.menu.popup_menu_emotion_tools, popup.getMenu());

            popup.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
                @Override
                public boolean onMenuItemClick(MenuItem item) {
                    itemId = item.getItemId();
                    if ( itemId == R.id.Meaning  ){
                        screen = "2";
                        SharedPreferences.Editor editor = sharedpreferences.edit();
                        editor.putString(Screen,screen);
                        editor.commit();
                        calculate();
                        Intent i = new Intent(getApplicationContext(), Screen109_Meaning.class);
                        startActivity(i);
                    }
                    else if ( itemId == R.id.ShiftState ) {
                        screen = "2";
                        SharedPreferences.Editor editor = sharedpreferences.edit();
                        editor.putString(Screen,screen);
                        editor.commit();
                        calculate();
                        Intent i = new Intent(getApplicationContext(), Screen125b_Shift_State.class);
                        startActivity(i);
                    }
                    else if ( itemId == R.id.Recreate ) {
                        screen = "2";
                        SharedPreferences.Editor editor = sharedpreferences.edit();
                        editor.putString(Screen,screen);
                        editor.commit();
                        calculate();
                        Intent i = new Intent(getApplicationContext(), Screen138_Recreate.class);
                        startActivity(i);
                    }
                    else if ( itemId == R.id.Rules ) {
                        screen = "2";
                        SharedPreferences.Editor editor = sharedpreferences.edit();
                        editor.putString(Screen,screen);
                        editor.commit();
                        calculate();
                        Intent i = new Intent(getApplicationContext(), Screen122_Rules.class);
                        startActivity(i);
                    }
                      return true;
                }
            });
            popup.show();
        }
    });

在 style.xml

中提供样式
 <style name="PopupMenu" parent="Widget.AppCompat.PopupMenu.Overflow">
    <item name="android:popupBackground">@color/yourcolor</item>
    </style>

Java代码

  Context colorContext= new ContextThemeWrapper(getContext(), R.style.PopupMenu);
  PopupMenu popup = new PopupMenu(colorContext,btnEmotionTools);

这样试试:

PopupMenu popup = new PopupMenu(getActivity(), btnEmotionTools);
            popup.getMenuInflater().inflate(R.menu.popup_menu_emotion_tools, popup.getMenu());
            for (int i = 0; i < popup.getMenu().size(); i++) {
                MenuItem item = popup.getMenu().getItem(i);
                SpannableString spanString = new SpannableString(popup.getMenu().getItem(i).getTitle().toString());
                spanString.setSpan(new ForegroundColorSpan(Color.RED), 0, spanString.length(), 0); 
                item.setTitle(spanString);
            }
            popup.show();