如何更改动作栏的文本颜色?我继承了Holo光主题,我能够改变动作栏的背景,但我不知道什么是属性调整来改变文本颜色。


好,我能够改变文本颜色与属性android:textColorPrimary但它也改变了文本颜色的下拉菜单显示时,溢出发生在动作栏按钮。知道如何改变那些下拉菜单/列表的颜色吗?


当前回答

我发现了一种适用于任何类型的ActionBar (Sherlock, Compat和Native)的方法:

只需使用html设置标题,并指定文本颜色。例如,要将ActionBar文本颜色设置为红色,只需这样做:

getActionBar()/* or getSupportActionBar() */.setTitle(Html.fromHtml("<font color=\"red\">" + getString(R.string.app_name) + "</font>"));

你也可以使用红色十六进制代码#FF0000来代替单词red。如果你对此有问题,请参阅Android Html.fromHtml(String)不适用于<font color='#'>text</font>。


此外,如果你想使用颜色资源,这段代码可以用来获得正确的HEX字符串,并在需要时删除alpha(字体标签不支持alpha):

int orange = getResources().getColor(R.color.orange);
String htmlColor = String.format(Locale.US, "#%06X", (0xFFFFFF & Color.argb(0, Color.red(orange), Color.green(orange), Color.blue(orange))));

其他回答

在操作栏上设置HTML字符串在SDK v21+的Material主题上不起作用

如果你想改变它,你应该在style.xml中设置主文本颜色

<resources>
    <!-- Base application theme. -->
    <style name="AppTheme" parent="android:Theme.Material.Light">
        <!-- Customize your theme here. -->
        <item name="android:textColorPrimary">@color/actionbar-text-color</item>
    </style>
</resources>    

如果您正在使用自定义操作栏,您可以使用这些属性:

app:titleTextColor="@color/...."
app:subtitleTextColor="@color/...." 

或者用这样的方法:

setTitleTextColor()

欲了解更多属性,请查看此链接

好吧,我找到了一个更好的办法。我现在只能改变标题的颜色。你也可以调整副标题。

下面是我的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>

我们需要在工具栏中使用app:theme属性定义主题,如下面的代码片段所示。

<androidx.appcompat.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:theme="@style/AppTheme.ToolbarFont" />

在此之前,我们必须在我们的styles.xml文件中添加主题,我们必须像下面的代码片段那样定义该样式的font-family。

<style name="AppTheme.ToolbarFont" parent="AppTheme">
<!--This line changes the color of text in Toolbar-->
<item name="android:textColorPrimary">@color/black</item>
<!--This line changes the color of icons in toolbar (back, overflow menu icons)-->
<item name="android:textColorSecondary">@color/azul</item>
<item name="textAllCaps">false</item>
<item name="android:textSize">16sp</item>
<item name="android:fontFamily">@font/montserrat_semi_bold</item>

为了添加自定义字体,我们需要在res目录中创建一个名为“font”的文件夹,如下面的截图所示。

我们已经在工具栏中实现了自定义字体。

试着在Activity的onCreate中添加这个。适用于几乎所有Android版本。

 actionBar.setTitle(Html.fromHtml("<font color='#ffff00'>Your Title</font>"));  

or

getSupportActionBar().setTitle(Html.fromHtml("<font color='#ffff00'>Your Title</font>"));