Android - Change Actionbar Title Text Color

2019-06-14 23:57发布

问题:

I'm developing an app and I want to change the textcolor of the actionbar title. now, I defined a theme in my manifest:

<application
        android:theme="@style/AppTheme">
</application>

which is defined in styles.xml:

<style name="AppTheme" parent="android:style/Theme.Holo.Light">
        <item name="android:textViewStyle">@style/AppTheme.TextView</item>
        <item name="android:actionBarStyle">@style/AppTheme.ActionBarStyle</item>
    </style>

    <style name="AppTheme.ActionBarStyle" parent="@android:style/Widget.Holo.Light.ActionBar">
        <item name="android:titleTextStyle">@style/AppTheme.ActionBar.TitleTextStyle</item>
    </style>

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

    <style name="AppTheme.TextView" parent="@android:style/Widget.TextView">
        <item name="android:textColor">#FFFFFF</item>
    </style>

this doesn't work but I don't get why. this should work according to this answer.neither the textview style is working.

what I am doing wrong?

UPDATE: changed to meet Grace Feng answer below

回答1:

Using SpannableString we can set Text Color of Actionbar title

SpannableString s = new SpannableString(title);
s.setSpan(new ForegroundColorSpan(Color.GREEN), 0, title.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
getSupportActionBar().setTitle(s);


回答2:

<style name="AppTheme" parent="android:style/Theme.Holo.Light">

<style name="AppTheme.TextView" parent="android:Widget.TextView">

You missed a symbol "@" here?

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

also

<style name="AppTheme.TextView" parent="@android:Widget.TextView">


回答3:

You could find how to customize ActionBar here. I recommend to use ToolBar and Theme.AppCompat instead - example.



回答4:

Just add app:titleTextColor="@color/CUSTOM_COLOR" like so:

<android.support.v7.widget.Toolbar
    android:id="@+id/toolbar"
    android:layout_width="match_parent"
    android:layout_height="?attr/actionBarSize"
    android:background="@color/yellow_light"
    app:titleTextColor="@color/CUSTOM_COLOR"
    app:popupTheme="@style/AppTheme.PopupOverlay"/>


回答5:

I am assuming you are using ToolBar as ActionBar. Of all the answers, I think this is the easiest one to implement. And works on all API versions.

So here it is:

Action bar title uses textColorPrimary as its text color. So to change that in styles.xml create a new style with an item for textColorPrimary and set whatever color you need.

<style name="appBarTheme" parent="Theme.AppCompat.NoActionBar">
    <item name="android:textColorPrimary">@color/colorRed</item>
</style>

Now, add that style as your theme for toolbar in the XML.

<android.support.v7.widget.Toolbar
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    app:layout_scrollFlags="enterAlways"
    android:id="@+id/toolbar"
    android:theme="@style/appBarTheme">
</android.support.v7.widget.Toolbar>

And now you got what you need.



回答6:

wandering around StackOverflow I found this answer which finally worked.

I report it here:

<style name="AppTheme" parent="android:Theme.Holo.Light.DarkActionBar">
    <item name="android:actionBarStyle">@style/ActionBar</item>
    ...
</style>

<style name="ActionBar" parent="@android:style/Widget.ActionBar">
  <item name="android:titleTextStyle">@style/ActionBar.Title</item>
  <item name="android:subtitleTextStyle">@style/ActionBar.Subtitle</item>
  ...
</style>

<style name="ActionBar.Title">
  <item name="android:textColor">@color/my_color</item>
</style>

<style name="ActionBar.Subtitle">
  <item name="android:textColor">@color/my_color</item>
</style>


回答7:

When i got the same problem, i used:

<item name="titleTextStyle">@style/ActionBarTextStyle</item>

<item name="android:titleTextStyle">@style/ActionBarTextStyle</item>


回答8:

I think it is just the problem of you activity.I used the same code as you, and it works.

Firstly ,i used AppCompatActivity as the base activity and get error. and then i use activity and it works. In my opinion, your code is use for theme for Theme.Holo, and now we use Theme.AppCompat . So if you want change, just reference Changing Background and text color of AppCompat Light DarkActionBar Theme on android. It works for me.Good luck for you.



回答9:

Seems you've used style instead of theme. Styles and themes differ. So replace

<style name="AppTheme" parent="android:style/Theme.Holo.Light">

With

<style name="AppTheme" parent="android:Theme.Light">


回答10:

I don't know the solution, but I was able to change the text color between white and black with this method.

so, in the styles.xml you have:

<resources>
    <style name="AppTheme" parent="Theme.AppCompat.DayNight.DarkActionBar">
        <item name="colorPrimary">#673AB7</item>
        <item name="colorPrimaryDark">#512DA8</item>
        <item name="colorAccent">#FF4081</item>
    </style>
</resources>

if you change what's inside the parent="..." to dark or light themes, it adapts the text color to these themes. For example, right now you have a dark theme, and the text is white, but when I change to "Theme.AppCompat.Light" the text color turns black.

Hope it helps in some way.



回答11:

Change the Activity theme as:

<style name="ThemeName" parent="android:Theme.Holo.Light">

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

</style>


回答12:

Just had this problem, finally figured it out. In your style.xml do not specify a background color for your toolbar. Then define the color in each layout.xml file:

<android.support.v7.widget.Toolbar
    android:id="@+id/my_toolbar"
    android:layout_width="match_parent"
    android:layout_height="?attr/actionBarSize"
    android:background="#B678EE"
    android:elevation="4dp"
    android:theme="@style/ToolbarTheme"
    app:popupTheme="@style/ThemeOverlay.AppCompat.Light"/>

This gives you the flexibility to be able to easily have different action bar colors depending on the activity if need be as well.