选项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)
这些方法需要一个颜色整数而不是颜色资源ID!
选项2)覆盖工具栏样式和主题属性
layout/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"/>
值/样式.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">
<item name="android:textColorPrimary">@color/actionbar_title_text</item>
</style>
救命啊!我的图标颜色也变了!
@PeterKnut反映这会影响溢出按钮、导航抽屉按钮和返回按钮的颜色,还会改变SearchView
的文本颜色。
关于图标颜色:在暗色主题(白字黑底)中,colorControlNormal
继承自android:textColorPrimary
;在亮色主题(黑字白底)中则继承自android:textColorSecondary
。
- 如果你将其应用到操作栏主题上,你可以自定义图标颜色。
<item name="colorControlNormal">#de000000</item>
在appcompat-v7版本号低于r23的情况下存在一个bug,需要你这样同时覆盖本地对应项:
There was a bug in appcompat-v7 up to r23 which required you to also override the native counterpart like so:
返回:
在appcompat-v7版本号低于r23的情况下存在一个bug,需要你这样同时覆盖本地对应项:
<item name="android:colorControlNormal" tools:ignore="NewApi">?colorControlNormal</item>
救命啊!我的SearchView出了问题!
注意:本节内容可能已经过时。
由于您使用的搜索小部件某种原因使用的是与appcompat-v7中包含的箭头不同的返回箭头(在视觉上不同,但技术上不同),您需要在应用程序主题中手动设置它。支持库的可绘制对象会正确地获取色调。否则它将始终为白色。
<item name="homeAsUpIndicator">@drawable/abc_ic_ab_back_mtrl_am_alpha</item>
关于搜索视图文本...没有简单的方式。在查看其源代码后,我找到了一种获取文本视图的方法。我尚未测试过这个方法,如果不起作用,请在评论中告诉我。
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>
ThemeOverlay
中定义背景并将其用作主题,那么每个子元素的背景也会被着色。 - Eugen Pechanec