更改对话框在屏幕 android 上的位置 [英] Changing position of the Dialog on screen android

查看:33
本文介绍了更改对话框在屏幕 android 上的位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我的 Activity 中做了一个简单的 AlertDialog:

I made a simple AlertDialog in my Activity:

View view = layoutInflater.inflate(R.layout.my_dialog, null);
AlertDialog infoDialog = new AlertDialog.Builder(MyActivity.this)
                    .setView(view)  
                    .create();

infoDialog.show();

使用上面的代码,对话框显示在(大约)屏幕中心.

With above code, the dialog shows at the (about) the center of the screen.

我想知道,如何自定义对话框位置以使其显示在顶部操作栏下方?(无论如何要改变对话框的重力或其他东西?)以及如何根据我的代码进行操作??

I am wondering, how can I customize the dialog position to make it showing just under the top Action Bar ? (Is there anyway to change the gravity or something of the dialog?) and how to do it based on my code??

推荐答案

我用这个代码在屏幕底部显示对话框:

I used this code to show the dialog at the bottom of the screen:

Dialog dlg = <code to create custom dialog>;

Window window = dlg.getWindow();
WindowManager.LayoutParams wlp = window.getAttributes();

wlp.gravity = Gravity.BOTTOM;
wlp.flags &= ~WindowManager.LayoutParams.FLAG_DIM_BEHIND;
window.setAttributes(wlp);

如果需要,此代码还可以防止 android 使对话框的背景变暗.你应该能够改变重力参数来移动对话框

This code also prevents android from dimming the background of the dialog, if you need it. You should be able to change the gravity parameter to move the dialog about

这篇关于更改对话框在屏幕 android 上的位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆