How to dynamically change the color of progress bar background android

前端 未结 2 928
梦谈多话
梦谈多话 2021-02-02 02:05

I\'d like to dynamically change the background color of a progress bar in android. I followed the \"bonus\" part near the end of the page of this tutorial:

http://colint

2条回答
  •  情话喂你
    2021-02-02 02:25

    Well, for anyone looking for how to do it programmatically:

        Drawable bckgrndDr = new ColorDrawable(Color.RED);
        Drawable secProgressDr = new ColorDrawable(Color.GRAY);
        Drawable progressDr = new ScaleDrawable(new ColorDrawable(Color.BLUE), Gravity.LEFT, 1, -1);
        LayerDrawable resultDr = new LayerDrawable(new Drawable[] { bckgrndDr, secProgressDr, progressDr });
    
        resultDr.setId(0, android.R.id.background);
        resultDr.setId(1, android.R.id.secondaryProgress);
        resultDr.setId(2, android.R.id.progress);
    
        progressBar.setProgressDrawable(resultDr);
    

    Setting ids to drawables is crucial, and takes care of preserving bounds and actual state of progress bar

提交回复
热议问题