Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
439 views
in Technique[技术] by (71.8m points)

java - How to set theme to ProgressDialog?

I would like to set theme of progressDialog. To create it, I use this code:

progressDialog = ProgressDialog.show(this, "Please Wait", "Loading dictionary file....", true, false);

I can't just write

progressDialog = new ProgressDialog(...);
progressDialog.(do_sth_with_dialog);
progressDialog.show(...)

because the show() method is static and I get compiler warning. Is there any way to use available constants like

progressDialog.THEME_HOLO_DARK 

to set the dialog theme?

I would also like to change the Dialog background and make the corners round (I don't want to change anything with the progressBar that is inside progressDialog. There is many tutorials here, but they usually describe how to create new class that extends progressDialog class.

Is there easier way to set THEME and BACKGROUND color of progressDialog?
Why I can access constants like progressDialog.THEME_HOLO_DARK if I cant use them?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

ProgressDialog.show() are static methods, so you don't get a class instance of ProgressDialog that you can set properties on.

To get a ProgressDialog instance:

// create a ProgressDialog instance, with a specified theme:    
ProgressDialog dialog = new ProgressDialog(mContext, ProgressDialog.THEME_HOLO_DARK);
// set indeterminate style
dialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
// set title and message
dialog.setTitle("Please wait");
dialog.setMessage("Loading dictionary file...");
// and show it
dialog.show();

EDIT 8/2016: Regarding the comments about deprecated themes, you may also use styles.xml and inherit from a base theme, e.g.:

<style name="MyProgressDialog" parent="Theme.AppCompat.Dialog">
</style>

the details on how to do this are already covered extensively elsewhere, start with https://developer.android.com/guide/topics/ui/themes.html.

Using themes and styles.xml is (in my opinion) a much cleaner and easier to maintain solution than hard-coding a theme when instantiating the ProgressDialog, i.e. set it once and forget it.

Then you can just do

new ProgressDialog(mContext);

and let your global theme/style provide the styling.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...