可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
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()
.