Android. ActionBar. How to change ActionBar title

2019-09-06 20:05发布

问题:

I am trying to change ActionBar title color. I tried different ways to do it. But I could not solve this problem. I have created my own actionbar theme in values/styles.xml and values-14/styles.xml

values/styles.xml

<style name="CustomActionBarTheme" parent="@style/Theme.AppCompat.Light">   
    <item name="actionBarStyle">@style/MyActionBar</item>
</style>

<style name="MyActionBar" parent="@style/Widget.AppCompat.Light.ActionBar.Solid.Inverse">
    <item name="background">@color/grey_actionbar_background</item>
    <item name="icon">@drawable/ic_launcher</item>
    <item name="actionBarTabTextStyle">@style/MyActionBarTabText</item>
</style>

<style name="MyActionBarTabText" parent="@style/Widget.AppCompat.Light.ActionBar.TabText">
    <item name="android:textColor">@color/grey_text</item>
</style>

values-v14/styles.xml

<style name="CustomActionBarTheme" parent="@style/Theme.AppCompat.Light">
    <item name="android:actionBarStyle">@style/MyActionBar</item>
    <item name="actionBarStyle">@style/MyActionBar</item>
</style>

<style name="MyActionBar" parent="@style/Widget.AppCompat.Light.ActionBar.Solid.Inverse">
    <item name="android:background">@color/grey_actionbar_background</item>
    <item name="background">@color/grey_actionbar_background</item>
    <item name="android:icon">@drawable/ic_launcher</item>
    <item name="icon">@drawable/ic_launcher</item>
    <item name="actionBarTabTextStyle">@style/MyActionBarTabText</item>
    <item name="android:actionBarTabTextStyle">@style/MyActionBarTabText</item>
</style>

<style name="MyActionBarTabText" parent="@style/Widget.AppCompat.Light.ActionBar.TabText">
    <item name="android:textColor">@color/grey_text</item>
</style>

grey_text color is #58585A. But when I am running on 4.3 device, ActionBar title color is F3F3F3.

I am wondering why this is happening. How to solve this problem?

回答1:

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


回答2:

just try method of Activity class "setTitleColor(int color)"