You cannot use reference color (by topic) in drawable

Consider:

styles.xml

<style name="BlueTheme" parent="@android:style/Theme.Black.NoTitleBar"> <item name="theme_color">@color/theme_color_blue</item> </style> 

attrs.xml

 <attr name="theme_color" format="reference" /> 

color.xml

 <color name="theme_color_blue">#ff0071d3</color> 

and finally drawable progress_bar.xml

 <?xml version="1.0" encoding="utf-8"?> <layer-list xmlns:android="http://schemas.android.com/apk/res/android" > <item android:id="@android:id/background"> <shape> <stroke android:width="2px" android:color="#ff515151" /> <solid android:color="@color/transparent" /> </shape> </item> <item android:id="@android:id/progress"> <clip> <shape> <solid android:color="?attr/theme_color" /> </shape> </clip> </item> </layer-list> 

when using the progress bar:

 <ProgressBar android:id="@+id/progressBarStartup" style="?android:attr/progressBarStyleHorizontal" android:layout_width="match_parent" android:layout_height="wrap_content" android:gravity="center_horizontal" android:progressDrawable="@drawable/progress_bar" android:visibility="visible" /> 

I get a runtime error . Android seems to be unable to get a color theme.

 06-24 16:35:57.032: E/AndroidRuntime(3493): java.lang.RuntimeException: Unable to start activity ComponentInfo{xxx.xxx.activities.startup.ActivityStartup}: android.view.InflateException: Binary XML file line #33: Error inflating class android.widget.ProgressBar 06-24 16:45:32.265: E/AndroidRuntime(9901): Caused by: android.content.res.Resources$NotFoundException: Resource is not a Drawable (color or path): TypedValue{t=0x2/d=0x7f010000 a=-1} 
+4
android android-resources android-theme
Jun 24 '13 at 14:41
source share
2 answers

Yes, this seems to be a known issue. But there is a workaround. Instead of using the ?attr colored links, as you described, create several drawings (as many as different colors you have), let everyone call its colors directly. At the topic level, referring to the drawings themselves depending on the topic. It works that way.

Update:

In Lollipop, you can draw drawing in XML. To cover pre-Lollipop, you can simply call this for all of your drawings in question (thanks to new versions of the appcompat library):

 public void setDrawableColor(Drawable d, int color) { if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) { d = DrawableCompat.wrap(d); DrawableCompat.setTint(d.mutate(), color); } } 
+3
Oct 13 '14 at 19:46
source share

You should use <attr name="theme_color" format="integer"/>

0
Mar 05 '18 at 21:04
source share



All Articles