Change progressbar color through CODE ONLY in Android

前端 未结 12 1818
北海茫月
北海茫月 2020-12-01 01:12

I have a progressBar using the ProgressBar class.

Just doing this:

progressBar = new ProgressBar(this, null, android.R.attr.progressBarStyleHorizont         


        
12条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2020-12-01 01:37

    In the case that you need to tint the background and the progress bar in different colors.

    progress_drawable.xml

    
    
        
            
                
            
        
        
            
                
                    
                
            
        
    
    

    Its possible, programmatically, to decompound its layer-list items, and tint them separately:

    LayerDrawable progressBarDrawable = (LayerDrawable) progressBar.getProgressDrawable();
    Drawable backgroundDrawable = progressBarDrawable.getDrawable(0);
    Drawable progressDrawable = progressBarDrawable.getDrawable(1);
    
    backgroundDrawable.setColorFilter(ContextCompat.getColor(this.getContext(), R.color.white), PorterDuff.Mode.SRC_IN);
    progressDrawable.setColorFilter(ContextCompat.getColor(this.getContext(), R.color.red), PorterDuff.Mode.SRC_IN);
    

提交回复
热议问题