Lollipop Progress Bar Tinting

前端 未结 5 1632
再見小時候
再見小時候 2021-02-05 02:49

I\'m on a Lollipop device (MotoG 2014), I read about progress bar tinting, but this is not working...I get the default progress bar color. What am I missing here?



        
5条回答
  •  爱一瞬间的悲伤
    2021-02-05 03:02

    The accepted solution wasn't working for me on pre-Lollipop, but I found this solution to fit all APIs and on top of that, it doesn't use any deprecated code:

    Java solution

    // fixes pre-Lollipop progressBar indeterminateDrawable tinting
    if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
    
        Drawable wrapDrawable = DrawableCompat.wrap(mProgressBar.getIndeterminateDrawable());
        DrawableCompat.setTint(wrapDrawable, ContextCompat.getColor(getContext(), android.R.color.holo_green_light));
        mProgressBar.setIndeterminateDrawable(DrawableCompat.unwrap(wrapDrawable));
    } else {
        mProgressBar.getIndeterminateDrawable().setColorFilter(ContextCompat.getColor(getContext(), android.R.color.holo_green_light), PorterDuff.Mode.SRC_IN);
    }
    

    Kotlin extension

    fun ProgressBar.setIndeterminateTintCompat(@ColorInt color: Int) {
        if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
            val wrapDrawable = DrawableCompat.wrap(indeterminateDrawable)
            DrawableCompat.setTint(wrapDrawable, color)
            indeterminateDrawable = DrawableCompat.unwrap(wrapDrawable)
        } else {
            indeterminateTintList = ColorStateList.valueOf(color)
        }
    }
    
    // usage
    
    val desiredColor = ContextCompat.getColor(context, R.color.myColor)
    myProgressBar.setIndeterminateTintCompat(desiredColor)
    

    Happy coding !

提交回复
热议问题