Resources returns a Drawable of the wrong type
I have an app with three ProgressBars
, each with its own custom progress Drawable
. These Drawables are fairly straightforward- each is a LayerDrawable
with a transparent background layer, a transparent secondary progress layer, and a solid color progress layer.
All three ProgressBars
are displayed in the same layout.
The issue I started running into is that one of these ProgressBars
is not displaying correctly- it simply does not display any progress. This only occurs on some devices (confirmed on a emulated Nexus One running 2.3.3 and a Galaxy SII running 4.1.2).
I put a breakpoint in onCreate
, and discovered that the first two ProgressBars
have their mProgressDrawable
property correctly set to a LayerDrawable
, but the third is set to a ColorDrawable
.
Sure enough, the following code returns two LayerDrawables
and one ColorDrawable
:
Drawable blueDrawable = getResources().getDrawable(R.drawable.progress_blue);
Drawable redDrawable = getResources().getDrawable(R.drawable.progress_red);
Drawable greenDrawable = getResources().getDrawable(R.drawable.progress_green);
No matter where I move the third ProgressBar
in the layout and in the code, or try to swap around the progressDrawable
attributes, the one referencing my third XML Drawable
displays no progress and gives me a ColorDrawable
.
Interestingly, I discovered that simply creating a new XML file in my drawable folder fixes the problem. This leads me to believe that there is an issue with the way that Android is packaging or loading my resources, but I am having trouble figuring out how to identify and correct the root problem.
I also cannot reproduce the problem in a new application.
How can I continue tracking down the source of this problem?
progressDrawable
XML:
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="@android:id/background">
<shape>
<solid android:color="@android:color/transparent" />
</shape>
</item>
<item android:id="@android:id/secondaryProgress">
<clip>
<shape>
<solid android:color="@android:color/transparent" />
</shape>
</clip>
</item>
<item android:id="@android:id/progress">
<clip>
<shape>
<solid android:color="@color/myapp_green" />
<!-- The other two drawbles only change this color -->
</shape>
</clip>
</item>
</layer-list>
colors.xml:
<resources>
<color name="myapp_red">#dd514c</color>
<color name="myapp_green">#5eb95e</color>
<color name="myapp_blue">#0e90d2</color>
</resources>
Update from moving the progressDrawable
attributes around
If you change the order, from:
Drawable blueDrawable = getResources().getDrawable(R.drawable.progress_blue);
Drawable redDrawable = getResources().getDrawable(R.drawable.progress_red);
Drawable greenDrawable = getResources().getDrawable(R.drawable.progress_green);
to for example:
Drawable redDrawable = getResources().getDrawable(R.drawable.progress_red);
Drawable greenDrawable = getResources().getDrawable(R.drawable.progress_green);
Drawable blueDrawable = getResources().getDrawable(R.drawable.progress_blue);
is than still the greenDrawable the problem or still the 3rd position (blueDrawable)?
If you don't even see the progress bar, maybe you have a light background stuck somewhere?
Try to set the style attribute to:
style="@android:style/Widget.ProgressBar.Inverse"
链接地址: http://www.djcxy.com/p/19286.html
下一篇: 资源返回错误类型的Drawable