Dialog或者Avtivity以Dialog形式弹出后,一般弹出默认是居中的。
但是,我们有时需要它从右边弹出,一直居右。或者从左边弹出,一直居左。
修改纯Dialog的弹出位置:
Dialog dialog = getDialog();
Window window = dialog == null ? null : dialog.getWindow();
if (dialog != null && window != null) {
LayoutParams attr = window.getAttributes();
if (attr != null) {
attr.height = android.view.ViewGroup.LayoutParams.MATCH_PARENT;
attr.gravity = Gravity.RIGHT;
}
}
如果是Activity Dialog的话,则在onCreate()方法修改:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getWindow().getAttributes().gravity = Gravity.RIGHT;
}