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

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

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.

查看更多
祖国的老花朵
3楼-- · 2019-06-14 23:59

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">
查看更多
Anthone
4楼-- · 2019-06-15 00:02

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>
查看更多
我命由我不由天
5楼-- · 2019-06-15 00:03

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

查看更多
forever°为你锁心
6楼-- · 2019-06-15 00:04

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"/>
查看更多
相关推荐>>
7楼-- · 2019-06-15 00:04

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.

查看更多
登录 后发表回答