Change the default background of AlertDialog

The AlertDialog that comes with Android 4.0 is too ugly, is there any? The black background is ugly. What I realized today is how to customize the style to change the background of AlertDialog

First add the following style in the values/styles.xml file

<style name="AlertDialog" parent="@android:Theme.DeviceDefault.Light.Dialog">
<!-- 这里设置背景为透明,为了隐藏边框 -->
        <item name="android:windowBackground">@android:color/transparent</item>
        <item name="android:windowNoTitle">true</item>
<!-- 这里是修改顶部标题背景颜色,具体颜色自己定,可以是图片 -->
        <item name="android:topDark">@color/white</item>
 <!-- 这里是修改内容区域背景颜色 -->
	<item name="android:centerDark">@color/white</item>
        <item name="android:windowIsTranslucent">true</item>
        <item name="android:windowAnimationStyle">@+android:style/Animation.Translucent</item>
</style>

Specific reference code:

public void showDeleteDiaog(final Account a) {
		//这里的构造函数使用两个参数,第二个参数即为设置样式
		AlertDialog.Builder builder = new AlertDialog.Builder(this,R.style.AlertDialog);
		builder.setMessage("撤销该记录?").setCancelable(false).setTitle("提示")
				.setPositiveButton("确定", new DialogInterface.OnClickListener() {
					public void onClick(DialogInterface dialog, int id) {
						dialog.cancel();
					}
				})
				.setNegativeButton("取消", new DialogInterface.OnClickListener() {
					public void onClick(DialogInterface dialog, int id) {
						dialog.cancel();
					}
				});
		builder.show();
	}

The actual style is as follows:

 

 

 

Guess you like

Origin blog.csdn.net/gs12software/article/details/48224885