材料设计向后兼容性android:当使用appcompat7时,colorAccent需要API级别21。

27

我在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 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>

我希望我的应用程序具备 Material Design 特性,并支持向后兼容。该如何解决?

3个回答

63

这是正确答案,为什么错误的被标记为正确的? - Shreyans
它的效果和下面的解决方案一样好,但是下面的那个可能是更好的解决方案! - Hack5

15
移除颜色名称前的 "android" 前缀,即可变为:

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

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


我正在使用Eclipse和Maven作为我的构建工具,并已经将appcompat-7库包含到我的项目中,并将其添加为引用库,所以我想知道为什么我仍然遇到这个问题。 - Mario Dennis
1
你不能在values/styles.xml中使用它们。相反,你应该将它们放在values-v21/styles.xml中。 - Kamil Lelonek
这个还能向前兼容之前的版本吗? - Mario Dennis
1
这是为之前的版本提供向后支持的唯一方法。 - Kamil Lelonek
这并不能解决我的问题,因为在v14+上没有显示出accent、primary和primary_dark颜色。 - Mario Dennis
请查看下面的答案。 - Shreyans

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接