Change position of progressbar in code

萝らか妹 提交于 2019-12-04 06:42:23

问题


I have a progress bar which I create in code my activity. This is code:

progressDialog = new ProgressDialog(this); 
// Progress bar message.
String progressBarMessage = "Loading, please wait...";
progressDialog.getWindow().setGravity(Gravity.BOTTOM);

progressDialog.setCancelable(false);
progressDialog.setMessage(progressBarMessage);
// set the progress to be horizontal
progressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
// reset the bar to the default value of 0
progressDialog.setProgress(0);
// convert the text value to a integer
int maximum = 100;
// set the maximum value
progressDialog.setMax(maximum);
// display the progressbar
progressDialog.show();  

As you can see position my progressbar is bottom. How I can change position this progressbar to 3/4 of my page? I want to move that position will be between center and bottom? Maybe can I setMarginTop, but how?


回答1:


Try something like this:

// ...
progressDialog.getWindow().setGravity(Gravity.CENTER);
WindowManager.LayoutParams wmlp = progressDialog.getWindow().getAttributes();
wmlp.y = height / 4;
progressDialog.getWindow().setAttributes(wmlp);
progressDialog.setCancelable(false);
// ...

Where height is an int value(the height of the screen) that you initialize in the onCreate method like this:

height = getResources().getDisplayMetrics().heightPixels;


来源:https://stackoverflow.com/questions/11289329/change-position-of-progressbar-in-code

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