您可以使用dialogBuiler.setView(your_custom_layout)
例如,自定义对话框布局文件custom_dialog_layout.xml
,您可以根据自己的喜好进行美化。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:gravity="center"
android:orientation="vertical">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:padding="7dp"
android:orientation="horizontal">
<ImageView
android:layout_width="30dp"
android:layout_height="30dp"
android:id="@+id/custom_diag_img_view"
android:src="@android:drawable/ic_dialog_alert"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/custom_diag_title_tv"
style="@style/TextAppearance.AppCompat.Headline"/>
</LinearLayout>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/custom_diag_msg_tv"
android:layout_marginTop="15dp"
style="@style/TextAppearance.AppCompat.Subhead"/>
</LinearLayout>
Utils.java
public static void showAlertDialog(Context activityContext, Integer iconResource, String title, String message){
AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(activityContext, R.style.Theme_MaterialComponents_DayNight_Dialog_Alert);
LinearLayout customLL = (LinearLayout) LayoutInflater.from(activityContext).inflate(R.layout.custom_dialog_layout, null);
ImageView icon = customLL.findViewById(R.id.custom_diag_img_view);
if(iconResource != null)
icon.setImageResource(iconResource);
android.widget.TextView titleTV = customLL.findViewById(R.id.custom_diag_title_tv);
titleTV.setText(title);
TextView msgTV= customLL.findViewById(R.id.custom_diag_msg_tv);
msgTV.setText(message);
dialogBuilder
.setView(customLL)
.setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
}
})
.show();
}
MyActivity.java
//without title
Utils.showAlertDialog(UploadActivity.this,R.drawable.ic_error, null, getString(R.string.allowed_images))
//with title
Utils.showAlertDialog(UploadActivity.this,R.drawable.ic_error, "Some title, getString(R.string.allowed_images))
您可以添加复选框或其他操作