你如何设置新工具栏的标题颜色?

我正在使用新的V7工具栏,对于我的生活无法弄清楚如何改变标题的颜色。 我已经将工具栏的@style设置为styles.xml中声明的样式,并使用textColor应用titleTextStyle。 我错过了什么吗? 我正在编写棒棒糖,但是目前正在Kitkat设备上进行testing。

styles.xml

<resources> <!-- Base application theme. --> <style name="AppTheme" parent="@style/Theme.AppCompat.Light"> <item name="android:windowNoTitle">true</item> <item name="windowActionBar">false</item> </style> <style name="ActionBar" parent="Theme.AppCompat"> <item name="android:background">@color/actionbar_background</item> <item name="android:titleTextStyle">@style/ActionBar.TitleTextStyle</item> </style> <style name="ActionBar.TitleTextStyle" parent="@style/TextAppearance.AppCompat.Widget.ActionBar.Title"> <item name="android:textColor">@color/actionbar_title_text</item> </style> </resources> 

actionbar.xml:

 <android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/toolbar_actionbar" android:layout_width="match_parent" android:layout_height="?actionBarSize" style="@style/ActionBar"/> 

选项1)快速简单的方法(仅工具栏)

由于appcompat-v7-r23,您可以直接在Toolbar或其样式上使用以下属性:

 app:titleTextColor="@color/primary_text" app:subtitleTextColor="@color/secondary_text" 

如果您的最小SDK为23,并且您使用本地Toolbar只需将名称空间前缀更改为android

在Java中,您可以使用以下方法:

 toolbar.setTitleTextColor(Color.WHITE); toolbar.setSubtitleTextColor(Color.WHITE); 

这些方法使用颜色int而不是颜色资源ID!

选项2)覆盖工具栏样式和主题属性

布局/ xxx.xml

 <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:minHeight="?attr/actionBarSize" android:theme="@style/ThemeOverlay.MyApp.ActionBar" app:popupTheme="@style/ThemeOverlay.AppCompat.Light" style="@style/Widget.MyApp.Toolbar.Solid"/> 

价值观/ styles.xml

 <style name="Widget.MyApp.Toolbar.Solid" parent="Widget.AppCompat.ActionBar"> <item name="android:background">@color/actionbar_color</item> <item name="android:elevation" tools:ignore="NewApi">4dp</item> <item name="titleTextAppearance">...</item> </style> <style name="ThemeOverlay.MyApp.ActionBar" parent="ThemeOverlay.AppCompat.ActionBar"> <!-- Parent theme sets colorControlNormal to textColorPrimary. --> <item name="android:textColorPrimary">@color/actionbar_title_text</item> </style> 

帮帮我! 我的图标也改变了颜色!

@PeterKnut报告这影响溢出button,导航抽屉button和后退button的颜色。 它也改变了SearchView文字颜色。

关于图标颜色: colorControlNormalinheritance自

  • android:textColorPrimary黑色主题(白色黑色)
  • android:textColorSecondary轻的主题(白底黑字)

如果将此应用于操作栏的主题 ,则可以自定义图标颜色。

 <item name="colorControlNormal">#de000000</item> 

appcompat-v7到r23有一个错误,这个错误也要求你重写本地的对应部分,比如:

 <item name="android:colorControlNormal" tools:ignore="NewApi">?colorControlNormal</item> 

帮帮我! 我的SearchView是一团糟!

注意:本节可能已经过时。

由于您使用的search小部件由于某种原因使用了与appcompat-v7中包含的search小部件不同的后退箭头(不是在视觉上,技术上),您必须在应用程序主题中手动设置它。 支持库的绘图正确着色。 否则,它将永远是白色的。

 <item name="homeAsUpIndicator">@drawable/abc_ic_ab_back_mtrl_am_alpha</item> 

至于search查看文本…有没有简单的方法。 挖掘其源代码后,我find了一种方法来到文本视图。 我没有testing过,所以请让我知道如果这不起作用。

 SearchView sv = ...; // get your search view instance in onCreateOptionsMenu // prefix identifier with "android:" if you're using native SearchView TextView tv = sv.findViewById(getResources().getIdentifier("id/search_src_text", null, null)); tv.setTextColor(Color.GREEN); // and of course specify your own color 

奖金:重写ActionBar风格和主题属性

适用于默认操作appcompat-v7操作栏的样式将如下所示:

 <!-- ActionBar vs Toolbar. --> <style name="Widget.MyApp.ActionBar.Solid" parent="Widget.AppCompat.ActionBar.Solid"> <item name="background">@color/actionbar_color</item> <!-- No prefix. --> <item name="elevation">4dp</item> <!-- No prefix. --> <item name="titleTextStyle">...</item> <!-- Style vs appearance. --> </style> <style name="Theme.MyApp" parent="Theme.AppCompat"> <item name="actionBarStyle">@style/Widget.MyApp.ActionBar.Solid</item> <item name="actionBarTheme">@style/ThemeOverlay.MyApp.ActionBar</item> <item name="actionBarPopupTheme">@style/ThemeOverlay.AppCompat.Light</item> </style> 

如果您只需要更改标题的颜色而不更改search窗口小部件的文字颜色,那么这里是我的解决scheme。

布局/ toolbar.xml

 <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:background="@color/toolbar_bg" app:theme="@style/AppTheme.Toolbar" app:titleTextAppearance="@style/AppTheme.Toolbar.Title" android:layout_width="match_parent" android:layout_height="wrap_content" android:minHeight="?attr/actionBarSize"/> 

值/的themes.xml

 <resources> <style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar"> <item name="windowActionBar">false</item> </style> <style name="AppTheme.Toolbar" parent="ThemeOverlay.AppCompat.ActionBar"> <!-- Customize color of navigation drawer icon and back arrow --> <item name="colorControlNormal">@color/toolbar_icon</item> </style> <style name="AppTheme.Toolbar.Title" parent="TextAppearance.Widget.AppCompat.Toolbar.Title"> <!-- Set proper title size --> <item name="android:textSize">@dimen/abc_text_size_title_material_toolbar</item> <!-- Set title color --> <item name="android:textColor">@color/toolbar_title</item> </style> </resources> 

以类似的方式,你也可以设置subtitleTextAppearance。

如果您支持API 23及以上版本,则现在可以使用titleTextColor属性来设置工具栏的标题颜色。

布局/ toolbar.xml

 <android.support.v7.widget.Toolbar android:id="@+id/toolbar" android:layout_width="match_parent" android:layout_height="wrap_content" android:titleTextColor="@color/colorPrimary" /> 

MyActivity.java

 Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar) toolbar.setTitleTextColor(Color.WHITE); 

在我的android.support.v7.widget.Toolbar设置app:titleTextColor适用于Android 4.4和6.0的com.android.support:appcompat-v7:23.1.0

 <android.support.v7.widget.Toolbar android:id="@+id/toolbar" android:layout_height="wrap_content" android:layout_width="match_parent" android:minHeight="?attr/actionBarSize" android:background="?attr/colorPrimary" app:titleTextColor="@android:color/white" /> 

这让我很烦恼,而且我也不喜欢给出的答案,所以我看了一下源代码来看看它是如何工作的。

FractalWrench是在正确的道路上,但它可以在API 23下使用,而不必在自己的工具栏上设置。

正如其他人所说,你可以在工具栏上设置样式

 app:theme="@style/ActionBar" 

并以这种风格,你可以设置标题的文字颜色

 <item name="titleTextColor">#00f</item> 

预先API 23或23+

 <item name="android:titleTextColor">your colour</item> 

完整的XML

 <android.support.v7.widget.Toolbar android:id="@+id/toolbar" android:layout_height="?attr/actionBarSize" android:layout_width="match_parent" app:theme="@style/ActionBar" app:popupTheme="@style/ThemeOverlay.AppCompat.Dark"/> <style name="ActionBar" parent="@style/ThemeOverlay.AppCompat.ActionBar"> <item name="android:titleTextStyle">@style/ActionBarTextStyle</item> <item name="titleTextColor">your colour</item> <item name="android:background">#ff9900</item> </style> 

下面是可以为工具栏设置的所有属性

 <declare-styleable name="Toolbar"> <attr name="titleTextAppearance" format="reference" /> <attr name="subtitleTextAppearance" format="reference" /> <attr name="title" /> <attr name="subtitle" /> <attr name="gravity" /> <attr name="titleMargins" format="dimension" /> <attr name="titleMarginStart" format="dimension" /> <attr name="titleMarginEnd" format="dimension" /> <attr name="titleMarginTop" format="dimension" /> <attr name="titleMarginBottom" format="dimension" /> <attr name="contentInsetStart" /> <attr name="contentInsetEnd" /> <attr name="contentInsetLeft" /> <attr name="contentInsetRight" /> <attr name="maxButtonHeight" format="dimension" /> <attr name="navigationButtonStyle" format="reference" /> <attr name="buttonGravity"> <!-- Push object to the top of its container, not changing its size. --> <flag name="top" value="0x30" /> <!-- Push object to the bottom of its container, not changing its size. --> <flag name="bottom" value="0x50" /> </attr> <!-- Icon drawable to use for the collapse button. --> <attr name="collapseIcon" format="reference" /> <!-- Text to set as the content description for the collapse button. --> <attr name="collapseContentDescription" format="string" /> <!-- Reference to a theme that should be used to inflate popups shown by widgets in the toolbar. --> <attr name="popupTheme" format="reference" /> <!-- Icon drawable to use for the navigation button located at the start of the toolbar. --> <attr name="navigationIcon" format="reference" /> <!-- Text to set as the content description for the navigation button located at the start of the toolbar. --> <attr name="navigationContentDescription" format="string" /> <!-- Drawable to set as the logo that appears at the starting side of the Toolbar, just after the navigation button. --> <attr name="logo" /> <!-- A content description string to describe the appearance of the associated logo image. --> <attr name="logoDescription" format="string" /> <!-- A color to apply to the title string. --> <attr name="titleTextColor" format="color" /> <!-- A color to apply to the subtitle string. --> <attr name="subtitleTextColor" format="color" /> </declare-styleable> 

这对我有效

 <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="?attr/colorPrimary" android:fitsSystemWindows="true" android:minHeight="?attr/actionBarSize" app:navigationIcon="@drawable/ic_back" app:popupTheme="@style/ThemeOverlay.AppCompat.Light" app:subtitleTextColor="@color/white" app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar" app:title="This week stats" app:titleTextColor="@color/white"> <ImageView android:id="@+id/submitEditNote" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentEnd="true" android:layout_alignParentRight="true" android:layout_gravity="right" android:layout_marginRight="10dp" android:src="@android:drawable/ic_menu_manage" /> </android.support.v7.widget.Toolbar> 

用于更改颜色

 <android.support.v7.widget.Toolbar android:id="@+id/toolbar" android:layout_height="wrap_content" android:layout_width="match_parent" android:background="?attr/colorPrimary" 

/>

 Toolbar myToolbar = (Toolbar) findViewById(R.id.toolbar); myToolbar.setTitleTextColor(ContextCompat.getColor(getApplicationContext(), R.color.Auth_Background)); setSupportActionBar(myToolbar); 

CollapsingToolbarLayout更改Toolbar标题颜色最简单的方法。

将以下样式添加到CollapsingToolbarLayout

 <android.support.design.widget.CollapsingToolbarLayout app:collapsedTitleTextAppearance="@style/CollapsedAppBar" app:expandedTitleTextAppearance="@style/ExpandedAppBar"> 

styles.xml

 <style name="ExpandedAppBar" parent="@android:style/TextAppearance"> <item name="android:textSize">24sp</item> <item name="android:textColor">@android:color/black</item> <item name="android:textAppearance">@style/TextAppearance.Lato.Bold</item> </style> <style name="CollapsedAppBar" parent="@android:style/TextAppearance"> <item name="android:textColor">@android:color/black</item> <item name="android:textAppearance">@style/TextAppearance.Lato.Bold</item> </style> 

如果你可以使用appcompat-v7 app:titleTextColor =“#fff”>

 <android.support.v7.widget.Toolbar android:id="@+id/toolbar" android:layout_width="match_parent" android:layout_height="wrap_content" android:background="@color/colorPrimary" android:visibility="gone" app:titleTextColor="#fff"> </android.support.v7.widget.Toolbar> 
 <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="wrap_content" xmlns:app="http://schemas.android.com/apk/res-auto"> <android.support.v7.widget.Toolbar android:id="@+id/toolbar" android:layout_width="match_parent" android:layout_height="wrap_content" app:titleTextColor="@color/white" android:background="@color/green" />