android - Re use gradient drawable with theme-dependent colors -
in 2 different activities want use same gradient drawable different colors. think refer gradient colors activity theme in follow way:
i've added follow rows in attrs.xml
<attr name="backgroundtopcolor" format="color" /> <attr name="backgroundbottomcolor" format="color" />
in bg_gradient.xml typed
<shape xmlns:android="http://schemas.android.com/apk/res/android"> <gradient android:startcolor="?backgroundtopcolor" android:endcolor="?backgroundbottomcolor" android:angle="270" /> <corners android:radius="0dp" /> </shape>
in activity theme, i've added
<item name="backgroundtopcolor">#ffffffff</item> <item name="backgroundbottomcolor">#ffffff00</item>
after application start in logcat
02-07 14:03:59.479: error/androidruntime(2096): caused by: java.lang.unsupportedoperationexception: can't convert color: type=0x2
02-07 14:03:59.479: error/androidruntime(2096): @ android.content.res.typedarray.getcolor(typedarray.java:326)
02-07 14:03:59.479: error/androidruntime(2096): @ android.graphics.drawable.gradientdrawable.inflate(gradientdrawable.java:647)
02-07 14:03:59.479: error/androidruntime(2096): @ android.graphics.drawable.drawable.createfromxmlinner(drawable.java:788)
02-07 14:03:59.479: error/androidruntime(2096): @ android.graphics.drawable.drawable.createfromxml(drawable.java:729)
02-07 14:03:59.479: error/androidruntime(2096): @ android.content.res.resources.loaddrawable(resources.java:1694)
02-07 14:03:59.479: error/androidruntime(2096): ... 29 more
how can solve this?
thank you
nb: api level = 8
you should use "?attr/backgroundtopcolor"
instead of "?backgroundtopcolor"
reference attributes.
edit: happens if indirectly reference color? instead of in theme saying:
<item name="backgroundtopcolor">#ffffffff</item>
you this:
<item name="backgroundtopcolor">@color/mycolor</item>
then in colors.xml, add:
<color name="mycolor">#ffffffff</color>
i suspect might happening attribute expecting explicit color. @ least, i've done outline, except indirectly reference color rather putting right theme.
Comments
Post a Comment