Dialog使用总结

来源:互联网 发布:22级战舰升级数据 编辑:程序博客网 时间:2024/06/01 08:08

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>
0 0
原创粉丝点击