"未能ATTR / colorPrimary转换为可绘制&QUOT?; [英] "Failed to convert ?attr/colorPrimary into a drawable"

查看:370
本文介绍了"未能ATTR / colorPrimary转换为可绘制&QUOT?;的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我开始一个新项目,因此该项目是多还是少空。我只是增加了一个MainActivity,具有MainActivtyFragment。我还没有添加任何code在所有他们。

现在,我编辑styles.xml所以它看起来是这样的:

\r
\r

<资源>\r
    <! - Base应用程序的主题。 - >\r
    <样式名称=MyTheme的父=@安卓风格/ Theme.Material.Light.DarkActionBar>\r
        <项目名称=机器人:windowNoTitle>真< /项目>\r
        <! - 我们将使用工具栏所以没有必要显示动作条 - >\r
        <项目名称=机器人:windowActionBar>假LT; /项目>\r
        <! - 从http://www.google.com/design/spec/style/color.html#color-color-palette-->设置主题颜色;\r
        <! - colorPrimary用于默认的动作栏背景 - >\r
        <项目名称=机器人:colorPrimary>#2196F3< /项目>\r
        <! - colorPrimaryDark用于状态栏 - >\r
        <项目名称=机器人:colorPrimaryDark>#1976D2< /项目>\r
        &所述;! - colorAccent用作colorControlActivated默认值\r
             这是用于着色部件 - >\r
        <项目名称=机器人:colorAccent>#FF4081< /项目>\r
        <! - 你也可以设置colorControlNormal,colorControlActivated\r
             colorControlHighlight和colorSwitchThumbNormal。 - >\r
        <项目名称=toolbarStyle> @风格/ Widget.AppCompat.Toolbar< /项目>\r
    < /风格>\r
< /资源>

\r

\r
\r

所以,我延长了材料的主题,你可以看到。

然后我创建一个工具栏的XML文件(mytoolbar.xml),像这样的:

\r
\r

<?XML版本=1.0编码=UTF-8? >\r
< android.support.v7.widget.Toolbar的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android\r
    机器人:ID =@ + ID / toolbar1\r
    机器人:layout_width =match_parent\r
    机器人:layout_height =WRAP_CONTENT\r
    安卓了minHeight =?ATTR / actionBarSize\r
    机器人:背景= gt;中ATTR / colorPrimary?\r
\r
    < ImageView的\r
        机器人:layout_width =WRAP_CONTENT\r
        机器人:layout_height =WRAP_CONTENT\r
        机器人:SRC =@绘制/ abc_ic_voice_search_api_mtrl_alpha/>\r
< /android.support.v7.widget.Toolbar>

\r

\r
\r

和这里的问题是:

我不能找出原因。我已经做了同步的gradle,清洁,重建,重新启动IDE(Android Studio中1.2.1.1),没有什么工作。

任何想法?


解决方案

有一个错误的android [pre-棒棒糖] OS哪些犯规让您在使用绘制ATTR。这里是链接到错误:

HTTPS://$c$c.google。 COM / p /安卓/问题/细节?ID = 26251

Android的开发团队已经发布了修复,但它适用于Android的L以上。
为了解决此问题的,请参考以下解决方案:

<一个href=\"http://stackoverflow.com/questions/8041537/how-to-reference-style-attributes-from-a-drawable\">How以引用样式从绘制属性?

I am starting a new project, so the project is more or less "empty". I just added a MainActivity, with a MainActivtyFragment. I havent added any code at all to them.

Now, I edit the styles.xml so it looks like this:

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

So, I extend the Material theme as you can see.

I then create a toolbar xml file (mytoolbar.xml), like this:

<?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>

And here is the problem:

I cant figure out why. I have done gradle sync, clean, rebuild, restarted IDE (Android Studio 1.2.1.1), nothing works.

Any ideas?

解决方案

There is a bug in android [pre-lollipop] OS which doesnt allow you to use attr in drawable. Here is the link to bug:

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

Android dev team has released a fix but it works on android L and above. For workaround to this problem, refer to following solution:

How to reference style attributes from a drawable?

这篇关于&QUOT;未能ATTR / colorPrimary转换为可绘制&QUOT?;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆