MainActivity中的代码:
//自定义dialog的样式
Dialog dialog = new Dialog(this,R.style.dialog_style);
View view = View.inflate(this, R.layout.dialog, null);
dialog.setContentView(view,new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
//获取dialog的窗体
Window window = dialog.getWindow();
//设置dialog进入时的动画效果
window.setWindowAnimations(R.style.dialog_animstyle);
WindowManager.LayoutParams layoutParams = window.getAttributes();
//设置窗体的宽,高的属性
layoutParams.width = LayoutParams.MATCH_PARENT;
layoutParams.height = LayoutParams.WRAP_CONTENT;
//设置窗体的背景,与dialog样式里面的背景(意义)不同
window.setBackgroundDrawableResource(R.drawable.window_bg);
//将刚才自定义的属性设置上去
window.setAttributes(layoutParams);
dialog.show();
styles.xml中的代码:
<style name="dialog_style" parent="@android:Theme.Dialog">
<!--Dialog的windowFrame框为无 -->
<item name="android:windowFrame">@null</item>
<!--是否显示title -->
<item name="android:windowNoTitle">true</item>
<!--是否半透明 -->
<item name="android:windowIsTranslucent">false</item>
<!--设置dialog的背景 -->
<item name="android:background">@drawable/dialog_bg</item>
<!--是否浮现在activity之上 -->
<item name="android:windowIsFloating">true</item>
<item name="android:windowContentOverlay">@null</item>
<!--背景(activity)是否模糊显示 -->
<item name="android:backgroundDimEnabled">true</item>
</style>
<style name="dialog_animstyle">
<item name="android:windowEnterAnimation">@anim/dialog_in_anim</item>
<item name="android:windowExitAnimation">@anim/dialog_out_anim</item>
</style>
@drawable/dialog_bg背景设置:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" >
<corners android:radius="2dp" />
<solid android:color="@android:color/white" />
<padding
android:bottom="5dp"
android:left="5dp"
android:right="5dp"
android:top="5dp" />
</shape>