Android 自定义dialog出现的位置

跟 https://www.cnblogs.com/guochangxin/p/11457537.html 配套

1、定义一个dialog的类

public class OtherDialog {
private View view;

public Dialog showOtherDialog(Context context) {
//1、使用Dialog、设置style
final Dialog dialog = new Dialog(context, R.style.DialogTheme);
//2、设置布局
view = View.inflate(context, R.layout.news_bottom_dialog, null);
dialog.setContentView(view);

Window window = dialog.getWindow();
//设置弹出位置
window.setGravity(Gravity.TOP);

int matchParent = ViewGroup.LayoutParams.MATCH_PARENT;//父布局的宽度

Window dialogWindow = dialog.getWindow();
dialogWindow.setGravity(Gravity.TOP | Gravity.RIGHT);
WindowManager.LayoutParams lp = dialogWindow.getAttributes();
lp.width = matchParent;
lp.height = matchParent;
lp.x = matchParent;
lp.y = 300; //设置出现的高度,距离顶部
window.setAttributes(lp);

//设置弹出动画
// window.setWindowAnimations(R.style.main_menu_animStyle);
//设置对话框大小
window.setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
dialog.show();

return dialog;


}
}

2、使用
private OtherDialog otherDialog=new OtherDialog();//实例化
otherDialog.showOtherDialog(getContext());


原文地址:https://www.cnblogs.com/guochangxin/p/11471323.html