操作栏文字颜色

313

如何更改ActionBar的文本颜色?我继承了Holo Light主题,能够更改ActionBar的背景,但找不到可调整的属性来更改文本颜色。


好的,我可以使用android:textColorPrimary属性更改文本颜色,但这也会更改在ActionBar按钮上溢出时显示的下拉菜单的文本颜色。有什么办法可以更改那些下拉菜单/列表的颜色吗?


我刚刚发布了一个对我有效的解决方案。请查看这里https://dev59.com/c2025IYBdhLWcg3wVkYR#16175220 - spaceMonkey
28个回答

0

这不是推荐的解决方案,因为我在这里使用了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();
    }

}

0

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

app:titleTextColor="@color/...."
app:subtitleTextColor="@color/...." 
或者通过类似这样的方法:
setTitleTextColor()

如需更多属性,请查看此link


0
我们需要使用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文件中添加主题,并在该样式中定义字体系列,如下面的代码片段所示。

<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”的文件夹,就像下面的截图一样。

enter image description here

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

enter image description here


0

0

对于 Android 5(棒棒糖)版本,您需要使用 android:actionBarPopupTheme 来设置溢出菜单的 textColor


0

你可以通过在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>

enter image description here


0
如果您需要以编程方式设置颜色,这是实现的方法:
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);
}

如果标题更改会发生什么? - Humpity
@Humpity 如果您在标题更改时不调用此函数,则会失去颜色。 - TBog

0
<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]

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接