使用属性修改可绘制元素颜色 [英] Using attribute to modify a drawable element color

查看:28
本文介绍了使用属性修改可绘制元素颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试使用属性制作自定义Drawable,但出现膨胀错误

I try to make a custom Drawable by using attribute but I have an inflating error

01-21 11:01:06.171: E/AndroidRuntime(13695): Caused by: android.content.res.Resources$NotFoundException: 
File res/drawable/action_bar_background.xml from drawable resource ID #0x7f020000

我的 action_bar_background.xml

My action_bar_background.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" 
 android:shape="rectangle" >
    <gradient
        android:angle="90"
        android:endColor="?attr/ActionBarEndColor"
        android:startColor="?attr/ActionBarStartColor" 
    />
</shape>

我的 attr.xml

My attr.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
     <attr name="ActionBarStartColor" format="reference|color" />
     <attr name="ActionBarEndColor" format="reference|color" />
</resources>

我的主题.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <style name="Theme.Theme1" parent="@style/Theme.GreenDroid.Light.NoTitleBar">
        <item name="ActionBarStartColor">@color/greyDark</item>
        <item name="ActionBarEndColor">@color/greyLight</item>
    </style>

    <style name="Theme.Theme2" parent="@style/Theme.GreenDroid.Light.NoTitleBar">
        <item name="ActionBarStartColor">@color/myRed</item>
        <item name="ActionBarEndColor">@color/myWhite</item>
    </style>
</resources>

我不明白为什么,我的属性是引用或颜色,如果我在 themes.xml 中强制"颜色,它也不起作用.

I don't undestand why, my attribute is a reference or a color, if I "force" the color in themes.xml, it don't work too.

有人可以帮忙吗?

我发现了这个问题 但它不能解决我的问题.

I've found this question but it's not resolve my problem.

推荐答案

大家好消息!从 Lollipop (21) 开始,您可以使用属性来引用颜色之类的东西,例如?attr/colorPrimary.

Good news everyone! Starting Lollipop (21) you can use attributes to reference stuff like colors e.g. ?attr/colorPrimary.

原答案:

据我所知你不能从 drawable 中引用样式属性,我在 Android 平台资源中找不到这样的用法 ($ANDROID_HOME/platforms/android-*/data/res/),我所看到的是,即使是渐变可绘制对象,每个可绘制对象都是在适当位置定义的,或者它们只是引用其他可绘制对象,例如颜色可绘制对象.

As far I know you cannot reference style attributes from drawables, I couldn't find an usage like that in Android platform resources ($ANDROID_HOME/platforms/android-*/data/res/), all I see is that every drawable even for gradient drawables are defined in place or they just reference other drawables like color drawables.

这是一个相关的错误报告(星标):

Here is a related bug report for this (star it):

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

您可以通过使用单独的 xml drawable 来解决此问题,如在以下位置解释的:

You can workaround this by using separate xml drawables like explained at:

https://stackoverflow.com/a/13471695/172670

这篇关于使用属性修改可绘制元素颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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