Android Dialog万能适配器
来源:互联网 发布:网络炒作kim 编辑:程序博客网 时间:2024/05/17 01:39
一个BaseDialog就可以走遍整个APP,只需要编写XML布局就行。
public class BaseDialog extends Dialog { private Context context; public BaseDialog(Context context) { super(context); this.context = context; } public BaseDialog(Context context, int themeResId) { super(context, themeResId); this.context = context; } protected BaseDialog(Context context, boolean cancelable, OnCancelListener cancelListener) { super(context, cancelable, cancelListener); this.context = context; } /* 显示对话框,参数:AppCompatActivity对象、Dialog对象、布局文件、背景资源文件、宽高比例 */ public void config(int layoutResID_contentView, int gravity, int anim, float width_config, float height_config, boolean canceledOnTouchOutside) { requestWindowFeature(Window.FEATURE_NO_TITLE);// 去除对话框的标题 setContentView(layoutResID_contentView);//填充对话框布局 Window window = getWindow(); WindowManager.LayoutParams lp = window.getAttributes(); lp.dimAmount = 0.2f; window.setBackgroundDrawableResource(R.drawable.dialog_bg);//设置对话框边框背景,必须在代码中设置对话框背景,不然对话框背景是黑色的 window.setGravity(gravity); switch (anim) { case 0://从底部进入 window.setWindowAnimations(R.style.dialog_anim_bottom); break; case 1://从头部进入 window.setWindowAnimations(R.style.dialog_anim_top); break; default: window.setWindowAnimations(R.style.dialog_zoom); break; } if (width_config != 0 && height_config != 0) { window.setLayout((int) (ScreenUtils.getScreenWidth(context) * width_config), (int) (ScreenUtils.getScreenHeight(context) * height_config)); } else { window.setLayout(WindowManager.LayoutParams.MATCH_PARENT, WindowManager.LayoutParams.WRAP_CONTENT); } setCanceledOnTouchOutside(canceledOnTouchOutside ? true : false); } public void config(int layoutResID_contentView, int gravity, int anim, boolean canceledOnTouchOutside) { requestWindowFeature(Window.FEATURE_NO_TITLE);// 去除对话框的标题 setContentView(layoutResID_contentView);//填充对话框布局 Window window = getWindow(); WindowManager.LayoutParams lp = window.getAttributes(); lp.dimAmount = 0.2f; window.setBackgroundDrawableResource(R.drawable.dialog_bg);//设置对话框边框背景,必须在代码中设置对话框背景,不然对话框背景是黑色的 window.setGravity(gravity); switch (anim) { case 0://从底部进入 window.setWindowAnimations(R.style.dialog_anim_bottom); break; case 1://从头部进入 window.setWindowAnimations(R.style.dialog_anim_top); break; default: window.setWindowAnimations(R.style.dialog_zoom); break; } window.setLayout(WindowManager.LayoutParams.MATCH_PARENT, WindowManager.LayoutParams.WRAP_CONTENT); setCanceledOnTouchOutside(canceledOnTouchOutside ? true : false); } public void setMarginTop(int top) { Window window = getWindow(); WindowManager.LayoutParams layoutParams = window.getAttributes(); layoutParams.y = top; }}
动画:
<style name="dialog_zoom"> <item name="android:windowEnterAnimation">@anim/dialog_scale_in</item> <item name="android:windowExitAnimation">@anim/dialog_scale_out</item> </style> <style name="dialog_anim_top"> <item name="android:windowEnterAnimation">@anim/dialog_top_in</item> <item name="android:windowExitAnimation">@anim/dialog_top_out</item> </style> <style name="dialog_anim_bottom"> <item name="android:windowEnterAnimation">@anim/dialog_bottom_in</item> <item name="android:windowExitAnimation">@anim/dialog_bottom_out</item> </style>
dialog_bottom_in.xml:
<?xml version="1.0" encoding="utf-8"?><set xmlns:android="http://schemas.android.com/apk/res/android" android:duration="300" > <translate android:fromYDelta="100%" android:toYDelta="0" /> <alpha android:fromAlpha="0" android:toAlpha="1" /></set>
dialog_bottom_out.xml:
<?xml version="1.0" encoding="utf-8"?><set xmlns:android="http://schemas.android.com/apk/res/android" android:duration="300" > <translate android:fromYDelta="0" android:toYDelta="100%" /> <alpha android:fromAlpha="1" android:toAlpha="0" /></set>
dialog_scale_in.xml:
<?xml version="1.0" encoding="utf-8"?><set xmlns:android="http://schemas.android.com/apk/res/android" android:duration="300" > <scale android:fromXScale="0" android:fromYScale="0" android:pivotX="50%" android:pivotY="50%" android:toXScale="1" android:toYScale="1" /> <alpha android:fromAlpha="0" android:toAlpha="1" /></set>
dialog_scale_out.xml:
<?xml version="1.0" encoding="utf-8"?><set xmlns:android="http://schemas.android.com/apk/res/android" android:duration="300" > <scale android:fromXScale="1" android:fromYScale="1" android:pivotX="50%" android:pivotY="50%" android:toXScale="0" android:toYScale="0" /> <alpha android:fromAlpha="1" android:toAlpha="0" /></set>
dialog_top_in.xml:
<?xml version="1.0" encoding="utf-8"?><set xmlns:android="http://schemas.android.com/apk/res/android" android:duration="300" > <translate android:fromYDelta="-100%" android:toYDelta="0%" /> <alpha android:fromAlpha="0" android:toAlpha="1" /></set>
dialog_top_out.xml:
<?xml version="1.0" encoding="utf-8"?><set xmlns:android="http://schemas.android.com/apk/res/android" android:duration="300" > <translate android:fromYDelta="0%" android:toYDelta="-100%" /> <alpha android:fromAlpha="1" android:toAlpha="0" /></set>
使用方法:
dialog_photo.xml:
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" style="@style/padding_content" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_marginBottom="10dp" android:orientation="vertical"> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:background="@drawable/white_shape" android:orientation="vertical"> <TextView android:id="@+id/tv_photo" android:layout_width="match_parent" android:layout_height="@dimen/height_btn_login" android:gravity="center" android:text="拍照" android:textColor="@color/Blue" android:textSize="@dimen/size_2" /> <View android:layout_width="match_parent" android:layout_height="0.1dp" android:background="@color/line" /> <TextView android:id="@+id/tv_album" android:layout_width="match_parent" android:layout_height="@dimen/height_btn_login" android:gravity="center" android:text="从相册选择" android:textColor="@color/Blue" android:textSize="@dimen/size_2" /> </LinearLayout> <TextView android:id="@+id/tv_photo_cancel" android:layout_width="match_parent" android:layout_height="@dimen/height_btn_login" android:layout_marginTop="10dp" android:background="@drawable/white_shape" android:gravity="center" android:text="取消" android:textColor="@color/Blue" android:textSize="@dimen/size_2" /></LinearLayout>
BaseDialog dialog_avatar = new BaseDialog(this); dialog_avatar.config(R.layout.dialog_photo, Gravity.BOTTOM, 0, 0, 0, true);
阅读全文
0 0
- Android Dialog万能适配器
- Android 万能适配器
- Android-打造万能适配器
- Android万能适配器
- Android中万能适配器
- Android 万能适配器
- Android--ListView--万能适配器
- Android 万能适配器
- Android 打造万能适配器
- Android万能适配器
- android打造万能适配器
- Android 万能适配器
- [Android]自定义万能Dialog
- RecyclerView万能适配器,dialog有FlycoDialog框架
- Android万能适配器基类
- android万能适配器之SuperAdapter
- Android打造ListView万能适配器
- android:打造万能通用适配器
- 支持向量机通俗导论(理解SVM的三层境界)
- JavaScript学习笔记
- linux下内存查看工具
- 字符统计
- [cnblogs镜像]我为什么不自建博客 谈谈自建博客的好处与坏处
- Android Dialog万能适配器
- Eclipse快捷键使用大全
- MAC & Workbench & MYSQL 入门:新建表
- 练习题
- TCP拥塞控制机制
- [cnblogs镜像]页面定制CSS代码初探(五):给每篇文章最后加上'<完>'
- 解决centos下yum install epel-release报错:CentOS No package nginx available.
- 接下来准备按照这个博主的需求开发一个完整的系统
- Go语言实战笔记