How to change progress bar's progress color in Android

前端 未结 30 1955
情话喂你
情话喂你 2020-11-22 07:49

I\'m using an horizontal progress bar in my Android application, and I want to change its progress color (which is Yellow by default). How can I do it using code

30条回答
  •  情歌与酒
    2020-11-22 08:11

    Hit the same problem while working on modifying the look/feel of the default progress bar. Here is some more info that will hopefully help people :)

    • The name of the xml file must only contain characters: a-z0-9_. (ie. no capitals!)
    • To reference your "drawable" it is R.drawable.filename
    • To override the default look, you use myProgressBar.setProgressDrawable(...), however you need can't just refer to your custom layout as R.drawable.filename, you need to retrieve it as a Drawable:
      Resources res = getResources();
      myProgressBar.setProgressDrawable(res.getDrawable(R.drawable.filename);
      
    • You need to set style before setting progress/secondary progress/max (setting it afterwards for me resulted in an 'empty' progress bar)

提交回复
热议问题