【问题标题】:?attr/ not setting right color in Toolbar?attr/ 没有在工具栏中设置正确的颜色
【发布时间】: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


【解决方案1】:

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

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

【讨论】:

  • 谢谢!那解决了它。时间一到,您就会获得赏金。
  • 奇怪,the Android guide for making an App Bar 告诉我使用这个主题。现在你告诉我们应该使用另一个主题并且它有效吗?我很高兴有这个解决方案,但是 Android 文档可能会更好一些。
【解决方案2】:

这很奇怪...也许可以尝试将其用作您的 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>

【讨论】:

【解决方案3】:

您很可能遇到此行为,因为您在 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】:

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

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

它们是使用小部件ToolBar 所必需的。但是你已经使用NoActionBar作为你的AppTheme,那么不需要上面两项,但是你应该在你的ToolBar中设置app:themeAppTheme

【讨论】:

  • 但是你的 targetSDKVersion 是什么? 4.3?
  • 没有。它是最新的 5.0
  • 看来你不需要这两项,因为你已经使用了NoActionBar,那么如果你从ToolBar中删除app:theme或将它设置为AppTheme,它应该可以工作。跨度>
【解决方案5】:

所以我不知道这是什么巫术......但这是对我有用的解决方法

在您的 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 不继承这些属性...但是哦,好吧

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-12-31
    • 1970-01-01
    • 1970-01-01
    • 2018-10-25
    • 2021-12-24
    • 1970-01-01
    • 1970-01-01
    • 2015-02-22
    相关资源
    最近更新 更多