前言
在设置下拉刷新进度条颜色
的时候 发现SwipeRefreshLayout
的setColorSchemeColors
一直没有效果
在寻找问题的过程中看到了源码 如下:
源码分析
/**
* @deprecated Use {@link #setColorSchemeResources(int...)}
*/
@Deprecated
public void setColorScheme(@ColorRes int... colors) {
setColorSchemeResources(colors);
}
/**
* Set the color resources used in the progress animation from color resources.
* The first color will also be the color of the bar that grows in response
* to a user swipe gesture.
*
* @param colorResIds
*/
public void setColorSchemeResources(@ColorRes int... colorResIds) {
final Context context = getContext();
int[] colorRes = new int[colorResIds.length];
for (int i = 0; i < colorResIds.length; i++) {
colorRes[i] = ContextCompat.getColor(context, colorResIds[i]);
}
setColorSchemeColors(colorRes);
}
/**
* Set the colors used in the progress animation. The first
* color will also be the color of the bar that grows in response to a user
* swipe gesture.
*
* @param colors
*/
public void setColorSchemeColors(@ColorInt int... colors) {
ensureTarget();
mProgress.setColorSchemeColors(colors);
}
从源码中 我们可以看到:
- 第一个方法
setColorScheme()
这个方法已经过期 不赞成使用! - 第二个方法
setColorSchemeResources(@ColorRes int... colorResIds)
需要传一个颜色资源,也就是我们常用的values资源文件夹里的color
文件
例如:
<resources>
<color name="colorPrimary">#FF18C35E</color>
<color name="colorPrimaryDark">#FF18C35E</color>
<color name="colorAccent">#FF18C35E</color>
<color name="colorDividingLine">#e1e1e1</color>
</resources>
- 第三个方法
setColorSchemeColors(@ColorInt int... colors)
需要传一个颜色值,也就是#FF18C35E
这种
正确使用姿势:
//初始化
SwipeRefreshLayout mSwipeRefreshLayout = findViewById(R.id.swiperefresh);
//在onCreate方法设置setColorSchemeResources()
mSwipeRefreshLayout.setColorSchemeResources(R.color.colorAccent,R.color.colorPrimaryDark);
//在onCreate方法设置setColorSchemeColors()
mSwipeRefreshLayout.setColorSchemeColors(Color.parseColor("#FF18C35E"),Color.parseColor("#e1e1e1"));
总结:如果使用setColorSchemeColors()
方法,参数传的是颜色值,用setColorSchemeResources()
参数传资源id。而setColorSchemeResources()
其实也是先通过资源id得到颜色值,最后调用setColorSchemeColors()
方法。
设置完成后的效果图:
与君共勉
我要一步一步往上爬
在最高点乘着叶片往前飞
任风吹干流过的泪和汗
我要一步一步往上爬
等待阳光静静看着它的脸
小小的天有大大的梦想
我有属于我的天
任风吹干流过的泪和汗
总有一天我有属于我的天