动作栏菜单项文本颜色

时间:2023-01-16 12:38:44

How to change text color of menu item title. I tried to change it as below

如何更改菜单项标题的文本颜色。我试着把它改成下面的样子

<style name="Theme.Kanku.ActionBar.TitleTextStyle" parent="@android:style/TextAppearance.Holo.Widget.ActionBar.Title">
    <item name="android:textColor">@color/white</item>
</style>

But it change color only of Action Bar title text, but not menu item text.

但它只改变动作条标题文本的颜色,而不改变菜单项文本的颜色。

11 个解决方案

#1


102  

Try something like this :

试试这样的方法:

<style name="ThemeName" parent="@style/Theme.Sherlock.Light">
    <item name="actionMenuTextColor">@color/white</item>
    <item name="android:actionMenuTextColor">@color/white</item>
</style>

#2


97  

I tried several things but nothing worked for me. Finally this did the trick:

我试了好几样东西,但都没有用。最后,这一招奏效了:

<style name="your_theme" parent="your_parent">
    <item name="android:itemTextAppearance">@style/TextAppearance</item>
</style>

<style name="TextAppearance">
    <item name="android:textColor">@android:color/black</item>
</style>

I didn't use Sherlock themes. This worked for Holo.Light.DarkActionBar.

我没有使用夏洛克主题。这为Holo.Light.DarkActionBar工作。

#3


13  

After trying all of these and having them not work, I went about it programmatically like this:

在尝试了所有这些并让它们不起作用之后,我以编程的方式这样做了:

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.WHITE), 0, spanString.length(), 0); //fix the color to white
        item.setTitle(spanString);
    }
    return true;
}

This will work dynamically every time. In this case, the text color is changed to white. Simpy, change Color.WHITE to Color.whatever-color-you-want to change it to whatever color you want.

每次都会动态地工作。在本例中,文本颜色被更改为白色。Simpy,改变颜色。白色的颜色。无论你想要什么颜色,你都想改变它。

#4


7  

To update the menu item text color you need to make changes in themes.xml. The following answer is for sherlock.actionbar. In your themes.xml file add following lines:

要更新菜单项的文本颜色,您需要对theme .xml进行更改。以下是sherlock.actionbar的答案。在你的主题。xml文件添加如下行:

<style name="Theme.Mytheme" parent="@style/Theme.Sherlock">
    <item name="actionMenuTextColor">@color/mycolor</item>     
    <item name="android:actionMenuTextColor">@color/mycolor</item>
</style> 

#5


5  

this worked for me:

这工作对我来说:

<style name="AppBaseTheme" parent="android:Theme.Holo.Light.DarkActionBar">
    <item name="android:textAppearanceLargePopupMenu">@style/MyOverflowItemCollor</item>       
</style>

<style name="MyOverflowItemCollor" parent="android:TextAppearance.Holo.Widget.PopupMenu.Large">
    <item name="android:textColor">#ffffff</item>
</style> 

#6


4  

If AppTheme is android:Theme.Holo.Light.DarkActionBar, then you need to set custom actionBarWidgetTheme in order to get action menu style changed. Like this:

如果android:AppTheme Theme.Holo.Light。然后,您需要设置定制的actionBarWidgetTheme来更改动作菜单样式。是这样的:

<style name="AppTheme" parent="android:Theme.Holo.Light.DarkActionBar">
    <item name="android:actionBarWidgetTheme">@style/ActionBarWidget</item>
    <item name="android:actionMenuTextColor">@color/{custom_menu_item_text_color}</item>
</style>

<style name="MenuItemText">
    <item name="android:textColor">@color/{custom_menu_item_text_color}</item>
</style>

<style name="ActionBarWidget" parent="@android:style/Theme.Holo">
    <item name="android:itemTextAppearance">@style/MenuItemText</item>
</style>

#7


1  

please use the below code it works

请使用下面的代码

<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
    <!-- Customize your theme here. -->

    <item name="colorPrimary">@color/colorWhite</item>
    <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
    <item name="colorAccent">@color/colorPrimaryDark</item>
    <item name="android:actionMenuTextColor">@color/colorWhite</item>
</style>

#8


0  

you can change the action item color simply by following line

您可以通过以下行来更改动作项的颜色。

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

you can apply like this.

你可以这样申请。

<style name="your_theme" parent="your_parent">
<item name="android:actionMenuTextColor">@color/selected_text_color</item>
</style>

In my case

在我的例子中

<style name="MyTheme" parent="@android:style/Theme.Holo.Light">
  <item name="android:actionMenuTextColor">@color/selected_text_color</item>
  </style>

#9


0  

Add following line of code into your style.xml file that change option menu text color into black:

在样式中添加以下代码行。将选项菜单文本颜色改为黑色的xml文件:

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

Then add this one line code into your theme to change color of option menu text:

然后将这一行代码添加到您的主题中,以更改选项菜单文本的颜色:

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

It works for me,Thanks.

它适用于我,谢谢。

#10


0  

You can set color programmatically. Hope it help you.

您可以以编程的方式设置颜色。希望它对你有所帮助。

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

        }
    });
}

#11


0  

If you use DarkActionBar add below two lines in your style.xml

如果您使用DarkActionBar,请在样式中添加以下两行

  1. @color/green
    1. @color/green
    2. @color /绿
  2. @color /绿@color /绿色

Complete code is given below

完整的代码如下所示

<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
    <!-- Customize your theme here. -->
    <item name="colorPrimary">@color/colorPrimary</item>
    <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
    <item name="colorAccent">@color/colorAccent</item>

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

#1


102  

Try something like this :

试试这样的方法:

<style name="ThemeName" parent="@style/Theme.Sherlock.Light">
    <item name="actionMenuTextColor">@color/white</item>
    <item name="android:actionMenuTextColor">@color/white</item>
</style>

#2


97  

I tried several things but nothing worked for me. Finally this did the trick:

我试了好几样东西,但都没有用。最后,这一招奏效了:

<style name="your_theme" parent="your_parent">
    <item name="android:itemTextAppearance">@style/TextAppearance</item>
</style>

<style name="TextAppearance">
    <item name="android:textColor">@android:color/black</item>
</style>

I didn't use Sherlock themes. This worked for Holo.Light.DarkActionBar.

我没有使用夏洛克主题。这为Holo.Light.DarkActionBar工作。

#3


13  

After trying all of these and having them not work, I went about it programmatically like this:

在尝试了所有这些并让它们不起作用之后,我以编程的方式这样做了:

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.WHITE), 0, spanString.length(), 0); //fix the color to white
        item.setTitle(spanString);
    }
    return true;
}

This will work dynamically every time. In this case, the text color is changed to white. Simpy, change Color.WHITE to Color.whatever-color-you-want to change it to whatever color you want.

每次都会动态地工作。在本例中,文本颜色被更改为白色。Simpy,改变颜色。白色的颜色。无论你想要什么颜色,你都想改变它。

#4


7  

To update the menu item text color you need to make changes in themes.xml. The following answer is for sherlock.actionbar. In your themes.xml file add following lines:

要更新菜单项的文本颜色,您需要对theme .xml进行更改。以下是sherlock.actionbar的答案。在你的主题。xml文件添加如下行:

<style name="Theme.Mytheme" parent="@style/Theme.Sherlock">
    <item name="actionMenuTextColor">@color/mycolor</item>     
    <item name="android:actionMenuTextColor">@color/mycolor</item>
</style> 

#5


5  

this worked for me:

这工作对我来说:

<style name="AppBaseTheme" parent="android:Theme.Holo.Light.DarkActionBar">
    <item name="android:textAppearanceLargePopupMenu">@style/MyOverflowItemCollor</item>       
</style>

<style name="MyOverflowItemCollor" parent="android:TextAppearance.Holo.Widget.PopupMenu.Large">
    <item name="android:textColor">#ffffff</item>
</style> 

#6


4  

If AppTheme is android:Theme.Holo.Light.DarkActionBar, then you need to set custom actionBarWidgetTheme in order to get action menu style changed. Like this:

如果android:AppTheme Theme.Holo.Light。然后,您需要设置定制的actionBarWidgetTheme来更改动作菜单样式。是这样的:

<style name="AppTheme" parent="android:Theme.Holo.Light.DarkActionBar">
    <item name="android:actionBarWidgetTheme">@style/ActionBarWidget</item>
    <item name="android:actionMenuTextColor">@color/{custom_menu_item_text_color}</item>
</style>

<style name="MenuItemText">
    <item name="android:textColor">@color/{custom_menu_item_text_color}</item>
</style>

<style name="ActionBarWidget" parent="@android:style/Theme.Holo">
    <item name="android:itemTextAppearance">@style/MenuItemText</item>
</style>

#7


1  

please use the below code it works

请使用下面的代码

<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
    <!-- Customize your theme here. -->

    <item name="colorPrimary">@color/colorWhite</item>
    <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
    <item name="colorAccent">@color/colorPrimaryDark</item>
    <item name="android:actionMenuTextColor">@color/colorWhite</item>
</style>

#8


0  

you can change the action item color simply by following line

您可以通过以下行来更改动作项的颜色。

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

you can apply like this.

你可以这样申请。

<style name="your_theme" parent="your_parent">
<item name="android:actionMenuTextColor">@color/selected_text_color</item>
</style>

In my case

在我的例子中

<style name="MyTheme" parent="@android:style/Theme.Holo.Light">
  <item name="android:actionMenuTextColor">@color/selected_text_color</item>
  </style>

#9


0  

Add following line of code into your style.xml file that change option menu text color into black:

在样式中添加以下代码行。将选项菜单文本颜色改为黑色的xml文件:

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

Then add this one line code into your theme to change color of option menu text:

然后将这一行代码添加到您的主题中,以更改选项菜单文本的颜色:

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

It works for me,Thanks.

它适用于我,谢谢。

#10


0  

You can set color programmatically. Hope it help you.

您可以以编程的方式设置颜色。希望它对你有所帮助。

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

        }
    });
}

#11


0  

If you use DarkActionBar add below two lines in your style.xml

如果您使用DarkActionBar,请在样式中添加以下两行

  1. @color/green
    1. @color/green
    2. @color /绿
  2. @color /绿@color /绿色

Complete code is given below

完整的代码如下所示

<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
    <!-- Customize your theme here. -->
    <item name="colorPrimary">@color/colorPrimary</item>
    <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
    <item name="colorAccent">@color/colorAccent</item>

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