Android dialog show top & left

时间:2015-10-29 15:42:09

标签: android dialog

I want to dialog appears in center I use this code .

public void dialog(View v){
    Dialog d=new Dialog(this);
    d.setContentView(R.layout.dl_search);
    d.setCancelable(true);
    d.show();
}

Appear the dialog in center . ![1][1] But when I add this code .

d.getWindow().setBackgroundDrawable(new BitmapDrawable(getResources(),blurbitmap));

The dialog appear in top & left . ![2][2] ....

I want to be dialog in center ** **Thanks for help

[1]: http:// i.stack.imgur.com/UPT3S.jpg

[2]: http:// i.stack.imgur.com/K0G1w.jpg

1 个答案:

答案 0 :(得分:0)

使用此:

Window window = customdialog.getWindow();
window.setLayout(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
window.setGravity(Gravity.CENTER);

或尝试:

android:gravity="center_vertical|center_horizontal"