打造自定义弹出框
2016-10-09 19:44
99 查看
自定义弹出框的使用还是比较频繁,本次主要讲解自定义弹出框和使用的封装,通过接口利于维护。
首先给出自定义弹出框的实现:
首先定义接口分别用于显示和不显示弹出框:
实现接口并注入弹出框对象,并且回调点击事件
剩下的就是使用接口调用方法来完成逻辑业务了。你可能会有不同的界面需求,那么你只需要仿照上面的接口实现类来做就行,当然这需要花上几分钟阅读一下代码。
首先给出自定义弹出框的实现:
public class CustomPopupWindow extends PopupWindow implements OnTouchListener{ protected View mPopupView; protected Context context; /** * PopupWindow显示在哪个view的下面 */ protected View locationView; protected LayoutInflater inflater; protected int _popupWindow_Layout_ID = -1; public View getPopupWindowView(){ return mPopupView; } /** * * @param _popupWindow_Layout_ID:需要显示的视图的布局id * @param viewLayoutLocation_ID:视图显示在哪个布局的下面 */ public void toShowPopupWindowLayout(int _popupWindow_Layout_ID, int viewLayoutLocation_ID){ if (this._popupWindow_Layout_ID == -1){ this._popupWindow_Layout_ID = _popupWindow_Layout_ID; } configPopupWindowView(); showPopupWindow(viewLayoutLocation_ID); } public void toShowPopupWindowLayout(int _popupWindow_Layout_ID, int viewLayoutLocation_ID, PopupWindowLayoutParams params){ if (this._popupWindow_Layout_ID == -1){ this._popupWindow_Layout_ID = _popupWindow_Layout_ID; } if (inflater == null){ inflater = (LayoutInflater) context .getSystemService(Context.LAYOUT_INFLATER_SERVICE); } mPopupView = inflater.inflate(_popupWindow_Layout_ID, null); this.setContentView(mPopupView); if (params == PopupWindowLayoutParams.wrap_parent){ this.setWidth(LayoutParams.WRAP_CONTENT); } else if (params == PopupWindowLayoutParams.match_parent){ this.setWidth(LayoutParams.MATCH_PARENT); } this.setHeight(LayoutParams.WRAP_CONTENT); mPopupView.setFocusable(true); mPopupView.setFocusableInTouchMode(true); this.setOutsideTouchable(false); this.setFocusable(false); this.setTouchable(true); this.setAnimationStyle(R.style.anim_menu_bottombar); ColorDrawable dw = new ColorDrawable(0xEEEEEE);//实例化一个ColorDrawable颜色为0xb0000000半透明 this.setBackgroundDrawable(dw); //设置弹出窗体的背景 mPopupView.setOnKeyListener(new OnKeyListener() { @Override public boolean onKey(View arg0, int keyCode, KeyEvent arg2) { // TODO Auto-generated method stub if (keyCode == KeyEvent.KEYCODE_BACK) { CustomPopupWindow.this.dismiss(); return true; } return false; } }); showPopupWindow(viewLayoutLocation_ID); } public enum PopupWindowLayoutParams{ wrap_parent, match_parent } private void configPopupWindowView(){ if (inflater != null){ return; } inflater = (LayoutInflater) context .getSystemService(Context.LAYOUT_INFLATER_SERVICE); mPopupView = inflater.inflate(_popupWindow_Layout_ID, null); this.setContentView(mPopupView); this.setWidth(LayoutParams.WRAP_CONTENT); this.setHeight(LayoutParams.WRAP_CONTENT); mPopupView.setFocusable(true); mPopupView.setFocusableInTouchMode(true); this.setOutsideTouchable(false); this.setFocusable(false); this.setTouchable(true); this.setAnimationStyle(R.style.anim_menu_bottombar); ColorDrawable dw = new ColorDrawable(0xEEEEEE);//实例化一个ColorDrawable颜色为0xb0000000半透明 this.setBackgroundDrawable(dw); //设置弹出窗体的背景 mPopupView.setOnKeyListener(new OnKeyListener() { @Override public boolean onKey(View arg0, int keyCode, KeyEvent arg2) { // TODO Auto-generated method stub if (keyCode == KeyEvent.KEYCODE_BACK) { CustomPopupWindow.this.dismiss(); return true; } return false; } }); } public CustomPopupWindow(Context context) { super(context); this.context = context; } private void showPopupWindow(int viewLayoutLocation_ID){ locationView = inflater.inflate(viewLayoutLocation_ID, null); this.showAtLocation(locationView, Gravity.BOTTOM|Gravity.CENTER_HORIZONTAL, 0, 0); } @Override public boolean onTouch(View arg0, MotionEvent arg1) { // TODO Auto-generated method stub if (arg1.getAction() == MotionEvent.ACTION_OUTSIDE){ boolean isShow = this.isShowing(); if (isShow){ // 不做处理 } } return false; } }
首先定义接口分别用于显示和不显示弹出框:
public interface IPopupWindowManager { void disposePopupWindowToShow(); void dismissPopupWindow(); }
实现接口并注入弹出框对象,并且回调点击事件
public class PopupWindowManagerImpl implements IPopupWindowManager{ private CustomPopupWindow customPopupWindow; private Context context; private Button btn_popupwindow_cancel_id; private OnPopupWindowClickListener listener; public PopupWindowManagerImpl(Context context, OnPopupWindowClickListener listener){ this.context = context; this.listener = listener; } @Override public void disposePopupWindowToShow() { if (customPopupWindow == null){ customPopupWindow = new CustomPopupWindow(context); } customPopupWindow.toShowPopupWindowLayout(R.layout.popupwindow_view, R.layout.download_files_queue_activity, CustomPopupWindow.PopupWindowLayoutParams.match_parent); btn_popupwindow_cancel_id = (Button) customPopupWindow.getPopupWindowView().findViewById(R.id.popupwindow_cancel_id); btn_popupwindow_cancel_id.setText(Application.getAppString(R.string.cancel_down_queue)); btn_popupwindow_cancel_id.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { listener.onPopupWindowClick(v); } }); } @Override public void dismissPopupWindow() { if (customPopupWindow != null && customPopupWindow.isShowing()){ customPopupWindow.dismiss(); } } public interface OnPopupWindowClickListener{ void onPopupWindowClick(View view); } }
剩下的就是使用接口调用方法来完成逻辑业务了。你可能会有不同的界面需求,那么你只需要仿照上面的接口实现类来做就行,当然这需要花上几分钟阅读一下代码。
相关文章推荐
- 自定义对话框,使弹出的对话框变为Viewpager加tab显示。
- android 自定义Dialog弹出提示框
- AlertDialog.Builder弹出自定义Layout窗口
- Android自定义Dialog实现弹出二维码效果
- 自定义弹出对话框
- 基于Bootstrap里面的Button dropdown打造自定义select
- 自定义Dialog之底部弹出Dialog
- Android 自定义 ViewPager 打造千变万化的图片切换效果
- javascript自定义右键弹出菜单
- iOS 自定义键盘ToolBar(与键盘的弹出、收起保持一致)
- 超简单的自定义AlertDialog弹出键盘可以输入的
- 使用android.hardware.camera2打造新的自定义相机
- Android基础控件——Toast的自定义、按两次返回键返回桌面并弹出自定义吐司、仿映客吐司
- 安卓自定义弹出对话框点击直接拨打电话
- 自定义底部菜单,当弹出输入法时,底部菜单被挤到输入法上面
- 自定义 Javascript 模板规则,打造轻量级模板引擎
- Android 自定义 HorizontalScrollView 打造再多图片(控件)也不怕 OOM 的横向滑动效果
- android自定义弹出框样式实现
- WPF 子窗口弹出并有回弹效果,自定义滚动条
- Android 自定义 HorizontalScrollView 打造再多图片(控件)也不怕 OOM 的横向滑动效果