你如何设置标题的颜色为新的工具栏? [英] How do you set the title color for the new Toolbar?
问题描述
我使用的是新的V7工具栏,为我的生活不能弄清楚如何更改标题的颜色。我给自己定了工具栏的@style在styles.xml声明的风格,应用了titleTextStyle用文字颜色。我失去了一些东西?我编码棒棒糖但奇巧设备上目前正在测试。
styles.xml
<资源>
<! - 基本应用主题。 - >
<样式名称=AppTheme父=@风格/ Theme.AppCompat.Light>
<项目名称=机器人:windowNoTitle>真< /项目>
<项目名称=windowActionBar>假< /项目>
< /风格>
<样式名称=动作条父=Theme.AppCompat>
<项目名称=机器人:背景> @色/ actionbar_background< /项目>
<项目名称=机器人:titleTextStyle> @风格/ ActionBar.TitleTextStyle< /项目>
< /风格>
<样式名称=ActionBar.TitleTextStyle父=@风格/ TextAppearance.AppCompat.Widget.ActionBar.Title>
<项目名称=机器人:文字颜色> @色/ actionbar_title_text< /项目>
< /风格>
< /资源>
actionbar.xml:
< android.support.v7.widget.Toolbar的xmlns:机器人=http://schemas.android.com/apk/res/android
机器人:ID =@ + ID / toolbar_actionbar
机器人:layout_width =match_parent
机器人:actionBarSizelayout_height =
风格=@风格/动作条/>
布局/ xxx.xml
< android.support.v7.widget.Toolbar
的xmlns:机器人=http://schemas.android.com/apk/res/android
的xmlns:程序=http://schemas.android.com/apk/res-auto
机器人:ID =@ + ID /工具栏
机器人:layout_width =match_parent
机器人:layout_height =WRAP_CONTENT
机器人:=了minHeight?ATTR / actionBarSize
机器人:主题=@风格/ ThemeOverlay.MyApp.ActionBar
应用程序:popupTheme =@风格/ ThemeOverlay.AppCompat.Light
风格=@风格/ Widget.MyApp.Toolbar.Solid/>
值/ styles.xml
<样式名称=Widget.MyApp.Toolbar.Solid父=Widget.AppCompat.ActionBar>
<项目名称=机器人:背景> @色/ actionbar_color< /项目>
<项目名称=机器人:海拔工具:忽略=NewApi> 4DP< /项目>
<项目名称=titleTextAppearance> ...< /项目>
< /风格>
<样式名称=ThemeOverlay.MyApp.ActionBar父=ThemeOverlay.AppCompat.ActionBar>
<! - 父主题设置colorControlNormal到textColorPrimary。 - >
<项目名称=机器人:textColorPrimary> @色/ actionbar_title_text< /项目>
< /风格>
编辑:
@PeterKnut报道这会影响溢出按钮,抽屉式导航键和后退按钮的颜色。它也改变文字颜色搜索查看
。
关于图标的颜色:在 colorControlNormal
从继承安卓textColorPrimary
默认情况下。如果应用此给的操作栏的主题的,你可以自定义图标的颜色。
<项目名称=colorControlNormal>#de000000< /项目>
< - 工具:忽略=NewApi关闭IDE起来 - >
<! - 在API 21溢图标忽略colorControlNormal ATTR赞成原生版本 - >
<项目名称=机器人:colorControlNormal工具:忽略=NewApi> colorControlNormal< /项目>
由于您使用它由于某种原因,使用不同的后退箭头(而不是视觉上,技术上),比一个包含appcompat-V7搜索小工具,你必须在应用程序的主题手动设置的。支持库的可绘得到正确着色。否则,它会始终为白色。
<项目名称=homeAsUpIndicator> @可绘制/ abc_ic_ab_back_mtrl_am_alpha< /项目>
对于搜索视图文本...有没有简单的方法。通过其源挖后,我发现一种方式来获得的文本视图。我没有测试此所以请让我在评论中知道,如果这没有奏效。
搜索查看SV = ...; //获得搜索视图实例在onCreateOptionsMenu
//其中preFIX标识符机器人:如果你使用本地搜索查看
TextView的电视= sv.findViewById(getResources()则getIdentifier(ID / search_src_text,NULL,NULL));
tv.setTextColor(Color.GREEN);当然//并指定自己的颜色
编辑:
合适的造型为一个默认的动作appcompat-V7的动作条是这样的:
<! - 动作条VS工具栏。 - >
<样式名称=Widget.MyApp.ActionBar.Solid父=Widget.AppCompat.ActionBar.Solid>
<项目名称=背景> @色/ actionbar_color< /项目> < - !没有preFIX。 - >
<项目名称=标高> 4DP< /项目> < - !没有preFIX。 - >
<项目名称=titleTextStyle> ...< /项目> <! - VS风格外观。 - >
< /风格>
<样式名称=Theme.MyApp父=Theme.AppCompat>
<项目名称=actionBarStyle> @风格/ Widget.MyApp.ActionBar.Solid< /项目>
<项目名称=actionBarTheme> @风格/ ThemeOverlay.MyApp.ActionBar< /项目>
<项目名称=actionBarPopupTheme> @风格/ ThemeOverlay.AppCompat.Light< /项目>
< /风格>
I'm using the new v7 Toolbar and for the life of me can't figure out how to change the color of the title. I've set the @style of the Toolbar to a style declared in styles.xml and applied a titleTextStyle with a textColor. Am I missing something? I'm coding for Lollipop but testing currently on a Kitkat device.
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"/>
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"/>
values/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>
EDIT:
@PeterKnut reported this affects the color of overflow button, navigation drawer button and back button. It also changes text color of SearchView
.
Concerning the icon colors: The colorControlNormal
inherits from android:textColorPrimary
by default. If you apply this to the action bar's theme, you can customize the icon color.
<item name="colorControlNormal">#de000000</item>
<!-- tools:ignore="NewApi" to shut IDE up -->
<!-- on API 21 the overflow icon ignores colorControlNormal attr in favor of the native version -->
<item name="android:colorControlNormal" tools:ignore="NewApi">?colorControlNormal</item>
Since you use the search widget which for some reason uses different back arrow (not visually, technically) than the one included with appcompat-v7, you have to set it manually in the app's theme. Support library's drawables get tinted correctly. Otherwise it would be always white.
<item name="homeAsUpIndicator">@drawable/abc_ic_ab_back_mtrl_am_alpha</item>
As for the search view text...there's no easy way. After digging through its source I found a way to get to the text view. I haven't tested this so please let me know in the comments if this didn't work.
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
EDIT:
Appropriate styling for a default action appcompat-v7 action bar would look like this:
<!-- 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>
这篇关于你如何设置标题的颜色为新的工具栏?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!