【问题标题】:Change Action Bar Title color更改操作栏标题颜色
【发布时间】:2013-04-20 20:58:13
【问题描述】:

我的代码如下,当它工作时(当我将父主题更改为 Theme.Sherlock 或 Theme.Sherlock.Light 时,它确实改变了主题)它不会改变标题颜色。

代码和here差不多

代码:

<?xml version="1.0" encoding="utf-8"?>
<resources>

<style name="MyTheme" parent="@style/Theme.Sherlock">
 <item name="actionBarStyle">@style/MyTheme.ActionBarStyle</item>
 <item name="android:actionBarStyle">@style/MyTheme.ActionBarStyle</item>
</style>

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

<style name="MyTheme.ActionBar.TitleTextStyle" parent="@style/TextAppearance.Sherlock.Widget.ActionBar.Title" >
     <item name="android:textColor">#FF0000</item>
</style>

</resources>

【问题讨论】:

    标签: android android-actionbar actionbarsherlock


    【解决方案1】:

    我已经像这样改变了标题颜色

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

    【讨论】:

    • 最简单的一个。谢谢。
    【解决方案2】:

    来自 Jake Wharton 的 ActionBarSherlock 网站:

    镜像属性

    由于 Android 主题系统的限制,任何主题 定制必须在两个属性中声明。正常的 android-prefixed 属性将主题应用到本机操作栏 并且不带前缀的属性用于自定义实现。

    必须将 MyTheme.ActionBarStyle 更改为:

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

    现在标题文本颜色已更改。

    【讨论】:

    • 但它发出警告 android:titleTextStyle 需要 API 级别 11(当前最低为 10)我正在使用 ABC !当我们使用 api level 10 使用 ABS 或 ABC 时如何使用它
    • 为我工作!谢谢!
    • @LOG_TAG 你需要在 values-v11 目录下的单独的 styles.xml 文件中定义它,所以 API >= 11 使用这个样式并且 API
    【解决方案3】:

    使用下面的代码为操作栏文本和操作栏背景提供不同的颜色,只需在清单中针对您想要输出的活动使用下面的主题:)

     <style name="MyTheme" parent="@android:style/Theme.Holo.Light">
            <item name="android:actionBarStyle">@style/MyActionBar</item>
        </style>
    
        <style name="MyActionBar" parent="@android:style/Widget.Holo.Light.ActionBar">
            <item name="android:titleTextStyle">@style/TitleBarTextColor</item>
            <item name="android:background">YOUR_COLOR_CODE</item>
        </style>
    
        <style name="TitleBarTextColor" parent="@style/TextAppearance.AppCompat.Widget.ActionBar.Title">
            <item name="android:textColor">YOUR_COLOR_CODE</item>
        </style>
    

    【讨论】:

      【解决方案4】:

      对我来说最简单的方法是在主题样式中添加以下内容:

      <item name="android:textColorPrimary">@color/orange_dark</item>
      

      【讨论】:

      • 这是不对的。它将改变整个应用程序文本的颜色。
      【解决方案5】:

      上面的方法我都试过了,还是不行。我刚刚通过下面的代码实现了..

      Spannable text = new SpannableString(actionBar.getTitle());
      text.setSpan(new ForegroundColorSpan(Color.BLUE), 0, text.length(), Spannable.SPAN_INCLUSIVE_INCLUSIVE);
      actionBar.setTitle(text);
      

      【讨论】:

        【解决方案6】:

        如果您尝试更改具有自定义工具栏的标题文本,请尝试将app:titleTextColor 添加到工具栏,如下所示:

         <android.support.v7.widget.Toolbar
                android:id="@+id/my_toolbar"
                android:layout_width="match_parent"
                android:layout_height="?attr/actionBarSize"
                android:background="?attr/colorPrimary"
                android:elevation="4dp"
                android:theme="@style/ThemeOverlay.AppCompat.ActionBar"
                app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
                app:titleTextColor="@color/colorWhite" />
        

        【讨论】:

          【解决方案7】:

          在您的 value 文件夹中的 style.xml 中,这将更改您的操作栏颜色。将 #666666 替换为您选择的标题背景颜色颜色代码,并将 #000000 替换为您的标题文本颜色。

          <style name="MyTheme" parent="@android:style/Theme.Holo.Light">
              <item name="android:actionBarStyle">@style/NewActionBar</item>
          </style>
          
          <style name="NewActionBar" parent="@android:style/Widget.Holo.Light.ActionBar">
          <item name="android:titleTextStyle">@style/TitleBarTextColor</item>
          <item name="android:background">#666666</item>
          </style>
          <style name="TitleBarTextColor" parent="@style/TextAppearance.Sherlock.Widget.ActionBar.Menu">
              <item name="android:textColor">#000000</item>
          </style>
          

          然后别忘了编辑你的清单文件 -> android:theme="@style/NewTheme"

              <application
              android:allowBackup="true"
              android:icon="@drawable/ic_launcher"
              android:label="@string/app_name"
              android:theme="@style/NewTheme" >
              <activity
                  android:name="com.nearby.welcome.Splash"
                  android:label="@string/app_name" >
                  <intent-filter>
                      <action android:name="android.intent.action.MAIN" />
          
                      <category android:name="android.intent.category.LAUNCHER" />
                  </intent-filter>
              </activity>
          
          </application>
          

          【讨论】:

            【解决方案8】:

            如果您想更改 ActionBar 标题颜色,请先按照此操作。 转到 style.xml 并在资源中使用以下代码

            <style name="ActionBar.Solid.Ribbit.TitleTextStyle"
                parent="TextAppearance.AppCompat.Widget.ActionBar.Title">
                <item name="android:textColor">@color/actionBarTextColor</item>
            </style>
            

            【讨论】: