Android - Change Actionbar Title Text Color

2019-06-14 22:59发布

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

12条回答
做个烂人
2楼-- · 2019-06-14 23:38

Change the Activity theme as:

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

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

</style>
查看更多
三岁会撩人
3楼-- · 2019-06-14 23:39

When i got the same problem, i used:

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

<item name="android:titleTextStyle">@style/ActionBarTextStyle</item>
查看更多
We Are One
4楼-- · 2019-06-14 23:44

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.

查看更多
Viruses.
5楼-- · 2019-06-14 23:46

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.

查看更多
SAY GOODBYE
6楼-- · 2019-06-14 23:50
<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">
查看更多
smile是对你的礼貌
7楼-- · 2019-06-14 23:52

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);
查看更多
登录 后发表回答