简体   繁体   English

Android - 更改操作栏标题文本颜色

[英]Android - Change Actionbar Title Text Color

I'm developing an app and I want to change the textcolor of the actionbar title .我正在开发一个应用程序,我想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:在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.这应该根据这个答案工作。textview 样式都没有工作。

what I am doing wrong?我做错了什么?

UPDATE: changed to meet Grace Feng answer below更新:更改为满足以下 Grace Feng 的回答

Using SpannableString we can set Text Color of Actionbar title 使用SpannableString我们可以设置Actionbar title Text Color

SpannableString s = new SpannableString(title);
s.setSpan(new ForegroundColorSpan(Color.GREEN), 0, title.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
getSupportActionBar().setTitle(s);
<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">

You could find how to customize ActionBar here . 您可以在此处找到如何自定义ActionBar I recommend to use ToolBar and Theme.AppCompat instead - example . 我建议使用ToolBarTheme.AppCompat代替 - 例如

I am assuming you are using ToolBar as ActionBar . 我假设你使用ToolBar作为ActionBar Of all the answers, I think this is the easiest one to implement. 在所有答案中,我认为这是最容易实现的答案。 And works on all API versions. 适用于所有API版本。

So here it is: 所以这里是:

Action bar title uses textColorPrimary as its text color. 操作栏标题使用textColorPrimary作为其文本颜色。 So to change that in styles.xml create a new style with an item for textColorPrimary and set whatever color you need. 因此,要在styles.xml中更改它,请为textColorPrimary创建一个带有项目的新样式,并设置您需要的任何颜色。

<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. 现在,将该样式添加为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. 现在你得到了你需要的东西。

Just add app:titleTextColor="@color/CUSTOM_COLOR" like so: 只需添加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"/>

wandering around StackOverflow I found this answer which finally worked. 在StackOverflow周围闲逛我发现这个答案终于奏效了。

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>

When i got the same problem, i used: 当我遇到同样的问题时,我使用了:

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

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

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. 首先,我使用AppCompatActivity作为基本活动并获得错误。 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 . 在我看来,你的代码用于Theme.Holo的主题,现在我们使用Theme.AppCompat。 So if you want change, just reference Changing Background and text color of AppCompat Light DarkActionBar Theme on android . 因此,如果您想要更改,只需在android上引用更改背景和AppCompat Light DarkActionBar主题的文本颜色 It works for me.Good luck for you. 它对我有用。祝你好运。

Seems you've used style instead of theme . 似乎你使用的是style而不是theme Styles and themes differ . 样式和主题不同 So replace 所以更换

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

With

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

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: 所以,在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>

if you change what's inside the parent="..." to dark or light themes, it adapts the text color to these themes. 如果您将parent =“...”内的内容更改为深色或浅色主题,则会将文本颜色调整为这些主题。 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. 例如,现在你有一个黑暗的主题,文本是白色的,但当我更改为“Theme.AppCompat.Light”时,文本颜色变为黑色。

Hope it helps in some way. 希望它在某种程度上有所帮助。

Change the Activity theme as: Activity主题更改为:

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

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

</style>

Just had this problem, finally figured it out. 刚出现这个问题,终于搞清楚了。 In your style.xml do not specify a background color for your toolbar. 在style.xml中,不要为工具栏指定背景颜色。 Then define the color in each layout.xml file: 然后在每个layout.xml文件中定义颜色:

<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. 这使您可以灵活地根据活动轻松拥有不同的操作栏颜色(如果需要)。

This is a usable code.这是一个可用的代码。 Follow this code.遵循此代码。

<style name="AppTheme" parent="Theme.AppCompat.Light">
        <!-- Customize your theme here. -->
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name="colorAccent">@color/colorApp</item>
</style>

<style name="TextAppearance.AppCompat.Widget.ActionBar.Title" parent="@android:style/TextAppearance">
    <item name="android:textSize">23dp</item>
    <item name="android:textColor">@color/YOUR_COLOR</item>
</style>

In second style tag we customize the actionBar title.在第二个样式标签中,我们自定义 actionBar 标题。

Now Add this above code in the application in Manifest File现在在Manifest File中的应用程序中添加上面的代码

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM