第一步:一个圆角边框背景文件:shape_bg_waring.xml
<?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle"> <!--<!–描边设置–>--> <!--<stroke android:color="@android:color/darker_gray"--> <!--android:width="1px"--> <!--/>--> <!--填充设置--> <solid android:color="@android:color/white"/> <!--圆角设置--> <corners android:radius="15dp"/> </shape>
第二步:在自定义的布局中的Layout中添加一条属性:
android:background="@drawable/shape_bg_waring"
第三步:自定义AlertDialog的代码
// 构建dialog显示的view布局 View view = getLayoutInflater().from(this).inflate(R.layout.dialog_warning_layout, null); AlertDialog dialog = new AlertDialog.Builder(this) .create(); dialog.show(); // 设置点击可取消 dialog.setCancelable(true); //给AlertDialog设置4个圆角 dialog.getWindow().setBackgroundDrawableResource(R.drawable.shape_bg_waring); // 获取Window对象 Window window = dialog.getWindow(); // 设置显示视图内容 window.setContentView(view); TextView textView = view.findViewById(R.id.dialog_waring_title); textView.setText(content); TextView buttonText = view.findViewById(R.id.dialog_waring_text); buttonText.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { dialog.dismiss(); } });