我在eclipse中有一个maven android项目,即使我已经配置我的项目使用兼容性库,它仍然在我的styles.xml中给出以下错误:
android:colorAccent requires API level 21 (current min is 15)
android:colorPrimary requires API level 21 (current min is 15)
android:colorPrimaryDark requires API level 21 (current min is 15)
style.xml
<style name="AppBaseTheme" parent="Theme.AppCompat"></style>
<style name="AppTheme" parent="AppBaseTheme">
<item name="android:colorPrimary">@color/primary</item>
<item name="android:colorPrimaryDark">@color/primary_dark</item>
<item name="android:colorAccent">@color/accent</item>
</style>
的AndroidManifest.xml
package="com.app"
android:versionCode="1"
android:versionName="0.0.1-SNAPSHOT" >
<uses-sdk
android:minSdkVersion="15"
android:targetSdkVersion="21" />
<uses-permission android:name="android.permission.INTERNET"/>
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme">
<activity android:name="com.app.activity.MainActivity" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
的pom.xml
<build>
<finalName>${project.artifactId}</finalName>
<pluginManagement>
<plugins>
<plugin>
<groupId>com.jayway.maven.plugins.android.generation2</groupId>
<artifactId>android-maven-plugin</artifactId>
<version>${android.plugin.version}</version>
<extensions>true</extensions>
</plugin>
</plugins>
</pluginManagement>
<plugins>
<plugin>
<groupId>com.jayway.maven.plugins.android.generation2</groupId>
<artifactId>android-maven-plugin</artifactId>
<configuration>
<sdk>
<platform>21</platform>
</sdk>
</configuration>
</plugin>
</plugins>
</build>
我希望我的应用程序材料设计功能具有向后兼容性支持。我怎样才能解决这个问题?
答案 0 :(得分:60)
答案 1 :(得分:14)
这些属性适用于Android 21 Material Theme,因此您无法在values/styles.xml
中使用它们。相反,您应该将它们放在values-v21/styles.xml
。
如果你想在以前版本的android中实现Material Design,你应该包括兼容性库:
dependencies {
compile "com.android.support:appcompat-v7:21.0.+"
}
然后你可以使用:
values/themes.xml:
<style name="Theme.MyTheme" parent="Theme.AppCompat.Light">
<!-- colorPrimary is used for the default action bar background -->
<item name=”colorPrimary”>@color/my_awesome_color</item>
<!-- colorPrimaryDark is used for the status bar -->
<item name=”colorPrimaryDark”>@color/my_awesome_darker_color</item>
<!-- colorAccent is used as the default value for colorControlActivated,
which is used to tint widgets -->
<item name=”colorAccent”>@color/accent</item>
<!-- You can also set colorControlNormal, colorControlActivated
colorControlHighlight, and colorSwitchThumbNormal. -->
</style>
此处有更多信息:http://android-developers.blogspot.com/2014/10/appcompat-v21-material-design-for-pre.html
答案 2 :(得分:13)
在颜色名称前删除android前缀。所以它将是:
<item name="colorPrimary">@color/primary</item>
<item name="colorPrimaryDark">@color/primary_dark</item>
<item name="colorAccent">@color/accent</item>