如何更改Android中菜单项的文本颜色?

我可以在Android中更改菜单项的背景颜色吗?

请让我知道如果有人有任何解决办法。 最后一个选项显然是自定义它,但有没有办法改变文本的颜色,而无需自定义。

一个简单的线路在你的主题:)

<item name="android:actionMenuTextColor">@color/your_color</item> 

这似乎是一个

  <item name="android:itemTextAppearance">@style/myCustomMenuTextApearance</item> 

在我的主题和

  <style name="myCustomMenuTextApearance" parent="@android:style/TextAppearance.Widget.IconMenu.Item"> <item name="android:textColor">@android:color/primary_text_dark</item> </style> 

在styles.xml中更改列表项的样式,但不更改菜单项。

您可以使用SpannableString而不是String来轻松更改MenuItem文本的颜色。

 @Override public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) { inflater.inflate(R.menu.your_menu, menu); int positionOfMenuItem = 0; // or whatever... MenuItem item = menu.getItem(positionOfMenuItem); SpannableString s = new SpannableString("My red MenuItem"); s.setSpan(new ForegroundColorSpan(Color.RED), 0, s.length(), 0); item.setTitle(s); } 

如果您正在使用新的工具栏,主题为Theme.AppCompat.Light.NoActionBar ,则可以按照以下方式进行设置。

  <style name="ToolbarTheme" parent="Theme.AppCompat.Light.NoActionBar"> <item name="android:textColorPrimary">@color/my_color1</item> <item name="android:textColorSecondary">@color/my_color2</item> <item name="android:textColor">@color/my_color3</item> </style>` 

根据我得到的结果,
android:textColorPrimary是显示活动名称的文本颜色,它是工具栏的主要文本。

android:textColorSecondary是字幕和更多选项(3点)button的文本颜色。 (是的,它根据这个属性改变了颜色!)

android:textColor是所有其他文本的颜色,包括菜单。

最后将主题设置为工具栏

 <android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" app:theme="@style/ToolbarTheme" android:layout_height="wrap_content" android:layout_width="match_parent" android:minHeight="?attr/actionBarSize"/> 

如果您将菜单用作<android.support.design.widget.NavigationView />则只需在NavigationView添加以下行:

 app:itemTextColor="your color" 

也可用colorTint图标,它也会覆盖你的图标的颜色。 为此,你必须添加下面的行:

 app:itemIconTint="your color" 

例:

 <android.support.design.widget.NavigationView android:id="@+id/nav_view" android:layout_width="wrap_content" android:layout_height="match_parent" android:layout_gravity="start" app:itemTextColor="@color/color_white" app:itemIconTint="@color/color_white" android:background="@color/colorPrimary" android:fitsSystemWindows="true" app:headerLayout="@layout/nav_header_main" app:menu="@menu/activity_main_drawer"/> 

希望它会帮助你。

正如你在这个问题中看到的那样,你应该:

 <item name="android:textColorPrimary">yourColor</item> 

以上代码更改API> = v21的菜单操作项的文本颜色。

 <item name="actionMenuTextColor">@android:color/holo_green_light</item> 

以上是API <v21的代码

android中的Options菜单可以自定义设置背景或者改变文字外观。 菜单中的背景和文本颜色不能使用主题和样式进行更改。 android源代码(data \ res \ layout \ icon_menu_item_layout.xml)使用类“com.android.internal.view.menu.IconMenuItem”视图的自定义项目作为菜单布局。 我们可以修改上面的类来定制菜单。 要达到相同的效果,请使用LayoutInflater工厂类,并为视图设置背景和文本颜色。

@Override public boolean onCreateOptionsMenu(Menu menu) { MenuInflater inflater = getMenuInflater(); inflater.inflate(R.menu.my_menu, menu); getLayoutInflater().setFactory(new Factory() { @Override public View onCreateView(String name, Context context, AttributeSet attrs) { if (name .equalsIgnoreCase(“com.android.internal.view.menu.IconMenuItemView”)) { try{ LayoutInflater f = getLayoutInflater(); final View view = f.createView(name, null, attrs); new Handler().post(new Runnable() { public void run() { // set the background drawable view .setBackgroundResource(R.drawable.my_ac_menu_background); // set the text color ((TextView) view).setTextColor(Color.WHITE); } }); return view; } catch (InflateException e) { } catch (ClassNotFoundException e) {} } return null; } }); return super.onCreateOptionsMenu(menu); }
@Override public boolean onCreateOptionsMenu(Menu menu) { MenuInflater inflater = getMenuInflater(); inflater.inflate(R.menu.my_menu, menu); getLayoutInflater().setFactory(new Factory() { @Override public View onCreateView(String name, Context context, AttributeSet attrs) { if (name .equalsIgnoreCase(“com.android.internal.view.menu.IconMenuItemView”)) { try{ LayoutInflater f = getLayoutInflater(); final View view = f.createView(name, null, attrs); new Handler().post(new Runnable() { public void run() { // set the background drawable view .setBackgroundResource(R.drawable.my_ac_menu_background); // set the text color ((TextView) view).setTextColor(Color.WHITE); } }); return view; } catch (InflateException e) { } catch (ClassNotFoundException e) {} } return null; } }); return super.onCreateOptionsMenu(menu); } 

感谢代码示例。 我不得不修改它去使用上下文菜单工作。 这是我的解决scheme。

  static final Class<?>[] constructorSignature = new Class[] {Context.class, AttributeSet.class}; class MenuColorFix implements LayoutInflater.Factory { public View onCreateView(String name, Context context, AttributeSet attrs) { if (name.equalsIgnoreCase("com.android.internal.view.menu.ListMenuItemView")) { try { Class<? extends ViewGroup> clazz = context.getClassLoader().loadClass(name).asSubclass(ViewGroup.class); Constructor<? extends ViewGroup> constructor = clazz.getConstructor(constructorSignature); final ViewGroup view = constructor.newInstance(new Object[]{context,attrs}); new Handler().post(new Runnable() { public void run() { try { view.setBackgroundColor(Color.BLACK); List<View> children = getAllChildren(view); for(int i = 0; i< children.size(); i++) { View child = children.get(i); if ( child instanceof TextView ) { ((TextView)child).setTextColor(Color.WHITE); } } } catch (Exception e) { Log.i(TAG, "Caught Exception!",e); } } }); return view; } catch (Exception e) { Log.i(TAG, "Caught Exception!",e); } } return null; } } public List<View> getAllChildren(ViewGroup vg) { ArrayList<View> result = new ArrayList<View>(); for ( int i = 0; i < vg.getChildCount(); i++ ) { View child = vg.getChildAt(i); if ( child instanceof ViewGroup) { result.addAll(getAllChildren((ViewGroup)child)); } else { result.add(child); } } return result; } @Override public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) { LayoutInflater lInflater = getLayoutInflater(); if ( lInflater.getFactory() == null ) { lInflater.setFactory(new MenuColorFix()); } super.onCreateContextMenu(menu, v, menuInfo); MenuInflater inflater = getMenuInflater(); inflater.inflate(R.menu.myMenu, menu); } 

对我来说,这适用于Android 1.6,2.03和4.03。

我以这样的方式编程:

 public boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.changeip_card_menu, menu); for(int i = 0; i < menu.size(); i++) { MenuItem item = menu.getItem(i); SpannableString spanString = new SpannableString(menu.getItem(i).getTitle().toString()); spanString.setSpan(new ForegroundColorSpan(Color.BLACK), 0, spanString.length(), 0); //fix the color to white item.setTitle(spanString); } return true; 

}

当菜单项被夸大时,我使用html标签来更改单个项目的文本颜色。 希望这会有所帮助。

 public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.menu_main, menu); menu.findItem(R.id.main_settings).setTitle(Html.fromHtml("<font color='#ff3824'>Settings</font>")); return true; } 

简短的答案是肯定的。 幸运的你!
为此,您需要重写一些Android默认样式的样式:

首先,看一下Android中主题的定义 :

 <style name="Theme.IconMenu"> <!-- Menu/item attributes --> <item name="android:itemTextAppearance">@android:style/TextAppearance.Widget.IconMenu.Item</item> <item name="android:itemBackground">@android:drawable/menu_selector</item> <item name="android:itemIconDisabledAlpha">?android:attr/disabledAlpha</item> <item name="android:horizontalDivider">@android:drawable/divider_horizontal_bright</item> <item name="android:verticalDivider">@android:drawable/divider_vertical_bright</item> <item name="android:windowAnimationStyle">@android:style/Animation.OptionsPanel</item> <item name="android:moreIcon">@android:drawable/ic_menu_more</item> <item name="android:background">@null</item> </style> 

所以,菜单中文字的外观是在@android:style/TextAppearance.Widget.IconMenu.Item
现在,在样式的定义中:

 <style name="TextAppearance.Widget.IconMenu.Item" parent="TextAppearance.Small"> <item name="android:textColor">?textColorPrimaryInverse</item> </style> 

所以现在我们有问题的颜色的名称,如果你看在系统的资源的颜色文件夹:

 <selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:state_enabled="false" android:color="@android:color/bright_foreground_light_disabled" /> <item android:state_window_focused="false" android:color="@android:color/bright_foreground_light" /> <item android:state_pressed="true" android:color="@android:color/bright_foreground_light" /> <item android:state_selected="true" android:color="@android:color/bright_foreground_light" /> <item android:color="@android:color/bright_foreground_light" /> <!-- not selected --> </selector> 

最后,这是你需要做的事情:

重写“TextAppearance.Widget.IconMenu.Item”并创build自己的样式。 然后将其链接到您自己的select器,使其成为您想要的方式。 希望这可以帮助你。 祝你好运!

我发现它尤里卡!

在你的应用主题中:

 <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar"> <item name="android:actionBarStyle">@style/ActionBarTheme</item> <!-- backward compatibility --> <item name="actionBarStyle">@style/ActionBarTheme</item> </style> 

这里是你的行动吧主题:

 <style name="ActionBarTheme" parent="@style/Widget.AppCompat.Light.ActionBar.Solid.Inverse"> <item name="android:background">@color/actionbar_bg_color</item> <item name="popupTheme">@style/ActionBarPopupTheme</item <!-- backward compatibility --> <item name="background">@color/actionbar_bg_color</item> </style> 

这里是你的popup式主题:

  <style name="ActionBarPopupTheme"> <item name="android:textColor">@color/menu_text_color</item> <item name="android:background">@color/menu_bg_color</item> </style> 

干杯;)

最简单的方法来为单个工具栏自定义菜单颜色,而不是AppTheme

  <android.support.design.widget.AppBarLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:theme="@style/AppTheme.AppBarOverlay.MenuBlue"> <android.support.v7.widget.Toolbar android:id="@+id/toolbar" android:layout_width="match_parent" android:layout_height="?attr/actionBarSize"/> </android.support.design.widget.AppBarLayout> 

styles.xml上的常用工具栏

 <style name="AppTheme.AppBarOverlay" parent="ThemeOverlay.AppCompat.Dark.ActionBar"/> 

我们的自定义工具栏风

 <style name="AppTheme.AppBarOverlay.MenuBlue"> <item name="actionMenuTextColor">@color/blue</item> </style> 
 @Override public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) { inflater.inflate(R.menu.search, menu); MenuItem myActionMenuItem = menu.findItem( R.id.action_search); SearchView searchView = (SearchView) myActionMenuItem.getActionView(); EditText searchEditText = (EditText) searchView.findViewById(android.support.v7.appcompat.R.id.search_src_text); searchEditText.setTextColor(Color.WHITE); //You color here 

Sephy的解决scheme不起作用。 可以使用上述方法覆盖选项菜单项文本外观,但不能使用项目或菜单。 要做到这一点基本上有3种方法:

  1. 如何更改选项菜单的背景颜色?
  2. 编写自己的视图来显示并覆盖onCreateOptionsMenu和onPrepareOptionsMenu以获得您想要的结果。 我说这一般是因为你通常可以在这些方法中做任何你想要的,但是你可能不想调用super()。
  3. 从开放源代码SDK复制代码并自定义您的行为。 Activity使用的默认菜单实现将不再适用。

请参阅问题4441:自定义选项菜单主题以获取更多线索。

试试这个代码….

  @Override public boolean onCreateOptionsMenu(Menu menu) { MenuInflater inflater = getMenuInflater(); inflater.inflate(R.menu.my_menu, menu); getLayoutInflater().setFactory(new Factory() { @Override public View onCreateView(String name, Context context, AttributeSet attrs) { if (name.equalsIgnoreCase("com.android.internal.view.menu.IconMenuItemView")) { try { LayoutInflater f = getLayoutInflater(); final View view = f.createView(name, null, attrs); new Handler().post(new Runnable() { public void run() { // set the background drawable view.setBackgroundResource(R.drawable.my_ac_menu_background); // set the text color ((TextView) view).setTextColor(Color.WHITE); } }); return view; } catch (InflateException e) { } catch (ClassNotFoundException e) { } } return null; } }); return super.onCreateOptionsMenu(menu); } 

只需将此添加到您的主题

 <style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar"> <item name="android:itemTextAppearance">@style/AppTheme.ItemTextStyle</item> </style> <style name="AppTheme.ItemTextStyle" parent="@android:style/TextAppearance.Widget.IconMenu.Item"> <item name="android:textColor">@color/orange_500</item> </style> 

testingAPI 21

感谢max.musterman,这是我在22级工作的解决scheme:

 public boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.menu_main, menu); SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE); MenuItem searchMenuItem = menu.findItem(R.id.search); SearchView searchView = (SearchView) searchMenuItem.getActionView(); searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName())); searchView.setSubmitButtonEnabled(true); searchView.setOnQueryTextListener(this); setMenuTextColor(menu, R.id.displaySummary, R.string.show_summary); setMenuTextColor(menu, R.id.about, R.string.text_about); setMenuTextColor(menu, R.id.importExport, R.string.import_export); setMenuTextColor(menu, R.id.preferences, R.string.settings); return true; } private void setMenuTextColor(Menu menu, int menuResource, int menuTextResource) { MenuItem item = menu.findItem(menuResource); SpannableString s = new SpannableString(getString(menuTextResource)); s.setSpan(new ForegroundColorSpan(Color.BLACK), 0, s.length(), 0); item.setTitle(s); } 

硬编码的Color.BLACK可能成为setMenuTextColor方法的附加参数。 此外,我只用于这是android:showAsAction="never"菜单项。

您可以以编程方式设置颜色。

 private static void setMenuTextColor(final Context context, final Toolbar toolbar, final int menuResId, final int colorRes) { toolbar.post(new Runnable() { @Override public void run() { View settingsMenuItem = toolbar.findViewById(menuResId); if (settingsMenuItem instanceof TextView) { if (DEBUG) { Log.i(TAG, "setMenuTextColor textview"); } TextView tv = (TextView) settingsMenuItem; tv.setTextColor(ContextCompat.getColor(context, colorRes)); } else { // you can ignore this branch, because usually there is not the situation Menu menu = toolbar.getMenu(); MenuItem item = menu.findItem(menuResId); SpannableString s = new SpannableString(item.getTitle()); s.setSpan(new ForegroundColorSpan(ContextCompat.getColor(context, colorRes)), 0, s.length(), 0); item.setTitle(s); } } }); } 

这是如何使用颜色为特定菜单项着色,适用于所有API级别:

 public static void setToolbarMenuItemTextColor(final Toolbar toolbar, final @ColorRes int color, @IdRes final int resId) { if (toolbar != null) { for (int i = 0; i < toolbar.getChildCount(); i++) { final View view = toolbar.getChildAt(i); if (view instanceof ActionMenuView) { final ActionMenuView actionMenuView = (ActionMenuView) view; // view children are accessible only after layout-ing actionMenuView.post(new Runnable() { @Override public void run() { for (int j = 0; j < actionMenuView.getChildCount(); j++) { final View innerView = actionMenuView.getChildAt(j); if (innerView instanceof ActionMenuItemView) { final ActionMenuItemView itemView = (ActionMenuItemView) innerView; if (resId == itemView.getId()) { itemView.setTextColor(ContextCompat.getColor(toolbar.getContext(), color)); } } } } }); } } } } 

通过这样做,你将失去背景select器效果,所以这里是将自定义背景select器应用于所有菜单项子项的代码。

 public static void setToolbarMenuItemsBackgroundSelector(final Context context, final Toolbar toolbar) { if (toolbar != null) { for (int i = 0; i < toolbar.getChildCount(); i++) { final View view = toolbar.getChildAt(i); if (view instanceof ImageButton) { // left toolbar icon (navigation, hamburger, ...) UiHelper.setViewBackgroundSelector(context, view); } else if (view instanceof ActionMenuView) { final ActionMenuView actionMenuView = (ActionMenuView) view; // view children are accessible only after layout-ing actionMenuView.post(new Runnable() { @Override public void run() { for (int j = 0; j < actionMenuView.getChildCount(); j++) { final View innerView = actionMenuView.getChildAt(j); if (innerView instanceof ActionMenuItemView) { // text item views final ActionMenuItemView itemView = (ActionMenuItemView) innerView; UiHelper.setViewBackgroundSelector(context, itemView); // icon item views for (int k = 0; k < itemView.getCompoundDrawables().length; k++) { if (itemView.getCompoundDrawables()[k] != null) { UiHelper.setViewBackgroundSelector(context, itemView); } } } } } }); } } } } 

这里还有助手function:

 public static void setViewBackgroundSelector(@NonNull Context context, @NonNull View itemView) { int[] attrs = new int[]{R.attr.selectableItemBackgroundBorderless}; TypedArray ta = context.obtainStyledAttributes(attrs); Drawable drawable = ta.getDrawable(0); ta.recycle(); ViewCompat.setBackground(itemView, drawable); } 

对于改变文本颜色,你可以设置一个MenuItem的自定义视图,然后你可以定义文本的颜色。

示例代码:MenuItem.setActionView()