自定义AlertDialog设置宽高的同时如何居中显示

Android中自定义AlertDialog时有时需要设置宽高,默认的布局宽度太宽了,设置宽度运行后问题来了,水平方向怎么不居中显示啊?垂直方向默认就是居中的,所以不用管垂直方向的,但是水平方向就是不居中,下面是我的解决代码。

AlertDialog dialog = new AlertDialog.Builder(activity).create();
dialog.setCancelable(false);
dialog.show();
//加载自己的布局
dialog.getWindow().setContentView(R.layout.customer_alert_dialog);
WindowManager.LayoutParams lp = dialog.getWindow().getAttributes();
//设置宽高,高度默认是自适应的,宽度根据屏幕宽度比例设置
lp.width = MainUtils.getScreenWidth(activity)/10*6;
//这里设置居中
lp.gravity = Gravity.CENTER;
dialog.getWindow().setAttributes(lp);

你可能感兴趣的:(自定义AlertDialog设置宽高的同时如何居中显示)