?attr /未在工具栏中设置正确的颜色

时间:2015-03-11 16:53:59

标签: android colors material-design

我使用http://android-developers.blogspot.in/2014/10/appcompat-v21-material-design-for-pre.html中的相同代码作为我的项目。

当我创建一个仅测试工具栏的空白项目时,颜色正常工作。但是,在使用相同的代码将项目升级到材料设计后,工具栏颜色变为灰色。

似乎android:background?attr/colorPrimary没有加载正确的颜色。当我使用@color\theme_red时,颜色在工具栏上正确设置。

这里出了什么问题?

我的colors.xml:

<?xml version="1.0" encoding="utf-8"?><resources>
    <color name="theme_red">#d43d1e</color>
    <color name="theme_red_dark">#aa3118</color>
    <color name="theme_accent">#3333ff</color>
</resources>

&安培; styles.xml:

<resources>

<!-- Base application theme. -->
<style name="AppTheme" parent="AppTheme.Base"/>

<style name="AppTheme.Base" parent="Theme.AppCompat.Light.NoActionBar">
    <!-- Customize your theme here. -->
    <item name="colorPrimary">@color/theme_red</item>
    <item name="colorPrimaryDark">@color/theme_red_dark</item>
    <item name="colorAccent">@color/theme_accent</item>
</style>

&安培;工具栏代码:

<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:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
app:popupTheme="@style/ThemeOverlay.AppCompat.Light"    />

android:background?attr/colorPrimary工具栏的外观如何: How the toolbar looks like 工具栏的显示方式与@color/theme_red

相似

How it should be

更新: 我正在更新问题,因为我在代码中有另一个似乎有关的错误。我在Android 5.0.2手机上试过这个应用程序&amp;即使正确的主题和状态,状态栏也没有暗色调。在styles.xml中定义的颜色。状态栏与第一张图片中的Toolbar颜色完全相同。

styles.xml在上面。 v-21 / styles.xml如下:

 <resources>
  <style name="AppTheme" parent="AppTheme.Base">
  <item name="colorPrimary">@color/theme_red</item>
  <item name="colorPrimaryDark">@color/theme_red_dark</item>
  <item name="colorAccent">@color/theme_accent</item>
  <item name="android:windowContentTransitions">true</item>
  <item name="android:windowAllowEnterTransitionOverlap">true</item>
  <item name="android:windowAllowReturnTransitionOverlap">true</item>
  <item name="android:windowSharedElementEnterTransition">@android:transition/move</item>
  <item name="android:windowSharedElementExitTransition">@android:transition/move</item>
  </style>
</resources>

不知何故,即使正确定义,系统也无法使用这些颜色。

完整项目位于:https://github.com/pauldmps/BPUTApp-AndroidStudio,如果有人想查看整个代码。

5 个答案:

答案 0 :(得分:8)

您在清单中指定了错误的主题:

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/Theme.AppCompat.Light.NoActionBar">

您应该使用android:theme="@style/AppTheme"

答案 1 :(得分:0)

这很奇怪......也许尝试将其用作styles.xml

<resources>
    <!-- Base application theme. -->
    <style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
        <!-- Customize your theme here. -->
        <item name="colorPrimary">@color/theme_red</item>
        <item name="colorPrimaryDark">@color/theme_red_dark</item>
        <item name="colorAccent">@color/theme_accent</item>
    </style>
</resources>

答案 2 :(得分:0)

如果您想将DarKActionBar用作AppTheme,则必须通过添加AppTheme来禁用默认操作栏:

<item name="windowActionBar">false</item>
<item name="android:windowNoTitle">true</item>

使用小部件ToolBar需要它们。不过,您已NoActionBar使用AppTheme,但不需要超过两项,但您应在app:theme中将AppTheme设为ToolBar

答案 3 :(得分:0)

您很可能会遇到此问题,因为您正在XML文件中将Toolbar的主题设置为ThemeOverlay.AppCompat.Dark.ActionBar。

尝试更改

app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"

app:theme="@style/AppTheme"

您可能还想专门为Toolbar指定主题。

<!-- Exclusive theme for the Toolbar -->
<style name="Theme.Toolbar" parent="Theme.AppCompat">
    <item name="colorPrimary">@color/yourColor</item>
    <item name="android:textColorPrimary">@color/yourTextColor</item>
</style>

答案 4 :(得分:0)

所以我不知道它是什么样的巫术......但这里有适合我的修复

在您的values-v21 / styles.xml中,添加以下行。

    <item name="colorPrimary">@color/theme_red</item>
    <item name="colorPrimaryDark">@color/theme_red_dark</item>
    <item name="colorAccent">@color/theme_accent</item>

我不知道为什么样式21不会继承这些属性......但是很好

相关问题