尝试在color.xml中更改颜色后出错

时间:2017-11-27 08:01:41

标签: android android-styles

我正在尝试学习Android,现在我正在尝试在我的android项目中改变颜色。这是我的工作,

我将color.xml更改为:

<resources>
    <color name="primaryColor">#d81b60</color>
    <color name="primaryLightColor">#ff5c8d</color>
    <color name="primaryDarkColor">#a00037</color>
    <color name="primaryTextColor">#ffffff</color>
</resources>

和我的style.xml:

<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
    <!-- Customize your theme here. -->
    <item name="primaryColor">@color/primaryColor</item>
    <item name="primaryLightColor">@color/primaryLightColor</item>
    <item name="primaryDarkColor">@color/primaryDarkColor</item>
    <item name="primaryTextColor">@color/primaryTextColor</item>
</style>

但是当我尝试跑步时,我得到了这个错误:

enter image description here

那么,我该如何解决呢?

我已经尝试过了:

  1. Android Studio does not find color style resource

  2. Android Studio can't find color reference within the same resource file

3 个答案:

答案 0 :(得分:0)

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

在样式中,这些标签用于指定colorPrimary重音和primaryDark

更具体地将样式xml更改为

<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
        <!-- Customize your theme here. -->
        <item name="colorPrimary">@color/primaryColor</item> 
        <item name="colorPrimaryDark">@color/primaryDarkColor</item>
        <item name="colorAccent">@color/primaryAcentColor</item>
    </style>

答案 1 :(得分:0)

你去..

您需要在值文件夹下创建attrs.xml

提及此文件中的所有参考颜色。

e.g。

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <attr name="primaryColor"></attr>
    <attr name="primaryLightColor"></attr>
    <attr name="primaryDarkColor"></attr>
    <attr name="primaryTextColor"></attr>
</resources>

现在您可以直接在style.xml中使用您的颜色

e.g。

<resources xmlns:android="http://schemas.android.com/tools">

    <!-- Base application theme. -->
    <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
        <!-- Customize your theme here. -->
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name="colorAccent">@color/colorAccent</item>
        <item name="primaryColor">@color/primaryColor</item>
        <item name="primaryLightColor">@color/primaryLightColor</item>
        <item name="primaryDarkColor">@color/primaryDarkColor</item>
        <item name="primaryTextColor">@color/primaryTextColor</item>
    </style>

</resources>

答案 2 :(得分:0)

您正试图在您的样式中定义不存在的属性:

<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
    <!-- Customize your theme here. -->
    <item name="primaryColor">@color/primaryColor</item>
    <item name="primaryLightColor">@color/primaryLightColor</item>
    <item name="primaryDarkColor">@color/primaryDarkColor</item>
    <item name="primaryTextColor">@color/primaryTextColor</item>
</style>

没有primaryColorprimaryLightColorprimaryDarkColorprimaryTextColor。正确的属性是colorPrimarycolorPrimaryDarkcolorPrimaryLighttextColorPrimary。此图像将显示属性(来自Using the material theme):

enter image description here

因此,您需要将其更改为以下内容:

<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
    <!-- Customize your theme here. -->
    <item name="colorPrimary">@color/primaryColor</item>
    <item name="colorPrimaryLight">@color/primaryLightColor</item>
    <item name="colorPrimaryDark">@color/primaryDarkColor</item>
    <item name="textColorPrimary">@color/primaryTextColor</item>
</style>