Action Bar menu item text color

旧街凉风 提交于 2020-01-09 01:56:13

问题


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.


回答1:


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:


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.




回答3:


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.




回答4:


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:

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



回答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:


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:

<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:


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:


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

 <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.




回答9:


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>



回答10:


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

  1. @color/green
    1. @color/green

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>




回答11:


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

        }
    });
}


来源:https://stackoverflow.com/questions/18015010/action-bar-menu-item-text-color

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!