Android:在容器中心显示自定义对话框
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10412192/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Android: Display custom dialog in center of the container
提问by Kamal
How to display custom dialog as a center of the container?
如何将自定义对话框显示为容器的中心?
Dialog customdialog = new Dialog(this,android.R.style.Theme_Translucent_NoTitleBar);
Window window = customdialog.getWindow();
window.setLayout(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT);
window.setGravity(Gravity.CENTER);
R.style.Theme_Translucent_NoTitleBar
- is used to remove border for cutomdialog. Once i used this line the dialog will appear at the topleft corner of the tablet?
R.style.Theme_Translucent_NoTitleBar
- 用于删除 cutomdialog 的边框。一旦我使用了这条线,对话框将出现在平板电脑的左上角?
Can anyone help me to solve this issue?
谁能帮我解决这个问题?
回答by Sreedev R
Change the fill_parent to wrap_content.I hope this will be the issue the dialog appear at the corner of the activity.It takes the space of whole layout.So changing this may help you to get what you realy wanted.
把fill_parent改成wrap_content。希望这就是Activity角落出现对话框的问题。它占用了整个布局的空间。所以改变这个可能会帮助你得到你真正想要的。
Window window = customdialog.getWindow();
window.setLayout(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
window.setGravity(Gravity.CENTER);
Use above code after dialog.show() statement
在 dialog.show() 语句之后使用上面的代码
回答by Shabnam Sarup
I added this to the dialog's custom style and it worked fine.
我将此添加到对话框的自定义样式中,效果很好。
<item name="android:layout_gravity">center</item>
My dialog's width and height are set to wrap_content. The style's parent is
我的对话框的宽度和高度设置为 wrap_content。样式的父项是
parent="@android:style/Theme.Light"
parent="@android:style/Theme.Light"
回答by Juan Pedro Martinez
I will go for this piece of code:
我将使用这段代码:
WindowManager.LayoutParams lp = new WindowManager.LayoutParams();
Window window = dialog.getWindow();
lp.copyFrom(window.getAttributes());
//This makes the dialog take up the full width
lp.width = WindowManager.LayoutParams.MATCH_PARENT;
lp.height = WindowManager.LayoutParams.MATCH_PARENT;
window.setAttributes(lp);
Where dialog, is the dialog object to be shown. Inside of the layout of the dialog you can define the view of the layout as you wish: centered or not.
其中 dialog,是要显示的对话框对象。在对话框的布局内,您可以根据需要定义布局视图:居中或不居中。
回答by Maysam R
I use this code and the problem solved
我使用此代码并解决了问题
Window window = alertDialog.getWindow();
window.setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
window.setGravity(Gravity.CENTER);