【问题标题】:Change the text colour of action bar title更改操作栏标题的文本颜色
【发布时间】:2015-12-13 23:08:23
【问题描述】:

我想更改操作栏中标题的文本颜色,并且通过做一些研究我想出了这个。

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

这不会更改操作栏标题的文本颜色。更改文本颜色的正确方法是什么?

【问题讨论】:

    标签: android android-actionbar


    【解决方案1】:
    <?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>
    

    【讨论】:

      【解决方案2】:

      你也可以从 java 做这个,像这样

      myActionBar.setTitle(Html.fromHtml("<font color='#ff0088'>MyTitle</font>"));
      

      【讨论】:

        【解决方案3】:

        您可以创建自定义操作栏并根据需要进行更改。

        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 中调用此方法。

        或者你可以点击这个链接

        How to change the text on the action bar

        【讨论】:

          【解决方案4】:

          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>
          

          ma​​in.xml

          <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
              xmlns:app="http://schemas.android.com/apk/res-auto"
              android:layout_width="match_parent"
              android:layout_height="match_parent">
              <include layout="@layout/toolbar"/>
          </RelativeLayout> 
          

          在你的活动中

          toolbar = (Toolbar) findViewById(R.id.toolbar);
                      toolbar.setBackgroundColor(getResources().getColor(R.color.base_color));
                      setSupportActionBar(toolbar);
                      getSupportActionBar().setTitle("");
                      getSupportActionBar().setDisplayHomeAsUpEnabled(true);
                      TextView userName = (TextView) findViewById(R.id.tool_head);
                      userName.setText(mTag);
                     //Set text color here if you need to do programmatically 
          

          【讨论】:

            【解决方案5】:

            试试这个,

            你可以像这样改变你的主题。

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

            现在文字颜色会改变。

            【讨论】: