SwipeRefreshLayout setProgressBackgroundColor()给出了奇怪的异常

时间:2015-01-04 14:09:17

标签: android swiperefreshlayout

当我尝试通过setProgressbackground()方法更改SwipeRefresh-Progressbar的颜色时,我得到错误:

 E/AndroidRuntime﹕ FATAL EXCEPTION: main
 android.content.res.Resources$NotFoundException: Resource ID #0xffffffff
        at android.content.res.Resources.getValue(Resources.java:1026)
        at android.content.res.Resources.getColor(Resources.java:756)
        at android.support.v4.widget.CircleImageView.setBackgroundColor(CircleImageView.java:118)
        at android.support.v4.widget.SwipeRefreshLayout.setProgressBackgroundColor(SwipeRefreshLayout.java:454)

我调用这样的方法.setProgressBackgroundColor(getResources()。getColor(R.color.mycolor))。颜色存在于资源文件中,并且在其他代码中运行良好。

然后我查看了SwipeRefreshLayout文件,发现方法是这样的:

  /**
 * Set the background color of the progress spinner disc.
 *
 * @param colorRes Resource id of the color.
 */
public void setProgressBackgroundColor(int colorRes) {
    mCircleView.setBackgroundColor(colorRes);
    mProgress.setBackgroundColor(getResources().getColor(colorRes));
}

对我来说,它似乎很奇怪,它只使用colorRes整数调用.setBackgroundColor()一次,使用getResources()。getColor(colorRes)调用另一次。

我做错了什么?

1 个答案:

答案 0 :(得分:0)

setBackgroundColor的参数采用十六进制形式的颜色,而不是资源ID

/res/colors.xml文件加载颜色,可以这样调用:

setBackgroundColor(getResources().getColor(R.color.red));

/res/values/colors.xml包含以下内容:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <color name="red">#FF0000</color>
</resources>