简体   繁体   中英

How to change the text color of a menu item in Theme.AppCompat.Light.DarkActionBar theme?

I have black background with white letters.

Any ideas? Thanks.

        <style name="PacerTheme"            
                parent="@style/Theme.AppCompat.Light.DarkActionBar">                                
            <item name="android:itemBackground">@android:color/black</item>
            <item name="android:itemTextAppearance">@style/myCustomMenuTextApearance</item>                                           
        </style>

        <style name="myCustomMenuTextApearance" parent="@style/Widget.AppCompat.Light.ActionBar.Solid">
            <item name="android:textColor">@android:color/white</item>             
        </style>

Normal:

正常

Click in menu button, and show :

菜单

Modify the style like this:

<style name="PacerTheme" parent="@style/Theme.AppCompat.Light.DarkActionBar">
        <item name="android:itemTextAppearance">@style/myCustomMenuTextApearance</item>                                           
</style>

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

For "myCustomMenuTextApearance" style use for parent

@android:style/TextAppearance.Widget.IconMenu.Item

instead of

@style/Widget.AppCompat.Light.ActionBar.Solid

I found the solution, program change text color, as follows:

  @Override
  public boolean onPrepareOptionsMenu(Menu menu) 
  {
    for(int i=0; i<menu.size(); i++) 
    {
        MenuItem mi = menu.getItem(i);
        String title = mi.getTitle().toString();
        Spannable newTitle = new SpannableString(title);        
        newTitle.setSpan(new ForegroundColorSpan(Color.WHITE), 0, newTitle.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        mi.setTitle(newTitle);
    }
    return true;
  }

The XML is as follows:

 <style name="PacerTheme"            
        parent="@style/Theme.AppCompat.Light.DarkActionBar">                                
        <item name="android:itemBackground">@android:color/black</item>                                         
 </style>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM