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

时间:2018-07-23 12:27:56

标签: android android-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();
        }
    });

菜单代码

Id <- c(1,2,3)
Start1 <-c("2018-01-01", "2018-01-01", "2018-01-01" )
End1 <-c(NA, "2018-06-01", "2018-06-01" )
Start2 <-c("2018-01-01", "2018-01-01", "2018-02-01" )
End2 <-c(NA, "2018-02-01", "2018-06-01" )
Start3 <-c("2018-06-01", "2018-01-01", "2018-01-01" )
End3 <-c(NA, "2018-05-01", "2018-06-01" )
Start4 <-c("2018-06-01", "2018-03-01", "2018-01-01" )
End4 <-c(NA, "2018-04-01", "2018-06-01" )

Table1 <- data.frame(Id, Start1, End1, Start2, End2, Start3, End3, Start4, End4)


Id <- c(1,2,3)
MultiToSingle <-c(203, 120, 151)
MultiToSingle.s <-c(0,1,0)
TotalChurn <-c(203,151,151)
TotalChurn.s <-c(0,1,1)

Table2 <- data.frame(Id, MultiToSingle, MultiToSingle.s ,TotalChurn,TotalChurn.s)

2 个答案:

答案 0 :(得分:1)

尝试这样:

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

答案 1 :(得分:0)

在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);