Android View 仿iOS7 底部弹出框

来源:互联网 发布:网络平台功能介绍 编辑:程序博客网 时间:2024/05/22 06:27

尊重原创转载请注明:http://blog.csdn.net/bfbx5173/article/details/52020733


UI 老是喜欢以iOS风格为主,同时产品要求两边风格一致。这个时候就悲催了。 什么时候我md可以当家做主,不过事还是要继续做。


   


在此分享一个仿iOS7的底部弹出框。

自定义Dialog 之前在  滚轮控件LoopView+自定义Dialog [时间地域选择器] Picker

有谈到过写法,是java中常用的一种构造者模式,大家多多练习一下就都会了。


public class BottomMenuDialog extends Dialog {    public BottomMenuDialog(Context context, int themeResId) {        super(context, themeResId);    }    public static class Params {        private final List<BottomMenu> menuList = new ArrayList<>();        private View.OnClickListener cancelListener;        private String menuTitle;        private String cancelText;        private Context context;    }    public static class Builder {        private boolean canCancel = true;        private boolean shadow = true;        private final Params p;        public Builder(Context context) {            p = new Params();            p.context = context;        }        public Builder setCanCancel(boolean canCancel) {            this.canCancel = canCancel;            return this;        }        public Builder setShadow(boolean shadow) {            this.shadow = shadow;            return this;        }        public Builder setTitle(String title) {            this.p.menuTitle = title;            return this;        }        public Builder addMenu(String text, View.OnClickListener listener) {            BottomMenu bm = new BottomMenu(text, listener);            this.p.menuList.add(bm);            return this;        }        public Builder addMenu(int textId, View.OnClickListener listener) {            return addMenu(p.context.getString(textId), listener);        }        public Builder setCancelListener(View.OnClickListener cancelListener) {            p.cancelListener = cancelListener;            return this;        }        public Builder setCancelText(int resId) {            p.cancelText = p.context.getString(resId);            return this;        }        public Builder setCancelText(String text) {            p.cancelText = text;            return this;        }        public BottomMenuDialog create() {            final BottomMenuDialog dialog = new BottomMenuDialog(p.context, shadow ? R.style.Theme_Light_NoTitle_Dialog : R.style.Theme_Light_NoTitle_NoShadow_Dialog);            Window window = dialog.getWindow();            window.setWindowAnimations(R.style.Animation_Bottom_Rising);            window.getDecorView().setPadding(0, 0, 0, 0);            WindowManager.LayoutParams lp = window.getAttributes();            lp.width = WindowManager.LayoutParams.MATCH_PARENT;            lp.height = WindowManager.LayoutParams.WRAP_CONTENT;            window.setAttributes(lp);            window.setGravity(Gravity.BOTTOM);            View view = LayoutInflater.from(p.context).inflate(R.layout.dialog_bottom_menu, null);            TextView btnCancel = (TextView) view.findViewById(R.id.btn_cancel);            ViewGroup layContainer = (ViewGroup) view.findViewById(R.id.lay_container);            LayoutParams lpItem = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);            LayoutParams lpDivider = new LayoutParams(LayoutParams.MATCH_PARENT, 1);            int dip1 = (int) (1 * p.context.getResources().getDisplayMetrics().density + 0.5f);            int spacing = dip1 * 12;            boolean hasTitle = !TextUtils.isEmpty(p.menuTitle);            if (hasTitle) {                TextView tTitle = new TextView(p.context);                tTitle.setLayoutParams(lpItem);                tTitle.setGravity(Gravity.CENTER);                tTitle.setTextColor(0xFF8F8F8F);                tTitle.setText(p.menuTitle);                tTitle.setTextSize(17);                tTitle.setPadding(0, spacing, 0, spacing);                tTitle.setBackgroundResource(R.drawable.common_dialog_selection_selector_top);                layContainer.addView(tTitle);                View viewDivider = new View(p.context);                viewDivider.setLayoutParams(lpDivider);                viewDivider.setBackgroundColor(0xFFCED2D6);                layContainer.addView(viewDivider);            }            for (int i = 0; i < p.menuList.size(); i++) {                BottomMenu bottomMenu = p.menuList.get(i);                TextView bbm = new TextView(p.context);                bbm.setLayoutParams(lpItem);                int backgroundResId = R.drawable.common_dialog_selection_selector_center;                if (p.menuList.size() > 1) {                    if (i == 0) {                        if (hasTitle) {                            backgroundResId = R.drawable.common_dialog_selection_selector_center;                        } else {                            backgroundResId = R.drawable.common_dialog_selection_selector_top;                        }                    } else if (i == p.menuList.size() - 1) {                        backgroundResId = R.drawable.common_dialog_selection_selector_bottom;                    }                } else if (p.menuList.size() == 1) {                    backgroundResId = R.drawable.common_dialog_selection_selector_singleton;                }                bbm.setBackgroundResource(backgroundResId);                bbm.setPadding(0, spacing, 0, spacing);                bbm.setGravity(Gravity.CENTER);                bbm.setText(bottomMenu.funName);                bbm.setTextColor(0xFF007AFF);                bbm.setTextSize(19);                bbm.setOnClickListener(bottomMenu.listener);                layContainer.addView(bbm);                if (i != p.menuList.size() - 1) {                    View viewDivider = new View(p.context);                    viewDivider.setLayoutParams(lpDivider);                    viewDivider.setBackgroundColor(0xFFCED2D6);                    layContainer.addView(viewDivider);                }            }            if (!TextUtils.isEmpty(p.cancelText)) {                btnCancel.setText(p.cancelText);            }            if (p.cancelListener != null) {                btnCancel.setOnClickListener(p.cancelListener);            } else {                btnCancel.setOnClickListener(new View.OnClickListener() {                    @Override                    public void onClick(View v) {                        dialog.dismiss();                    }                });            }            dialog.setContentView(view);            dialog.setCanceledOnTouchOutside(canCancel);            dialog.setCancelable(canCancel);            return dialog;        }    }    private static class BottomMenu {        public String funName;        public View.OnClickListener listener;        public BottomMenu(String funName, View.OnClickListener listener) {            this.funName = funName;            this.listener = listener;        }    }}

主体代码如上,逻辑并不是很难。

名字不会取,所以就叫BottomMenuDialog了, 嘿嘿 通俗易懂。

先通过Builder配置一些可变的属性 ,然后在create的时候将这个自己配置的Bottom构建出来。


之后在activity中:

findViewById(R.id.b_5).setOnClickListener(new View.OnClickListener(){            @Override            public void onClick(View v) {                mBottomMenuDialog = new BottomMenuDialog.Builder(MainActivity.this)                        .setTitle("更换封面")                        .addMenu("从手机相册选择", new View.OnClickListener() {                            @Override                            public void onClick(View v) {                                mBottomMenuDialog.dismiss();                                Toast.makeText(v.getContext(), "从手机相册选择" , Toast.LENGTH_SHORT).show();                            }                        }).addMenu("从空间相册选择", new View.OnClickListener() {                             @Override                             public void onClick(View v) {                                 mBottomMenuDialog.dismiss();                                 Toast.makeText(v.getContext(), "从空间相册选择" , Toast.LENGTH_SHORT).show();                             }                         }).addMenu("拍一张", new View.OnClickListener() {                             @Override                             public void onClick(View v) {                                 mBottomMenuDialog.dismiss();                                 Toast.makeText(v.getContext(), "拍一张" , Toast.LENGTH_SHORT).show();                             }                         }).create();                mBottomMenuDialog.show();            }        });

希望能给刚入手的朋友提供帮助,同时节约时间。


[戳我转到源码地址,是一个demo工程,导入ide可直接运行]



0 0
原创粉丝点击