我有一个自定义对话框(继承自android.app.Dialog
),没有标题或边框。在此对话框中,我添加了一个普通的ProgressBar
,并希望将其置于对话框中。对话框本身有一个背景图像,可以延伸到整个屏幕。除了进度条没有在对话框中居中之外,一切似乎都有效。它看起来像这样:
这是我在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);
我有什么遗漏或错误吗?
答案 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);