在安卓系统中改变对话框在屏幕上的位置

134

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

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

infoDialog.show();

通过上述代码,对话框会显示在屏幕的中心位置(大约)。

我想知道,如何自定义对话框的位置,使其显示在顶部操作栏正下方?(有没有什么方法可以更改对话框的重力或其他内容?)并且如何基于我的代码执行此操作?


1
如果您向我们展示您的my_dialog xml布局,我们可能可以帮助您进行更改。 - Alan Moore
duplicate?https://dev59.com/Z2035IYBdhLWcg3wW-v0 - Samir Mangroliya
@ Ankit,你能把你的评论放在答案里吗?因为我在查看了你的链接后解决了我的问题。 - Leem.fin
18个回答

288

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

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 让对话框的背景变暗,如果你需要的话。你可以更改重力参数来移动对话框。


1
你好,谢谢。我将它的重力设置为顶部,对话框出现在屏幕顶部,但也覆盖了我的操作栏,我希望对话框在顶部但是在操作栏下面...如何调整这个? - Leem.fin
7
你可以尝试使用 wlp.xwlp.y 字段来明确设置对话框在屏幕上的位置。我还没有尝试过,但应该可以正常工作。 - Aleks G
5
清除暗淡对我没有起作用(尽管其他部分都很好)。我在另一个地方找到了解决暗淡的方法,即:window.clearFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND); - Rubberduck
1
通过WindowManager源,只有当重力设置为TOP或BOTTOM时,wlp.y才起作用。 - Joao Polo
@KyleR 如果你看一下回答的日期,你会意识到它是在 API 22 之前写的(我相信当时我使用的是 API 11)。当你有浮动按钮(即全屏应用程序)时,所有应用程序 UI 都将在浮动导航按钮后面 - 总是这样。 - Aleks G
显示剩余3条评论

28
private void showPictureialog() {
    final Dialog dialog = new Dialog(this,
            android.R.style.Theme_Translucent_NoTitleBar);

    // Setting dialogview
    Window window = dialog.getWindow();
    window.setGravity(Gravity.CENTER);

    window.setLayout(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT);
    dialog.setTitle(null);
    dialog.setContentView(R.layout.selectpic_dialog);
    dialog.setCancelable(true);

    dialog.show();
}

根据您的需求,您可以根据重力和布局参数自定义对话框。

根据您的要求更改重力和布局参数。


1
嗨,谢谢。我将其重力设置为顶部,对话框出现在屏幕顶部,但它也覆盖了我的操作栏,我希望对话框在顶部,但仅在操作栏下面...如何调整此设置? - Leem.fin
3
你可以设置布局的顶部边距。 - Ramesh Solanki
"'FILL_PARENT'已被弃用。我使用的是API 21。根据开发者参考" - fWd82
1
@fWd82 所以请使用 'MATCH_PARENT'。 - William

28

我从@gypsicoder的代码这里找到了这段代码片段。

    private CharSequence[] items = {"Set as Ringtone", "Set as Alarm"};
    AlertDialog.Builder builder = new AlertDialog.Builder(this);
    builder.setItems(items, new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int item) {

            if(item == 0) {

            } else if(item == 1) {

            } else if(item == 2) {

            }
        }
    });
    
    AlertDialog dialog = builder.create();
    dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
    WindowManager.LayoutParams wmlp = dialog.getWindow().getAttributes();
    
    wmlp.gravity = Gravity.TOP | Gravity.LEFT;
    wmlp.x = 100;   //x position
    wmlp.y = 100;   //y position
    
    dialog.show();

Here x position's value is pixels from left to right. For y position value is from bottom to top.


在修改完LayoutParams后,您忘记设置它们了。 - Harsh Vardhan
1
@HarshVardhan 参数是在getAttributes()返回的对象内部修改的,该对象是窗口管理器实际使用的对象。 - user905686

16
在我尝试将对话框定位到文本视图的底部并被选中的位置时,这对我非常有效。
public void setPosition(int yValue) {
    Window window = getWindow();
    WindowManager.LayoutParams param = window.getAttributes();
    param.gravity = Gravity.TOP | Gravity.CENTER_HORIZONTAL;
    param.y = yValue;
    window.setAttributes(param);
    window.clearFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
}

是的,它可以使用Y位置,但当我设置X位置时,它对X位置没有影响。 - Shubham Mogarkar

15

新的 BottomSheetDialog

BottomSheetDialog dialog = new BottomSheetDialog(YourActivity.this);    
dialog.setContentView(YourView);

dialog.show();

9

只需将以下代码添加到您的代码中:

dialog.getWindow().setGravity(Gravity.BOTTOM);

3

在Java中

AlertDialog.Builder builder = new AlertDialog.Builder(ConnectActivity.this);

AlertDialog alertDialog = builder.create();  
// set the gravity      
alertDialog.getWindow().setGravity(Gravity.TOP);
// set the margin 
alertDialog.getWindow().getAttributes().verticalMargin = 0.1F;
alertDialog.show();

3

在 Kotlin 中设置 Dialog 重力的最简单方法:

dialog.window?.setGravity(Gravity.TOP) // Set the gravity here
private fun searchDialog() {
    val dialog = Dialog(this)
    dialog.window?.setGravity(Gravity.TOP) // Set the gravity here
    val binding = DialogSearchHomeBinding.inflate(layoutInflater)
    dialog.setContentView(binding.root)
    dialog.show()
}

3

Kotlin:

val dialog = Dialog(context)
//set graviy bottom
dialog.window.setGravity(Gravity.BOTTOM)

3
您可以使用此片段将AlertDialog放置在屏幕底部。
AlertDialog dialogPopup;

dialogPopup = mBuilder.create();
dialogPopup.getWindow().getAttributes().gravity = Gravity.BOTTOM;

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接