使用属性修改绘制元素颜色 [英] Using attribute to modify a drawable element color
问题描述
我试图通过使用属性使自定义绘制对象
,但我有一个充气误差
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>
我的themes.xml
My themes.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>
我不undestand为什么,我的属性是一个引用或颜色,如果我逼在的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.
有人能帮忙吗?
我发现<一href="http://stackoverflow.com/questions/8271329/how-to-get-my-own-defined-attribute-value-in-my-style">this问题但它没有解决我的问题。
I've found this question but it's not resolve my problem.
推荐答案
好消息吧!启动棒棒糖(21),您可以使用属性来引用的东西喜欢的颜色如?ATTR / colorPrimary
。
Good news everyone! Starting Lollipop (21) you can use attributes to reference stuff like colors e.g. ?attr/colorPrimary
.
原来的答复:
据我知道你不能引用从可绘样式属性,我再也找不到像在Android平台的资源($ ANDROID_HOME /平台/ Android的<$ C C $>的用法 - * /数据/ 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.
下面是相关的bug报告本(STAR吧):
Here is a related bug report for this (star it):
HTTPS://$c$c.google。 COM / P /安卓/问题/详细信息?ID = 26251
您可以通过使用单独的XML可绘像在解释这个解决方法:
You can workaround this by using separate xml drawables like explained at:
http://stackoverflow.com/a/13471695/172670
这篇关于使用属性修改绘制元素颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!