简体   繁体   中英

Center a ProgressBar in a Custom Dialog

I have a custom dialog (inherited from android.app.Dialog ) with no title or border. To this dialog I add a normal ProgressBar , and would like to center it in the dialog. The dialog itself has a background image that stretches for the full screen. Everything seems to work except that the progress bar is not getting centered in the dialog. It looks like this:

ProgBar不在对话框中居中

This is the code I have used within the customDialog class, to add the progress bar to the dialog:

ProgressBar progBar = new ProgressBar(context);

LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
addContentView(progBar, layoutParams);

getWindow().setGravity(Gravity.CENTER);

Is there something I'm missing, or getting wrong?

Try this..

    ProgressBar progBar = new ProgressBar(context);

    RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(
            RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
    lp.addRule(RelativeLayout.CENTER_HORIZONTAL, progBar.getId());
    lp.addRule(RelativeLayout.CENTER_VERTICAL, progBar.getId());

    parentLayout.addView(progBar, lp);

Try this,

      LinearLayout.LayoutParams ll = (LinearLayout.LayoutParams)b.getLayoutParams();    
ll.gravity = Gravity.CENTER;
progBar.setLayoutParams(ll);

Try this, Set layout hight and width FILL_PARENT

LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT);
params.gravity = Gravity.CENTER;
progBar.setLayoutParams(params);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM