【发布时间】:2015-05-13 14:24:51
【问题描述】:
我在我的项目中使用来自 http://android-developers.blogspot.in/2014/10/appcompat-v21-material-design-for-pre.html 的相同代码。
当我创建一个空白项目来仅测试工具栏时,颜色正常工作。但是,在使用相同的代码将我的项目升级到材料设计后,工具栏的颜色变为灰色。
似乎android:background?attr/colorPrimary 没有加载正确的颜色。当我使用@color\theme_red 时,工具栏上的颜色设置正确。
这里出了什么问题?
我的颜色.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 时工具栏的外观:
使用@color/theme_red 时工具栏的外观:
更新:
我正在更新这个问题,因为现在我的代码中有另一个似乎相关的错误。我在 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 获取完整项目。
【问题讨论】:
-
去掉
app:theme还能用吗?如果是这样,您可能需要创建一个继承自ThemeOverlay.AppCompat.Dark.ActionBar的样式并在那里设置colorPrimary。 -
没有。删除 app:theme 会使工具栏变成纯灰色,上面带有黑色文本。有点像工具栏的默认颜色。
-
在这里遇到同样的情况...尚未找到解决方案。有没有机会,你清理了项目?还是你唯一做的就是创建这两种样式?
-
我没有清理项目。我刚刚删除了 ActionBar 及其相关代码并放置了工具栏。
标签: android colors material-design