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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26947276/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 03:33:03  来源:igfitidea点击:

Material design backward compatibility android:colorAccent requires API level 21 when using appcompat7

javaandroideclipsemavenmaterial-design

提问by Mario Dennis

I have a maven android project in eclipse and even though I have configured my project to use the compatibility library its still give the following error in my styles.xml:

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

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

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>

I want my applications material design features to have backward compatibility support. How can I fix this?

我希望我的应用程序材料设计功能具有向后兼容性支持。我怎样才能解决这个问题?

回答by Kamil Lelonek

These properties are intended to work with Android 21, Material Themethus you cannot use them in values/styles.xml. Instead, you should put them in values-v21/styles.xml.

这些属性旨在与 Android 21、Material Theme 一起使用,因此您不能在values/styles.xml. 相反,您应该将它们放入values-v21/styles.xml.

If you want to implement Material Design in previous versions of android you should include compatibility library:

如果你想在以前的 android 版本中实现 Material Design,你应该包含兼容性库:

dependencies {
    compile "com.android.support:appcompat-v7:21.0.+"
}

and then you can use:

然后你可以使用:

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>

More info here: http://android-developers.blogspot.com/2014/10/appcompat-v21-material-design-for-pre.html

更多信息:http: //android-developers.blogspot.com/2014/10/appcompat-v21-material-design-for-pre.html

回答by Dr. Hasan Hashem

Remove the android prefix before the color name. So it will be:

删除颜色名称前的 android 前缀。所以它将是:

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