How to make Android ProgressBar determinate in code? [duplicate]

匿名 (未验证) 提交于 2019-12-03 03:06:01

问题:

This question already has an answer here:

I'm trying to do add a progressbar in code and make it determinate:

progressBar = new ProgressBar(getActivity()); progressBar.setLayoutParams(layoutParams); parent.addView(progressBar, index); progressBar.setId(id.list_item_secondary); progressBar.setProgressDrawable(getResources().getDrawable(drawable.progress_horizontal)); progressBar.setIndeterminate(false); progressBar.setMax(100); 

After progressBar.setIndeterminate(false), isIndeterminate is still true and the progress keeps shows the indeterminate circle.

How can I make it determinate?

回答1:

From the ProgressBar source code here, the constructor you are calling is in line 237 which is calling the constructor in line 241 which in turn calls the constructor in line 245 with the style:

com.android.internal.R.attr.progressBarStyle 

This style has the attribute android:indeterminateOnly set to true by default so your calls to setIndeterminate are ignored. See the function description at line 433.

I haven't done this but I assume that if you call the constructor in line 245 like this:

progressBar = new ProgressBar(getActivity(), null, <Your Style ID>); 

passing as the third parameter a style definition with android:indeterminateOnly to false it should work. Based in the source code I assume that setIndeterminate is there only to enable it and not to disable it.

Hope this helps...



回答2:

It doesn't look like you are setting the ProgressBar's style attribute. From the docs for setIndeterminate():

If this progress bar's style only supports indeterminate mode (such as the circular progress bars), then this will be ignored.

You should manually set the style e.g. via style="@android:style/Widget.ProgressBar.Horizontal". Simply changing the Drawable isn't enough.



回答3:

Your default ProgressBar style has android:indeterminateOnly set to true. So you cant change ProgressBar's indeterminate state. source code.



回答4:

Try also using setProgress(), which takes an integer argument between zero and getMax().



标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!