简体   繁体   English

更改 ActionBar 中标题的颜色

[英]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?我可以知道如何根据下面的 xml 代码将 ActionBar 中标题的颜色更改为黑色吗? I have tried several methods found but it doesn't really work.我已经尝试了几种找到的方法,但它并没有真正起作用。

Below are the part of the style xml code:::下面是样式 xml 代码的一部分:::

<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这是我的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如果我没记错的话,设置 ActionBar 文本样式的属性是

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

I also noticed that you are using ActionBarCompat.我还注意到您正在使用 ActionBarCompat。 If you are targeting pre HoneyComb devices you have also to add the same attribute without the android: prefix如果您的目标是预 HoneyComb 设备,您还必须添加相同的属性,但不带android:前缀

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

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

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