第一步:一個圓角邊框背景文件: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(); } });