在屏幕的任何位置显示 AlertDialog [英] Show AlertDialog in any position of the screen

查看:45
本文介绍了在屏幕的任何位置显示 AlertDialog的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我们在 android 中显示 AlertDialog 时,它会显示在屏幕中央.有什么办法可以改变位置吗?

When we show an AlertDialog in android it shows in the center of the screen. Is there any way to change the position?

推荐答案

在各种帖子中搜索后,我找到了解决方案.

After searching in various post I have found the solution.

代码如下:

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();

这里 x 位置的值是从左到右的像素.对于 y 位置值是从下到上.

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

这篇关于在屏幕的任何位置显示 AlertDialog的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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