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


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


当前回答

我们需要在工具栏中使用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”的文件夹,如下面的截图所示。

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

其他回答

您可以直接在XML文件中使用HTML <font>属性更改任何文本的颜色。 例如在strings.xml中:

<resources>
    <string name = "app_name">
        <html><font color="#001aff">Multi</font></html>
        <html><font color="#ff0044">color</font></html>
        <html><font color="#e9c309">Text </font></html>
    </string>
</resources>

一个很好的解决方案是使用SpannableStringBuilder并这样设置颜色。你甚至可以在字符串的不同部分使用不同的颜色,添加图像等。

使用新的支持库进行测试。

查看Android:使用TextView.setText()为字符串的一部分着色?

我已经用简单的一行代码完成了

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

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

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

或者用这样的方法:

setTitleTextColor()

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

这些函数运行良好

在Java中

private void setActionbarTextColor(ActionBar actBar, int color) {

    String title = actBar.getTitle().toString();
    Spannable spannablerTitle = new SpannableString(title);
    spannablerTitle.setSpan(new ForegroundColorSpan(color), 0, spannablerTitle.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
    actBar.setTitle(spannablerTitle);

}

然后使用它只需输入你的动作栏和新的颜色。

ActionBar actionBar = getActionBar();    // Or getSupportActionBar() if using appCompat
int red = Color.RED
setActionbarTextColor(actionBar, red);

在Kotlin

你可以像这样使用扩展函数:

private fun ActionBar.setTitleColor(color: Int) {
    val text = SpannableString(title ?: "")
    text.setSpan(ForegroundColorSpan(color),0,text.length, Spannable.SPAN_INCLUSIVE_INCLUSIVE)
    title = text
}

然后应用到你的动作栏

actionBar?.setTitleColor(Color.RED)