简体   繁体   中英

Change color of the title in ActionBar

Can I know how to change the color of the title in ActionBar to black color based on the xml code below? I have tried several methods found but it doesn't really work.

Below are the part of the style xml code:::

<style name="AppTheme" parent="AppBaseTheme">
    <!-- All customizations that are NOT specific to a particular API-level can go here. -->
</style>

<style name="TestingBaseTheme" parent="Theme.AppCompat.Light">
    <item name="android:actionBarStyle">@style/ActionBarTheme</item>
    <item name="android:actionBarTabTextStyle">@style/ActionBarTabText</item>
    <item name="android:actionMenuTextColor">@color/white</item> 
    <item name="android:textColor">@color/black</item>
</style>

<style name="ActionBarTheme" parent="@android:style/Widget.Holo.Light.ActionBar.Solid.Inverse">
    <item name="android:background">@color/white</item>
</style>

<style name="ActionBarTabText" parent="@android:style/TextAppearance.Holo.Widget.ActionBar.Title">
    <item name="android:textColor">@color/white</item>
</style>

<style name="ActionBarTitleText" parent="@android:TextAppearance.Holo.Widget.ActionBar.Title">
    <item name="android:textColor">@color/yellow</item>
</style>

Here is the answer : click below link

ActionBar text color

Ok I've found a better way. I'm now able to only change the color of the title, you can also tweak the subtitle.

Here is my styles.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
  <style name="MyTheme" parent="@android:style/Theme.Holo.Light">
    <item name="android:actionBarStyle">@style/MyTheme.ActionBarStyle</item>
  </style>

  <style name="MyTheme.ActionBarStyle"parent="@android:style/Widget.Holo.Light.ActionBar">
    <item name="android:titleTextStyle">@style/MyTheme.ActionBar.TitleTextStyle</item>
  </style>

  <style name="MyTheme.ActionBar.TitleTextStyle" parent="@android:style/TextAppearance.Holo.Widget.ActionBar.Title">
    <item name="android:textColor">@color/red</item>
  </style>
</resources>

我找到了最简单最简单的代码

actionBar.setTitle(Html.fromHtml("<font color='#ff0000'>ActionBarTitle </font>"));

If I recall correctly the attribute to set ActionBar's text style is

 <item name="android:titleTextStyle" tools:ignore="NewApi"

I also noticed that you are using ActionBarCompat. If you are targeting pre HoneyComb devices you have also to add the same attribute without the android: prefix

 <item name="titleTextStyle" tools:ignore="NewApi"

在您的活动中设置 android 标签 android:label="name"

You may use this code snippet:

SpannableStringBuilder spannableStringBuilder = new SpannableStringBuilder();
SpannableString spannableString = new SpannableString(getSupportActionBar().getTitle()+"");
spannableString.setSpan(new ForegroundColorSpan(ContextCompat.getColor(MainActivity.this, R.color.yellow)), 0, spannableString.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
spannableStringBuilder.append(spannableString);
getSupportActionBar().setTitle(spannableStringBuilder);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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