android 创建自定义对话框
16lz
2021-01-23
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);
更多相关文章
- android 自定义用相机拍照后的照片存储位置
- Android之百度地图开发,包含定位,覆盖物,经纬度位置转换,长按设置中
- android 获取手机位置信息
- [置顶] Android 实现对话框圆角功能
- Android,对话框定时自动关闭的实现
- 修改AVD存放位置
- 在任意位置获取应用程序Context