找不到与给定名称attr"colorPrimary"匹配的资源

Bor*_*ris 13 android xamarin.android xamarin

我很难在Xamarin Studio中编译我的Android应用程序.出现的错误如下:

找不到与给定名称attr"colorPrimary"匹配的资源

哪个引用我的styles.xml:

<?xml version="1.0" encoding="UTF-8"?>
<resources>
    <!-- Base application theme. -->
    <style name="AppTheme" parent="@android:style/Theme.Material.Light.DarkActionBar">
        <item name="colorPrimary">@color/colorPrimary</item>
        <!--item name="colorPrimaryDark">@color/colorPrimaryDark</item-->
        <!--item name="colorAccent">@color/colorAccent</item-->
    </style>
</resources>
Run Code Online (Sandbox Code Playgroud)

在线发现的通常建议是将SDK版本设置为21或更高版本.但我已经尝试过了:

  • 在清单中将minSdkVersion设置为21
  • 在清单中将targetSdkVersion设置为21
  • 在项目设置中将taget框架设置为Android 5.0
  • 清理和重建项目

错误仍然存​​在:-(是否还需要其他设置才能使其工作?

K N*_*Lal 14

这对我的项目有用.

res/values-v21/styles.xml,

<?xml version="1.0" encoding="UTF-8"?>
<resources>
    <style name="AppTheme" parent="@android:style/Theme.Material.Light.DarkActionBar">
    <item name="android:colorPrimary">@color/primaryColor</item>
    </style>
</resources>
Run Code Online (Sandbox Code Playgroud)

res/values/styles.xml,

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
        <item name="colorPrimary">@color/primaryColor</item>
    </style>
</resources>
Run Code Online (Sandbox Code Playgroud)


Jon*_*las 7

您应该直接从Material Design文档(https://developer.android.com/training/material/theme.html#ColorPalette)遵循约定:

<resources>
  <!-- inherit from the material theme -->
  <style name="AppTheme" parent="android:Theme.Material">
    <!-- Main theme colors -->
    <!--   your app branding color for the app bar -->
    <item name="android:colorPrimary">@color/primary</item>
    <!--   darker variant for the status bar and contextual app bars -->
    <item name="android:colorPrimaryDark">@color/primary_dark</item>
    <!--   theme UI controls like checkboxes and text fields -->
    <item name="android:colorAccent">@color/accent</item>
  </style>
</resources>
Run Code Online (Sandbox Code Playgroud)

您在这里缺少的是项目的android:名称空间前缀colorPrimary.因此,它无法找到相应的属性,因为它未在范围中定义.

否则,您需要android:从主题中删除前缀

parent="@android:style/Theme.Material.Light.DarkActionBar"
Run Code Online (Sandbox Code Playgroud)