Android: Display custom dialog in center of the container

≡放荡痞女 提交于 2019-12-03 22:56:29

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.

Window window = customdialog.getWindow();
window.setLayout(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
window.setGravity(Gravity.CENTER);

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

parent="@android:style/Theme.Light"

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.

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);
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!