Switching Color of a Progressbar programmatically

前端 未结 1 661
忘了有多久
忘了有多久 2021-02-06 13:50

so i have a Layer-List with an Item with a shape and a solid color. Now i want to change this color inside my code.



        
1条回答
  •  抹茶落季
    2021-02-06 13:55

    I know this question is a few months old but I was able to get a level-list working with a progress bar. The trick is to assign your levels values between 0 and 10,000 In my particular example I had 3 levels (red/yellow/green) used to represent battery life.

    Here is how I did it:

    res/drawable/progress_horizontal_yellow.xml

    
    
    
    
        
            
                
                
            
        
    
        
            
                
                    
                    
                
            
        
    
        
            
                
                    
                    
                
            
        
    
    

    res/drawable/progress_horizontal_red.xml

    
    
    
        
            
                
                
            
        
    
        
            
                
                    
                    
                
            
        
    
        
            
                
                    
                    
                
            
        
    
    
    

    res/drawable/progress_horizontal_green.xml

    
    
    
        
            
                
                
            
        
    
        
            
                
                    
                    
                
            
        
    
        
            
                
                    
                    
                
            
        
    
    

    res/drawable/battery_charge_fill.xml

    
    
        
        
        
    
    

    Then in the layout file my progress bar declaration was like this:

    
    

    Then in java:

    //progress is between 0 and 100 so set level of drawable to progress * 100
    Drawable batteryProgressD = batteryProgressBar.getProgressDrawable();
    batteryProgressD.setLevel(progress*100);
    batteryProgressBar.setProgress(progress);
    

    0 讨论(0)
提交回复
热议问题