我正在学习使用新的 Material Design主题 设计Android应用。在设计自定义 ToolBar 时,我试图为自定义 ToolBar 中的标题提供自定义颜色。 在Android Lollipop设备上,它正如预期的那样正常工作,正如您在下面给出的图像链接中看到的那样,但是当我尝试在Lollipop前设备上运行时,我得不到相同的结果。为什么这样?我附上了以下代码。
在这里,我附上了Lollipop版本以及Lollipop前版本屏幕的截图。
代码:
activity_main.xml中
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<include
android:id="@+id/app_bar"
layout="@layout/app_bar" />
<TextView
android:layout_marginTop="10dp"
android:layout_below="@+id/app_bar"
android:text="@string/hello_world"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</RelativeLayout>
styles.xml
<resources>
<!-- Base application theme. -->
<style name="AppTheme" parent="AppTheme.Base">
<!-- Customize your theme here. -->
</style>
<style name="AppTheme.Base" parent="Theme.AppCompat.Light.NoActionBar">
<item name="colorPrimary">#455A64</item>
<item name="colorPrimaryDark">#607D8B</item>
<item name="colorAccent">#9E9E9E</item>
</style>
<style name="CustomToolbarTheme" parent="ThemeOverlay.AppCompat.Light">
<item name="android:textColorPrimary">#D32F2F</item>
<item name="android:textColorSecondary">#F44336</item>
</style>
</resources>
app_bar.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#1976D2"
android:theme="@style/CustomToolbarTheme"
android:popupTheme="@style/ThemeOverlay.AppCompat.Dark">
</android.support.v7.widget.Toolbar>
P.S。:如果此问题(或类似问题)已在此处得到解答,请在此处注释链接。谢谢!
答案 0 :(得分:1)
您正在使用
android:theme="@style/CustomToolbarTheme"
这是:
<style name="CustomToolbarTheme" parent="ThemeOverlay.AppCompat.Light">
<item name="android:textColorPrimary">#D32F2F</item>
<item name="android:textColorSecondary">#F44336</item>
</style>
它没有覆盖像AppTheme.Base那样的colorPrimary属性。将该行添加到此样式并确保将背景设置为它,如下所示:
<style name="CustomToolbarTheme" parent="ThemeOverlay.AppCompat.Light">
<item name="android:background">?attr/colorPrimary</item>
<item name="colorPrimary">#455A64</item>
<item name="android:textColorPrimary">#D32F2F</item>
<item name="android:textColorSecondary">#F44336</item>
</style>
答案 1 :(得分:1)
尝试为titleTextAppearance
主题设置Toolbar
,如下所示:
<style name="Toolbar" parent="Theme.AppCompat.Light">
<item name="titleTextAppearance">@style/Toolbar.TitleText</item>
</style>
<style name="Toolbar.TitleText">
<item name="android:textColor">@color/toolbar_title_text</item>
</style>
修改强> 对不起,我忘了补充一点,你应该再设一件事。
<android.support.v7.widget.Toolbar
...
xmlns:app="http://schemas.android.com/apk/res-auto"
app:theme="@style/Toolbar"
app:titleTextAppearance="?attr/titleTextAppearance"/>
(上述解决方案也可用于其他文字外观的属性,例如android:textStyle
或android:textSize
。)
答案 2 :(得分:1)
如果它不起作用,那么您还可以在运行时设置工具栏自定义标题颜色和工具栏自定义标题。代码段如下所示
private Toolbar toolbar;
toolbar = (Toolbar) findViewById(R.id.app_bar);
toolbar.setTitleTextColor(Color.parseColor("#FF557F"));
setSupportActionBar(toolbar);
getSupportActionBar().setHomeButtonEnabled(true);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setTitle("Title");
答案 3 :(得分:0)
我用这个:
<强> color.xml 强>
<resources>
<color name="white">#fff</color>
<color name="primary">#673ab7</color>
<color name="primary_dark">#512da8</color>
<color name="accent">#ffc400</color>
<color name="text_primary">#009688</color>
<color name="text_secondary">#e2eef0</color>
</resources>
<强>值/的themes.xml 强>
<resources>
<style name="AppTheme" parent="AppTheme.Base"/>
<style name="AppTheme.Base" parent="Theme.AppCompat">
<item name="colorPrimary">@color/primary</item>
<item name="colorPrimaryDark">@color/primary_dark</item>
<item name="colorAccent">@color/accent</item>
<item name="android:textColorPrimary">@color/text_primary</item>
<item name="android:textColor">@color/text_secondary</item>
<item name="android:windowNoTitle">true</item>
<item name="windowActionBar">false</item>
</style>
</resources>
<强>值-V21 /的themes.xml 强>
<resources>
<style name="AppTheme" parent="android:Theme.Material.Light">
<item name="android:colorPrimary">@color/primary</item>
<item name="android:colorPrimaryDark">@color/primary_dark</item>
<item name="android:colorAccent">@color/accent</item>
<item name="android:textColorPrimary">@color/text_primary</item>
<item name="android:textColor">@color/text_secondary</item>
<item name="android:navigationBarColor">@color/primary_dark</item>
</style>
</resources>
参见一些教程:
答案 4 :(得分:0)
你在主题android:textColorPrimary
上使用它,所以请使用此textColorPrimary
,因为第一个是在棒棒糖上使用v-21