在自定义对话框中居中一个ProgressBar

时间:2013-05-30 11:25:39

标签: android android-layout android-progressbar

我有一个自定义对话框(继承自android.app.Dialog),没有标题或边框。在此对话框中,我添加了一个普通的ProgressBar,并希望将其置于对话框中。对话框本身有一个背景图像,可以延伸到整个屏幕。除了进度条没有在对话框中居中之外,一切似乎都有效。它看起来像这样:

ProgBar Not Centered in Dialog

这是我在customDialog类中使用的代码,用于将进度条添加到对话框中:

ProgressBar progBar = new ProgressBar(context);

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

getWindow().setGravity(Gravity.CENTER);

我有什么遗漏或错误吗?

3 个答案:

答案 0 :(得分:2)

试试这个..

    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);

答案 1 :(得分:2)

试试这个,

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

答案 2 :(得分:0)

试试这个, 设置布局高度和宽度FILL_PARENT

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