设置dialog宽度为屏幕宽度:
Window window = dialog.getWindow();
window.setGravity(Gravity.BOTTOM); //此处可以设置dialog显示的位置window.setWindowAnimations(R.style.anim_push_out); //添加动画
dialog.show();
//设置dialog的宽度和手机宽度一样
WindowManager.LayoutParams lp = dialog.getWindow().getAttributes();
lp.width = window.getWindowManager().getDefaultDisplay().getWidth();
dialog.getWindow().setAttributes(lp);//设置宽度
但是往往dialog还是会有margin,
这时可以给这个AlertDialog设置自定义的样式:
AlertDialog dialog = new AlertDialog.Builder(new ContextThemeWrapper(context, R.style.MyAlertDialog))
.setView(view)
.create();
在样式中把margin设为为负数:
<style name="MyAlertDialog" parent="Theme.AppCompat.Light.Dialog.Alert">
<item name="android:layout_marginLeft">-10dp</item>
<item name="android:layout_marginRight">-10dp</item>
<item name="android:layout_marginBottom">-5dp</item>
</style>
这样这个AlertDialog就是宽度为屏幕宽度,并且和底部没有margin的了。