【问题标题】:Android - Change Actionbar Title Text ColorAndroid - 更改操作栏标题文本颜色
【发布时间】:2015-11-13 17:06:05
【问题描述】:

我正在开发一个应用程序,我想change the textcolor of the actionbar title。现在,我在清单中定义了一个主题:

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

这不起作用,但我不明白为什么。这应该可以根据this answer.textview 样式工作。

我做错了什么?

更新: 改成下面的 Grace Feng 回答

【问题讨论】:

  • 不幸的是还没有

标签: android android-actionbar textview


【解决方案1】:

使用SpannableString我们可以设置Actionbar title的文字颜色

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

【讨论】:

    【解决方案2】:
    <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">
    

    还有

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

    【讨论】:

    • 哦,这很尴尬。无论如何,这无论如何都不起作用,操作栏标题保持白色,文本视图保持黑色
    【解决方案3】:

    您可以找到如何自定义ActionBar here。我建议改用ToolBarTheme.AppCompat - example

    【讨论】:

      【解决方案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 非常精确。我不得不看很多答案,直到我达到这个并且它起作用了。谢谢!
      【解决方案5】:

      只需像这样添加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"/>
      

      【讨论】:

        【解决方案6】:

        在 StackOverflow 上闲逛,我发现 this answer 终于奏效了。

        我在这里报告:

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

        【讨论】:

        • 我按照这个示例将@color/my_color 设置为#000000,我在操作栏中的文本仍然是白色的。
        • actionBar 背景和文字颜色都变成了白色。
        【解决方案7】:

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

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

        【讨论】:

          【解决方案8】:

          我认为这只是您活动的问题。我使用了与您相同的代码,并且可以正常工作。

          首先,我使用 AppCompatActivity 作为基础活动并得到错误。然后我使用活动并且它有效。 在我看来,您的代码用于 Theme.Holo 的主题,现在我们使用 Theme.AppCompat 。所以如果你想改变,只需参考Changing Background and text color of AppCompat Light DarkActionBar Theme on android。 它对我有用。祝你好运。

          【讨论】:

          • 我在活动中有一个 NavigationDrawer,所以它扩展了 AbstractNavDrawerActivity。不幸的是我没有写这段代码,所以我真的不知道写这个的人为什么这样使用它:S
          • 我看到了 AbstractNavDrawerActivity,这个活动是从 ActionBarActivity 扩展而来的。你的主题是表单 Theme.Holo 。所以它不会起作用。如果您不想更改活动,只需更改主题,如stackoverflow.com/questions/29957822/…。它会工作
          【解决方案9】:

          您似乎使用了style 而不是themeStyles and themes differ。所以替换

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

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

          【讨论】:

          • 如果我像你这样建议android studio 找不到它并返回错误,所有建议都包括:style
          • 我做到了。我知道我不应该将它用于应用程序选项卡,而应该用于单个视图。但是我的问题仍然存在,如何更改文本颜色?
          【解决方案10】:

          我不知道解决方案,但我可以用这种方法在白色和黑色之间更改文本颜色。

          所以,在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”时,文本颜色变为黑色。

          希望它在某种程度上有所帮助。

          【讨论】:

            【解决方案11】:

            Activity 主题更改为:

            <style name="ThemeName" parent="android:Theme.Holo.Light">
            
               <item name="android:actionMenuTextColor">@color/white</item>
            
            </style>
            

            【讨论】:

              【解决方案12】:

              刚遇到这个问题,终于解决了。 在您的 style.xml 中,不要为您的工具栏指定背景颜色。 然后在每个 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"/>
              

              这使您能够灵活地根据活动轻松拥有不同的操作栏颜色(如果需要)。

              【讨论】:

                【解决方案13】:

                这是一个可用的代码。遵循此代码。

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

                在第二个样式标签中,我们自定义了actionBar的标题。

                现在在Manifest File的应用程序中添加上面的代码

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

                【讨论】:

                  猜你喜欢
                  • 2015-12-13
                  • 2014-12-24
                  • 1970-01-01
                  • 2015-02-03
                  • 1970-01-01
                  • 2013-04-20
                  • 1970-01-01
                  • 1970-01-01
                  • 2023-03-09
                  相关资源
                  最近更新 更多