如何更改动作栏的文本颜色?我继承了Holo光主题,我能够改变动作栏的背景,但我不知道什么是属性调整来改变文本颜色。
好,我能够改变文本颜色与属性android:textColorPrimary但它也改变了文本颜色的下拉菜单显示时,溢出发生在动作栏按钮。知道如何改变那些下拉菜单/列表的颜色吗?
如何更改动作栏的文本颜色?我继承了Holo光主题,我能够改变动作栏的背景,但我不知道什么是属性调整来改变文本颜色。
好,我能够改变文本颜色与属性android:textColorPrimary但它也改变了文本颜色的下拉菜单显示时,溢出发生在动作栏按钮。知道如何改变那些下拉菜单/列表的颜色吗?
当前回答
一个很好的解决方案是使用SpannableStringBuilder并这样设置颜色。你甚至可以在字符串的不同部分使用不同的颜色,添加图像等。
使用新的支持库进行测试。
查看Android:使用TextView.setText()为字符串的一部分着色?
其他回答
我们需要在工具栏中使用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”的文件夹,如下面的截图所示。
我们已经在工具栏中实现了自定义字体。
这些函数运行良好
在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)
一个很好的解决方案是使用SpannableStringBuilder并这样设置颜色。你甚至可以在字符串的不同部分使用不同的颜色,添加图像等。
使用新的支持库进行测试。
查看Android:使用TextView.setText()为字符串的一部分着色?
最简单的方法是: 将这两行添加到styles.xml文件中
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
...
<item name="android:textColorSecondary">@color/white</item>
<item name="android:textColor">@color/white</item>
</style>
用你的颜色替换颜色。
这里style。xml就像
<resources>
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
<item name="actionBarTheme">@style/MyTheme</item>
</style>
<style name="MyTheme" parent="@android:style/Theme.Holo.Light">
<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/colorBlack</item>
</style>
你应该加上
<item name="actionBarTheme">@style/MyTheme</item>
在AppTheme