android - ?attr/不在工具栏中设置正确的颜色

标签 android colors material-design

我正在使用来自 http://android-developers.blogspot.in/2014/10/appcompat-v21-material-design-for-pre.html 的相同代码对于我的项目。

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

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>

&样式.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 手机上试用了该应用程序,即使在 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如果有人想看一下整个代码。

最佳答案

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

<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"

关于android - ?attr/不在工具栏中设置正确的颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28992683/

相关文章:

java - phonegap 的 activity.java 错误

java - 设置行中每个表格单元格的颜色

numpy - 使用 Numpy 在 python 中移动 HSV 像素值

angular - 使用 Angular Material 实现 Notched Outline 主题时遇到问题

css - Angular Material md-tabs 和 md-grid-lists

android - 适用于 Android 的第 3 方平台 sdk 或库概述

java - 直接调用 onResume() 的替代方法

android - 海拔不适用于 android.support.design.widget.FloatingActionButton

android - 如何在 Android 的 View 寻呼机页面中使用滑动手势?

javascript - 一天中特定时间的颜色变化在 Js 中不起作用