Android - 更改Actionbar标题文本颜色

18

我正在开发一个应用程序,我想要 更改操作栏标题的文本颜色。现在,在我的清单中定义了一个主题:

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

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

这个方法不起作用,但我不知道为什么。根据这个答案,这应该可以工作。文本视图样式也无效。

我做错了什么?

更新: 已按照Grace Feng的答案进行更改。


暂时还没有。 - jack_the_beast
13个回答

18

使用SpannableString,我们可以设置Actionbar标题的文本颜色。

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

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

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

您在这里漏掉了一个符号"@"?
<style name="MyTheme" parent="@android:style/Theme.Holo.Light">

also

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

2
哦,这很尴尬。无论如何,这个不起作用,ActionBar 标题仍然是白色的,TextView 仍然是黑色的。 - jack_the_beast

4
我假设你正在使用 ToolBar 作为 ActionBar。在所有答案中,我认为这是最容易实现的。并且适用于所有API版本。
因此,在这里是:
操作栏标题使用 textColorPrimary 作为其文本颜色。因此,在 styles.xml 中创建一个新样式,其中包含一个 textColorPrimary 的项目,并设置您需要的任何颜色来更改它。
<style name="appBarTheme" parent="Theme.AppCompat.NoActionBar">
    <item name="android:textColorPrimary">@color/colorRed</item>
</style>

现在,在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>

现在你已经得到了你所需要的。


android:textColorPrimary非常精确。我不得不查看很多答案,直到找到这个并且它起作用了。谢谢! - JorgeAmVF

4

您可以在这里找到如何自定义ActionBar。我建议使用ToolBarTheme.AppCompat代替-示例


2
只需添加app:titleTextColor="@color/CUSTOM_COLOR"即可:
<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"/>

1

在 StackOverflow 上闲逛时,我找到了这个答案,最终解决了问题。

我在这里报告一下:

<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
我按照这个示例将@color/my_color设置为#000000,但我的操作栏内的文本仍然是白色。 - James
ActionBar的背景和文本颜色都变成了白色。 - Mohammed

1
当我遇到同样的问题时,我使用了:

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

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

0

看起来你使用了style而不是theme样式和主题有所不同。请进行替换。

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

使用

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

如果我按照你建议的方式放置,Android Studio 就无法找到它并返回错误,所有的建议都包括 :style - jack_the_beast
我明白了。我应该不将其用于应用程序选项卡,而是用于单个视图。但是我的问题仍然存在,如何更改文本颜色? - jack_the_beast

0

我认为这只是你的活动问题。我使用了与你相同的代码,它可以正常工作。enter image description here

首先,我使用AppCompatActivity作为基本活动并出现错误。然后我使用activity,它可以正常工作。 在我看来,你的代码用于Theme.Holo主题,而现在我们使用Theme.AppCompat。因此,如果您想进行更改,请参考在Android上更改AppCompat Light DarkActionBar主题的背景和文本颜色。 这对我有效。祝你好运。


我在Activity中有一个NavigationDrawer,因此它扩展了AbstractNavDrawerActivity。不幸的是,我没有编写这段代码,所以我真的不知道为什么编写这个代码的人会这样使用它:S - jack_the_beast
1
我看到了AbstractNavDrawerActivity,这个活动是从ActionBarActivity扩展而来的。而你的主题是Theme.Holo。所以它不会起作用。如果你不想改变这个活动,只需像https://dev59.com/sl0a5IYBdhLWcg3wxbLm那样更改主题即可。这样就可以工作了。 - sunday

0

我不知道解决方案,但是我可以用这种方法来改变文本颜色,使其在白色和黑色之间切换。

所以,在styles.xml文件中您有:

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

如果你将parent="..."中的内容更改为暗色或亮色主题,它会自适应这些主题的文本颜色。例如,现在你有一个暗色主题,文本是白色的,但当我更改为"Theme.AppCompat.Light"时,文本颜色变成了黑色。

希望它能在某种程度上帮助到你。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接