final AlertDialog dialog = new AlertDialog.Builder(context).create(); dialog.show(); Window window = dialog.getWindow(); window.setWindowAnimations(R.style.dialogWindowAnim);//设置对话框显示和消失动画 window.setLayout((int) context.getResources().getDimension(100dp,100dp));//设置对话框的大小 window.setBackgroundDrawableResource(R.drawable.tanchuangbeijing);//设置对话框的背景 LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); View view = inflater.inflate(R.layout.dialog_prompt, null); TextView tvDesc = (TextView) view.findViewById(R.id.tv_desc); if (!TextUtils.isEmpty(text)) { tvDesc.setText(text); } Button btnPositive = (Button) view.findViewById(R.id.btn_positive); Button btnNegative = (Button) view.findViewById(R.id.btn_negative); btnPositive.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { if (positiveListener != null) { positiveListener.onClick(v); dialog.dismiss(); } } }); btnNegative.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { dialog.dismiss(); } });
//默认显示在窗口的正中间,但是可以通过x改变水平位置,y改变垂直位置,x为正向右偏移,反之向左偏移,y为正向下偏移,反之向上;
WindowManager.LayoutParams attributes = window.getAttributes();
attributes.x = x; attributes.y = y; window.setAttributes(attributes); window.setContentView(view);