0

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

ProgBar 未在对话框中居中

这是我在 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);

有什么我遗漏或出错的吗?

4

3 回答 3

2

尝试这个,

      LinearLayout.LayoutParams ll = (LinearLayout.LayoutParams)b.getLayoutParams();    
ll.gravity = Gravity.CENTER;
progBar.setLayoutParams(ll);
于 2013-05-30T11:36:23.870 回答
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);
于 2013-05-30T11:35:41.643 回答
0

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

LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT);
params.gravity = Gravity.CENTER;
progBar.setLayoutParams(params);
于 2013-05-30T11:40:01.297 回答