Frage

I have been having a lot of trouble trying to set the action bar text color using android-support-v7-appcompat. I was able to change the selector color from the default color to a light grey however now when I select something the text color changes as well. I want the text color to stay black when the user makes a selection. Any ideas? Thanks!

Text Color needs to stay black

War es hilfreich?

Lösung

in my themes.xml, this did the trick

<style name="AppTheme.AppCompat.Light" parent="@style/Theme.AppCompat.Light"> 
        <item name="android:itemTextAppearance">@style/MenuTextAppearance</item>    
</style>

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

Additionally, I found you could use a spannable string to customize the text color of the menuItems programmatically. This caused the app to crash when using appcompay-v7 however others have had success with Sherlock, Holo, etc. Example:

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

https://stackoverflow.com/a/19008593/2820963

This would be great if you want different menu items to have different colors.

Andere Tipps

I believe there is a style for select items so you will need to explicitly define the text color for when an item is selected or your app will revert to the default textcolor. The attribute should be something like android:textColorHighlight

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top