Android 更改操作栏标题的文本颜色

Android 更改操作栏标题的文本颜色,android,android-actionbar,Android,Android Actionbar,我想改变动作栏标题的文字颜色,通过做一些研究,我得出了这个结论 <resources xmlns:tools="http://schemas.android.com/tools" xmlns:android="http://schemas.android.com/apk/res/android"> <style name="MyTheme" parent="@style/Theme.AppCompat.Light"> <item name=

我想改变动作栏标题的文字颜色,通过做一些研究,我得出了这个结论

<resources xmlns:tools="http://schemas.android.com/tools" xmlns:android="http://schemas.android.com/apk/res/android">

    <style name="MyTheme" parent="@style/Theme.AppCompat.Light">
        <item name="android:actionBarStyle"   tools:ignore="NewApi">@style/MyActionBar</item>
        <item name="actionBarStyle">@style/MyActionBar</item>
        <item name="android:windowContentOverlay">@null</item>
    </style>

    <style name="MyActionBar" parent="@style/Widget.AppCompat.Light.ActionBar.Solid.Inverse">
        <item name="android:background"  tools:ignore="NewApi">@color/action_bar_blue</item>
        <item name="background">@color/action_bar_blue</item>
        <item name="android:titleTextStyle">@style/MyActionBarTitleTextStyle</item>
    </style>

    <style name="MyActionBarTitleTextStyle" parent="@style/TextAppearance.AppCompat.Widget.ActionBar.Title">
        <item name="android:textColor">@android:color/white</item>
    </style>

</resources>

@样式/MyActionBar
@样式/MyActionBar
@空的
@颜色/动作条\蓝色
@颜色/动作条\蓝色
@style/MyActionBarTitleTextStyle
@android:彩色/白色
这不会更改操作栏标题的文本颜色。更改文本颜色的正确方法是什么?


<?xml version="1.0" encoding="utf-8"?>
<resources>
  <style name="MyTheme" parent="@android:style/Theme.Holo.Light">
    <item name="android:actionBarStyle">@style/MyTheme.ActionBarStyle</item>
  </style>

  <style name="MyTheme.ActionBarStyle" parent="@android:style/Widget.Holo.Light.ActionBar">
    <item name="android:titleTextStyle">@style/MyTheme.ActionBar.TitleTextStyle</item>
  </style>

  <style name="MyTheme.ActionBar.TitleTextStyle" parent="@android:style/TextAppearance.Holo.Widget.ActionBar.Title">
    <item name="android:textColor">@color/red</item>
  </style>
</resources>
@style/MyTheme.ActionBarStyle @style/MyTheme.ActionBar.TitleTextStyle @颜色/红色
您可以创建自定义actionbar并根据需要进行更改

private void getCustomActionbar() {
        // TODO Auto-generated method stub

        ActionBar mActionBar = getActionBar();
        mActionBar.setDisplayShowHomeEnabled(false);
        mActionBar.setDisplayShowTitleEnabled(false);
        LayoutInflater mInflater = LayoutInflater.from(this);
        int width = Constants.getScreenResolution(mContext);
        View mCustomView;       

        mCustomView = mInflater.inflate(R.layout.secondcustom_actionbar, null);

        mActionBar.setCustomView(mCustomView);
        mActionBar.setDisplayShowCustomEnabled(true);
    }
在oncreate中调用此方法

或者你也可以点击这个链接


toolbar.xml

 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:id="@+id/toolbar_layout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">

        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="@dimen/toolbar_height"
            app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
            app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">

            <TextView
                android:id="@+id/tool_head"
                android:layout_width="wrap_content"
                android:layout_height="50dp"
                android:layout_marginLeft="5dp"
                android:gravity="center_vertical"
                android:text="@string/new_account_toolbar_tittle"
                android:textColor="@color/white" 
                android:textSize="@dimen/heading_text" />
        </android.support.v7.widget.Toolbar>

        <View
            android:id="@+id/shadow"
            android:layout_width="match_parent"
            android:layout_height="4dp"
            android:background="@drawable/shadow" />

    </LinearLayout>

您也可以从java执行此操作,如下所示

myActionBar.setTitle(Html.fromHtml("<font color='#ff0088'>MyTitle</font>"));
myActionBar.setTitle(Html.fromHtml(“MyTitle”);
试试这个

你可以这样改变你的主题

<style name="MyTheme.ActionBarStyle" parent="@style/Widget.Sherlock.ActionBar">
     <item name="android:titleTextStyle">@style/MyTheme.ActionBar.TitleTextStyle</item>
     <item name="titleTextStyle">@style/MyTheme.ActionBar.TitleTextStyle</item>
   </style>

@style/MyTheme.ActionBar.TitleTextStyle
@style/MyTheme.ActionBar.TitleTextStyle
现在文本颜色将更改

myActionBar.setTitle(Html.fromHtml("<font color='#ff0088'>MyTitle</font>"));
<style name="MyTheme.ActionBarStyle" parent="@style/Widget.Sherlock.ActionBar">
     <item name="android:titleTextStyle">@style/MyTheme.ActionBar.TitleTextStyle</item>
     <item name="titleTextStyle">@style/MyTheme.ActionBar.TitleTextStyle</item>
   </style>