我正在开发我想将对话框显示为屏幕大小的应用程序。所以我使用了下面的代码。我通过这里得到了解决方案警报消息没有显示在警报对话框中?
AlertDialog.Builder builder = new AlertDialog.Builder(this);
TextView title = new TextView(this);
title.setText("DM2");
title.setBackgroundColor(Color.DKGRAY);
title.setPadding(10, 10, 10, 10);
title.setGravity(Gravity.CENTER);
title.setTextColor(Color.WHITE);
title.setTextSize(20);
TextView text = new TextView(this);
text.setText("Hello This text");
text.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
text.setTextSize(20);
text.setGravity(Gravity.CENTER);
//Creates a linearlayout layout and sets it with initial params
LinearLayout ll = new LinearLayout(this);
ll.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
ll.setGravity(Gravity.CENTER);
ll.addView(text);
builder.setCustomTitle(title);
builder.setPositiveButton(
"Ok", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.dismiss();
}
});
Dialog d = builder.setView(ll).create();
//Fills up the entire Screen
WindowManager.LayoutParams lp = new WindowManager.LayoutParams();
lp.copyFrom(d.getWindow().getAttributes());
lp.width = WindowManager.LayoutParams.FILL_PARENT;
lp.height = WindowManager.LayoutParams.FILL_PARENT;
d.show();
d.getWindow().setAttributes(lp);
但我希望对话框以居中对齐的方式出现。现在它显示在窗口顶部。我尝试使用 lp.garvity = Gravity.center,但没有奏效。如果设备方向发生变化,我需要再按一次确定按钮来关闭对话框。如何解决这个问题?
在此先感谢 Pushpa