如何使定制对话框中心在android系统? [英] How to align custom dialog centre in android ?
问题描述
我工作的地方我想显示对话框是屏幕尺寸的应用。所以我用code below.I通过了这里的解决方案<一个href=\"http://stackoverflow.com/questions/10432184/alert-message-is-not-displaying-in-alert-dialog-box\">Alert消息没有在警告对话框中显示?
I am working on the application where i wanted to display the dialog to be screen size. So i used code below.I got the solution through here Alert message is not displaying in alert dialog box?
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,但没有奏效。
而如果设备方向的变化,我需要preSS确定按钮更多的时间来关闭对话框。如何解决这一问题?
But I want dialog to be appeared in center aligned. Now it is display in top of the window. I tried with lp.garvity = Gravity.center, but didn't work. and If the device orientation changes, i need to press ok button more time to close the dialog. How to fix this?
在此先感谢
普什帕
Thanks in advance Pushpa
推荐答案
我来晚了一点,但更好的后期从来就不是:-)
您可以使用下面的code:(我也看到这里)
I'm little late, but better late then never :-) you can use the code below: (I saw also here)
dialog = new Dialog(activity, android.R.style.Theme_Translucent_NoTitleBar);
dialog.setContentView(R.layout.activity_upload_photo_dialog);
Window window = dialog.getWindow();
window.setLayout(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
window.setGravity(Gravity.CENTER);
//The below code is EXTRA - to dim the parent view by 70%
LayoutParams lp = window.getAttributes();
lp.dimAmount = 0.7f;
lp.flags = LayoutParams.FLAG_DIM_BEHIND;
dialog.getWindow().setAttributes(lp);
//Show the dialog
dialog.show();
希望我帮助...
Hope I helped...
这篇关于如何使定制对话框中心在android系统?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!