final AlertDialog dialog = new AlertDialog.Builder(context).create(); dialog.show(); Window window = dialog.getWindow(); window.setWindowAnimations(R.style.dialogWindowAnim);//Set the dialog box display and disappear animation window.setLayout((int) context.getResources().getDimension(100dp,100dp));//Set the size of the dialog window.setBackgroundDrawableResource(R.drawable.tanchuangbeijing);//Set the background of the dialog 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(); } });
//It is displayed in the middle of the window by default, but you can change the horizontal position by x, and change the vertical position by y.
WindowManager.LayoutParams attributes = window.getAttributes();
attributes.x = x; attributes.y = y; window.setAttributes(attributes); window.setContentView(view);