实现自己的DialogBuilder封装:大小、显示位置和动画

来源:互联网 发布:淘宝香水四大靠谱点 编辑:程序博客网 时间:2024/05/13 16:31

简单介绍

  1. 借助系统的Dialog和Window对象实现
  2. 将Dialog分为三个部分title、content、bottom。给对应的部分设置View才会显示,否则不显示 。
  3. 提供:绝对值,屏幕宽高百分比设置Dialog窗口的宽高
  4. 提供:设置Dialog出现位置,支持左,左上….左下等8个位置控制
  5. 提供:约束Dialog出现位置,通过指定View的参考点和Dialog的参考点
  6. 提供方法设置出现、消失动画

demo

DialogBuilder 使用例子

对照 GIF 第一个展示

SimpleDialogBuilderDialogBuilder 的派生类,牺牲一定的定制能力更简单便捷的使用。
setLayoutParamsByPercent(…):根据屏幕宽、高百分比设置Dialog的具体宽高

new SimpleDialogBuilder(this)                .setTitle("Simple DialogBuilder Test")                .setContent("This is a Dialog.\nThis is a Dialog.\nThis is a Dialog.\nThis is a Dialog.")                .setPositiveButton("知道了", new View.OnClickListener() {                    @Override                    public void onClick(View v) {                        NotifyUtils.toast("知道了");                    }                })                .setLayoutParamsByPercent(0.7f, 0.3f)                .build()                .show();

对照 GIF 第二个展示

setLayoutGravity(…):指定 Dialog 显示在屏幕的位置

new DialogBuilder(this)                // 设置布局,并在回调中国 View 数据和动作                // setTitleLayout()、setBottomLayout() 使用方式类似                .setContentLayout(R.layout.v_dialog_menu, new DialogBuilder.ViewCreatorCallback() {                    @Override                    public void onViewCreated(View view, Dialog dialog) {//                        view.findViewById(R.id.xxx).setOnClickListener(onClicker);                    }                })                .setAnimations(R.style.DialogAnimationStyle1)                .setLayoutParams(ViewUtils.dp2px(150), ViewUtils.dp2px(300))                .setLayoutGravity(Gravity.END | Gravity.TOP)                .build()                .show();

对照 GIF 第三个展示

setDisplayLocation(…):根据指定 TargetView ,并分别设置 TargetView 的参考点和 Dialog 的参考点来确定Dialog的显示位置
setAnimations(…):设置Dialog的出现动画

new DialogBuilder(this)                .setContentLayout(R.layout.v_dialog_menu, null)                .setAnimations(R.style.DialogAnimationStyle2)                .setLayoutParams(ViewUtils.dp2px(150), ViewUtils.dp2px(30))                .setDisplayLocation(tvText3, DialogBuilder.POINT_END_BOTTOM, DialogBuilder.POINT_END_TOP)                .build()                .show();

完整代码在github,里面还有更多功能哦

阅读全文
0 0
原创粉丝点击