Changing position of the Dialog on screen android

2019-01-03 23:11发布

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??

8条回答
smile是对你的礼貌
2楼-- · 2019-01-03 23:48
dialog.getWindow().getAttributes().gravity = Gravity.BOTTOM;
查看更多
劫难
3楼-- · 2019-01-03 23:49

I found this code snippet from @gypsicoder code here

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.

查看更多
叛逆
4楼-- · 2019-01-03 23:58

For me, this worked out pretty well where I was trying to position my dialog somewhere exactly at the bottom of the textview where it gets selected.

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);
}
查看更多
等我变得足够好
5楼-- · 2019-01-04 00:03

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

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

查看更多
Emotional °昔
6楼-- · 2019-01-04 00:07
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();
}

you can customize you dialog based on gravity and layout parameters change gravity and layout parameter on the basis of your requirenment

查看更多
爷、活的狠高调
7楼-- · 2019-01-04 00:08

just add this to your code:

dialog.getWindow().setGravity(Gravity.BOTTOM);
查看更多
登录 后发表回答