【问题标题】:Material design backward compatibility android:colorAccent requires API level 21 when using appcompat7材料设计向后兼容 android:colorAccent 在使用 appcompat7 时需要 API 级别 21
【发布时间】:2015-01-12 20:45:15
【问题描述】:

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

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

【问题讨论】:

    标签: java android eclipse maven material-design


    【解决方案1】:

    这些属性适用于 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 库包含到我的项目中,并将其添加为引用库,这就是为什么我想知道为什么我还在有这个问题。
    • "你不能在 values/styles.xml 中使用它们。相反,你应该把它们放在 values-v21/styles.xml 中"
    • 这是否仍会为以前的版本提供向后支持?
    • 这是为以前的版本提供向后支持的唯一方法。
    • 这并不能解决我的问题,因为在 v14+ 上没有显示重音、主色和主暗色
    【解决方案2】:

    【讨论】:

    • 这是正确的答案,为什么不正确的答案被标记为正确?
    • 它和下面的解决方案一样有效,但下面的解决方案可能是更好的解决方案!
    【解决方案3】:

    去掉颜色名称前的android前缀。所以它会是:

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

    【讨论】:

      猜你喜欢
      • 2014-11-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-12-13
      • 1970-01-01
      • 1970-01-01
      • 2017-07-21
      相关资源
      最近更新 更多