如何更改动作栏的文本颜色?我继承了Holo光主题,我能够改变动作栏的背景,但我不知道什么是属性调整来改变文本颜色。
好,我能够改变文本颜色与属性android:textColorPrimary但它也改变了文本颜色的下拉菜单显示时,溢出发生在动作栏按钮。知道如何改变那些下拉菜单/列表的颜色吗?
如何更改动作栏的文本颜色?我继承了Holo光主题,我能够改变动作栏的背景,但我不知道什么是属性调整来改变文本颜色。
好,我能够改变文本颜色与属性android:textColorPrimary但它也改变了文本颜色的下拉菜单显示时,溢出发生在动作栏按钮。知道如何改变那些下拉菜单/列表的颜色吗?
当前回答
在操作栏上设置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>
其他回答
ActionBar ID不是直接可用的,所以你必须做一点hack。
int actionBarTitleId = Resources.getSystem().getIdentifier("action_bar_title", "id", "android");
if (actionBarTitleId > 0) {
TextView title = (TextView) findViewById(actionBarTitleId);
if (title != null) {
title.setTextColor(Color.RED);
}
}
我已经用简单的一行代码完成了
actionBar.setTitle(Html.fromHtml("<font color='#ff0000'>ActionBartitle </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>"));
如果你需要以编程方式设置颜色,这是一种方法:
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);
}
很多答案已弃用,所以我将更新线程,并展示如何在ActionBar(工具栏)和ActionBar弹出菜单中更改文本颜色和背景色。
在Android中(截至2018年5月)使用操作栏(工具栏)的最新方法是使用主题和NoActionBar,而不是使用工具栏。
这就是你所需要的:
In Activity (which extends AppCompatActivity) declare Toolbar: Toolbar myToolbar = (Toolbar) findViewById(R.id.toolbar); setSupportActionBar(myToolbar); Add Toolbar in the Activity's layout xml. Here we will set our custom (overwritten themes), note android:theme="@style/ThemeOverlay.AppTheme.ActionBar" and app:popupTheme="@style/ThemeOverlay.AppTheme.PopupMenu", they will do the trick. <android.support.v7.widget.Toolbar android:id="@+id/toolbar" android:layout_width="match_parent" android:layout_height="wrap_content" android:minHeight="?attr/actionBarSize" android:theme="@style/ThemeOverlay.AppTheme.ActionBar" app:popupTheme="@style/ThemeOverlay.AppTheme.PopupMenu" app:layout_constraintTop_toTopOf="parent" /> In your styles.xml you will have to overwrite those two styles to set custom colors: <style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar"> <!-- 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/ThemeOverlay.AppTheme.ActionBar</item> <item name="actionBarPopupTheme">@style/ThemeOverlay.AppTheme.PopupMenu</item> </style> <style name="ThemeOverlay.AppTheme.ActionBar" parent="ThemeOverlay.AppCompat.Dark.ActionBar"> <item name="android:textColorPrimary">@color/action_bar_text_color</item> <item name="android:background">@color/action_bar_bg_color</item> </style> <style name="ThemeOverlay.AppTheme.PopupMenu" parent="ThemeOverlay.AppCompat.Dark.ActionBar"> <item name="android:background">@color/popup_bg_color</item> <item name="android:textColorPrimary">@color/popup_text_color</item> </style>
就是这样,伙计们
附注:如果你问我,我会说:是的,整个主题的事情是一团糟。