SwipeRefreshLayout的setColorSchemeColors没效果的原因

版权声明:> 本文为吕氏春秋i的原创文章,未经博主允许不得转载。 https://blog.csdn.net/Life_s/article/details/81940053
前言

设置下拉刷新进度条颜色的时候 发现SwipeRefreshLayoutsetColorSchemeColors一直没有效果
在寻找问题的过程中看到了源码 如下:

源码分析
 /**
     * @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()方法。

设置完成后的效果图:

这里写图片描述

与君共勉

我要一步一步往上爬
在最高点乘着叶片往前飞
任风吹干流过的泪和汗
我要一步一步往上爬
等待阳光静静看着它的脸
小小的天有大大的梦想
我有属于我的天
任风吹干流过的泪和汗
总有一天我有属于我的天
这里写图片描述

猜你喜欢

转载自blog.csdn.net/Life_s/article/details/81940053
今日推荐