Resources返回错误类型的Drawable

Tan*_*.7x 7 android android-drawable

我有一个有三个的应用程序ProgressBars,每个都有自己的自定义进度Drawable.这些Drawable非常简单 - 每个都是LayerDrawable透明背景图层,透明辅助进度图层和纯色进度图层.

所有三个ProgressBars都以相同的布局显示.

我开始遇到的问题是其中一个ProgressBars没有正确显示 - 它根本不显示任何进展.这仅在某些设备上发生(在运行2.3.3的模拟Nexus One和运行4.1.2的Galaxy SII上确认).

我把一个断点onCreate,并发现前两个ProgressBars有他们的mProgressDrawable属性设置正确的LayerDrawable,但第三个被设置为ColorDrawable.

果然,以下代码返回两个LayerDrawables和一个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);
Run Code Online (Sandbox Code Playgroud)

无论我ProgressBar在布局和代码中移动第三个,或者尝试交换progressDrawable属性,引用我的第三个XML的那个Drawable显示没有进展并给我一个ColorDrawable.

有趣的是,我发现只需在我的drawable文件夹中创建一个新的XML文件就可以解决问题.这让我相信Android打包或加载我的资源的方式存在问题,但我无法弄清楚如何识别和纠正根本问题.

我也无法在新的应用程序中重现该问题.

如何继续追踪此问题的根源?

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>
Run Code Online (Sandbox Code Playgroud)

colors.xml:

<resources>
    <color name="myapp_red">#dd514c</color>
    <color name="myapp_green">#5eb95e</color>
    <color name="myapp_blue">#0e90d2</color>
</resources>
Run Code Online (Sandbox Code Playgroud)

通过移动progressDrawable属性更新

  • 红色,蓝色,绿色:第3个坏了
  • 红色,绿色,绿色:第二和第三个被打破
  • 蓝色,绿色,红色:第二个坏了
  • 蓝色,红色,蓝色:一切正常
  • 绿色,绿色,绿色:一切正常
  • 绿色,绿色,红色:一切正常
  • 绿色,蓝色,红色:一切正常

luk*_*kas 3

如果您更改顺序,则从:

Drawable blueDrawable  = getResources().getDrawable(R.drawable.progress_blue);
Drawable redDrawable   = getResources().getDrawable(R.drawable.progress_red);
Drawable greenDrawable = getResources().getDrawable(R.drawable.progress_green);
Run Code Online (Sandbox Code Playgroud)

例如:

Drawable redDrawable   = getResources().getDrawable(R.drawable.progress_red);
Drawable greenDrawable = getResources().getDrawable(R.drawable.progress_green);
Drawable blueDrawable  = getResources().getDrawable(R.drawable.progress_blue);
Run Code Online (Sandbox Code Playgroud)

问题仍然是 greenDrawable 还是第三个位置(blueDrawable)?