【问题标题】:"Failed to convert ?attr/colorPrimary into a drawable"“无法将 ?attr/colorPrimary 转换为可绘制对象”
【发布时间】:2015-07-20 09:26:47
【问题描述】:

我正在开始一个新项目,因此该项目或多或少是“空的”。我刚刚添加了一个带有 MainActivityFragment 的 MainActivity。我根本没有给它们添加任何代码。

现在,我编辑 styles.xml,使其看起来像这样:

<resources>
    <!-- Base application theme. -->
    <style name="MyTheme" parent="@android:style/Theme.Material.Light.DarkActionBar">
        <item name="android:windowNoTitle">true</item>
        <!--We will be using the toolbar so no need to show ActionBar-->
        <item name="android:windowActionBar">false</item>
        <!-- Set theme colors from http://www.google.com/design/spec/style/color.html#color-color-palette-->
        <!-- colorPrimary is used for the default action bar background -->
        <item name="android:colorPrimary">#2196F3</item>
        <!-- colorPrimaryDark is used for the status bar -->
        <item name="android:colorPrimaryDark">#1976D2</item>
        <!-- colorAccent is used as the default value for colorControlActivated
             which is used to tint widgets -->
        <item name="android:colorAccent">#FF4081</item>
        <!-- You can also set colorControlNormal, colorControlActivated
             colorControlHighlight and colorSwitchThumbNormal. -->
        <item name="toolbarStyle">@style/Widget.AppCompat.Toolbar</item>
    </style>
</resources>

所以,如您所见,我扩展了 Material 主题。

然后我创建一个工具栏 xml 文件 (mytoolbar.xml),如下所示:

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/toolbar1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:minHeight="?attr/actionBarSize"
    android:background="?attr/colorPrimary">

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/abc_ic_voice_search_api_mtrl_alpha"/>
</android.support.v7.widget.Toolbar>

这就是问题所在:

我不知道为什么。我已经完成了 gradle 同步、清理、重建、重新启动 IDE(Android Studio 1.2.1.1),没有任何效果。

有什么想法吗?

【问题讨论】:

    标签: android material-design


    【解决方案1】:

    android [pre-lollipop] 操作系统中存在一个错误,它不允许您在 drawable 中使用 attr。这是错误的链接:

    https://code.google.com/p/android/issues/detail?id=26251

    Android 开发团队已经发布了一个修复程序,但它适用于 android L 及更高版本。 有关此问题的解决方法,请参阅以下解决方案:

    How to reference style attributes from a drawable?

    【讨论】:

    • 嗯,这是在PC上,当我开发时,它不在Android操作系统中,所以我不完全确定你的意思=)编码时操作系统版本不相关,我可以选择在 manifest/gradle 文件中定位不同的 SDK 版本,对吧?这就是你的意思,目标SDK版本是问题吗?如果我将它设置为 22,它会工作吗?因为它不适合我。
    • 你也将安卓操作系统更新到了 api 22 吗? .. 还是行不通。试图改变 min api
    • 当然,设备的操作系统会有所不同。一台设备可能运行 4.1,另一台 5.1 以及介于两者之间的所有设备。这应该不会影响 Android Studio。
    • 应用程序没有崩溃。这是一个 android studio 问题。
    • 在@Ted 评论之后,我在 Android Studio 1.2.1.1 中也有这个并且使用 API 22,所以它还没有修复。
    【解决方案2】:

    为其添加 android 关键字。

    ?attr/colorPrimary"
    

    变成

    ?android:attr/colorPrimary
    

    在 android studio 3.0 中运行良好。

    【讨论】:

      【解决方案3】:

      Android OS版本-21以下有问题,可以使用

      android:background="?attr/colorPrimary"

      而不是

      app:background="?attr/colorPrimary"

      它会正常工作的。

      【讨论】: