如何更改ActionBar的文本颜色?我继承了Holo Light主题,能够更改ActionBar的背景,但找不到可调整的属性来更改文本颜色。
好的,我可以使用android:textColorPrimary属性更改文本颜色,但这也会更改在ActionBar按钮上溢出时显示的下拉菜单的文本颜色。有什么办法可以更改那些下拉菜单/列表的颜色吗?
如何更改ActionBar的文本颜色?我继承了Holo Light主题,能够更改ActionBar的背景,但找不到可调整的属性来更改文本颜色。
好的,我可以使用android:textColorPrimary属性更改文本颜色,但这也会更改在ActionBar按钮上溢出时显示的下拉菜单的文本颜色。有什么办法可以更改那些下拉菜单/列表的颜色吗?
这不是推荐的解决方案,因为我在这里使用了Android API,但由于我的应用程序需要根据条件动态更改主题,所以无法在xml中实现,所以我需要这样做。但是这个解决方案非常好用。
/**
*
* @author Kailash Dabhi
* @email kailash09dabhi@gmail.com
*
*/
public static void setActionbarTextColor(Activity activity, int color) {
Field mActionViewField;
try {
mActionViewField = activity.getActionBar().getClass()
.getDeclaredField("mActionView");
mActionViewField.setAccessible(true);
Object mActionViewObj = mActionViewField.get(activity
.getActionBar());
Field mTitleViewField = mActionViewObj.getClass().getDeclaredField(
"mTitleView");
mTitleViewField.setAccessible(true);
Object mTitleViewObj = mTitleViewField.get(mActionViewObj);
TextView mActionBarTitle = (TextView) mTitleViewObj;
mActionBarTitle.setTextColor(color);
// Log.i("field", mActionViewObj.getClass().getName());
} catch (NoSuchFieldException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (IllegalArgumentException e) {
e.printStackTrace();
}
}
app:titleTextColor="@color/...."
app:subtitleTextColor="@color/...."
setTitleTextColor()
如需更多属性,请查看此link。
<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文件中添加主题,并在该样式中定义字体系列,如下面的代码片段所示。
<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”的文件夹,就像下面的截图一样。
我们在工具栏中实现了自定义字体。对于 Android 5(棒棒糖)版本,您需要使用 android:actionBarPopupTheme 来设置溢出菜单的 textColor。
static void setActionBarTextColor(Activity activity, int color)
{
ActionBar actionBar = activity instanceof AppCompatActivity
? ((AppCompatActivity) activity).getSupportActionBar()
: activity.getActionBar();
String title = activity.getTitle(); // or any title you want
SpannableString ss = new SpannableString(title);
ss.setSpan(new ForegroundColorSpan(color), 0, title.length(), Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
actionBar.setTitle(ss);
}
<android.support.v7.widget.Toolbar
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/color_primary"
android:theme="@style/GalaxyZooThemeToolbarDarkOverflow"
app:popupTheme="@style/Theme.AppCompat.NoActionBar" />
<style name="GalaxyZooThemeToolbarDarkOverflow" parent="Theme.AppCompat.NoActionBar">
<!-- android:textColorPrimary is the color of the title text
in the Toolbar, in the Theme.AppCompat theme: -->
<item name="android:textColorPrimary">@color/abc_primary_text_material_light</item>
<!-- android:textColorPrimaryInverse is the color of the title
text in the Toolbar, in the Theme.AppCompat.Light theme: -->
<!-- <item name="android:textColorPrimaryInverse">@color/abc_primary_text_material_light</item> -->
<!-- android:actionMenuTextColor is the color of the text of
action (menu) items in the Toolbar, at least in the
Theme.AppCompat theme.
For some reason, they already get the textColorPrimary
when running on API 21, but not on older versions of
Android, so this is only necessary to support older
Android versions.-->
<item name="actionMenuTextColor">@color/abc_primary_text_material_light</item>
<!-- android:textColorSecondary is the color of the menu
overflow icon (three vertical dots) -->
<item name="android:textColorSecondary">@color/abc_secondary_text_material_light</item>
<!-- This would set the toolbar's background color,
but setting this also changes the popup menu's background,
even if we define popupTheme for our <Toolbar> -->
<!-- <item name="android:background">@color/color_primary</item> -->
</style>
**Reference:**
[https://www.murrayc.com/permalink/2014/10/28/android-changing-the-toolbars-text-color-and-overflow-icon-color/][1]