1、構造函數
Dialog默認使用所在Activity主題:
public Dialog(Context context) {
this(context, 0, true);
}
使用指定的主題:
public Dialog(Context context, int theme) {
this(context, theme, true);
}
2、常用用法
Dialog實質是一個Window,要控制Dialog的顯示位置和大小實質就是控制Window的顯示位置和大小
mDialog=new Dialog(this,R.style.Translucent_NoTitle);
mDialog.setCancelable(true);//返回鍵可以消失
mDialog.setCanceledOnTouchOutside(true);//點擊外面消失
mDialog.setContentView(R.layout.dialog_layout);
WindowManager.LayoutParams layoutParams=mDialog.getWindow().getAttributes();
layoutParams.gravity= Gravity.BOTTOM|Gravity.LEFT;
layoutParams.width=getScreenWidth(this)/3*2;
layoutParams.x=50;//參照點x座標
layoutParams.y=50;//參照點y座標
3、設置動畫
mDialog.getWindow().setWindowAnimations(R.style.anim_style);
style文件:
<style name="anim.style" parent="android:Animation">
<item name="android:windowEnterAnimation">@anim/dialog_enter</item>
<item name="android:windowExitAnimation">@anim/dialog_exit</item>
</style>
Dialog彈出動畫:
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate
android:duration="600"
android:fromYDelta="100%p"
/>
</set>
消失動畫:
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate
android:toYDelta="100%p"
android:duration="600"
/>
</set>