4

我想在状态栏后面设置AlertDialog的位置,当我的Dialog中的内容会增加时,怎么做?我正在使用我自己的布局创建一个自定义 AlertDialog.... 请帮帮我....


下面是我的代码,我正在设置alertDialog的高度和xy位置,但它仍然没有显示它的效果..

AlertDialog.Builder builder = new AlertDialog.Builder(this);
       LayoutInflater inf = getLayoutInflater();
        View layout = inf.inflate(R.layout.main, null);
        builder.setView(layout);
        builder.setTitle("Add to Home screen");
    AlertDialog dialog = builder.create();
        WindowManager.LayoutParams WMLP = dialog.getWindow().getAttributes();
        int dialogOriginalHeight = WMLP.height;
    WMLP.height += 750;
    Log.i("XnY", "x="+WMLP.x+", y="+WMLP.y);
    WMLP.x = -10;   //x position
    WMLP.y = -10;   //y position
    Log.i("XnY", "x="+WMLP.x+", y="+WMLP.y);
    dialog.getWindow().setAttributes(WMLP);
    Log.i("POSITION", "POS::HEIGHT:"+WMLP.height);
    dialog.show();
4

2 回答 2

1

我知道这真的很旧,但对于以后看到这个的人来说:

x 和 y 被忽略,因为您使用的是默认重力。

文档

此窗口的 X 位置。使用默认重力它被忽略。当使用 LEFT 或 START 或 RIGHT 或 END 时,它提供从给定边缘的偏移量。

我需要我的从右上角偏移,所以我将我的重力设置为 0x00000035。这是顶部和右侧。(顶部是 0x00000030,右侧是 0x00000005)。这将导致 x 和 y 不会被忽略。

alert.getWindow().setGravity(0x00000035);
于 2011-12-14T17:14:05.190 回答
1
WindowManager.LayoutParams wmlp = dialog.getWindow().getAttributes();

wmlp.gravity = Gravity.BOTTOM | Gravity.RIGHT;
wmlp.x = 50;   //x position
wmlp.y = 50;   //y position

dialog.show();

可能是一个不错的选择

于 2013-05-02T14:16:08.307 回答