安卓4.0自带的AlertDialog太丑了有木有?黑色的背景很难看,今天实现的是怎么自定义style实现改变AlertDialog背景
首先在values/styles.xml文件中增加如下style
<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>
具体引用代码:
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();
}
实际样式如下: