APP中抽屉效果的项目讲解
导语 Android开发中,几乎我们在接触的项目中,多多少少都会需要用到PopupWindow来实现的弹出对话框,所以有必要熟悉掌握这个控件的使用 。这个控件其实使用起来并不是很难,如果自己亲自敲一个Demo 出来,我相信很快就可以上手的。下面 ,我将拿自己公司的项目来讲解一下我对于PopupWindow 的使用心得。 1、PopupWindow效果图 还是老习惯,讲一个新控件,先上效果图: 2、PopupWindow是什么? Android开发中常见的对话框有两种:AlertDialog 和 PopupWindow。它们的不同点在于:AlertDialog的位置固定在屏幕的中间,而PopupWindow的位置可以随意 。PopupWindow 这个类用来实现弹出框,可以把任意布局的View作为内容,而且这个弹出框是悬浮在当前Activity之上的。 3、PopupWindow 用法 创建PopupWindow构造方法有四种形式: public PopupWindow (Context context) public PopupWindow(View contentView) public PopupWindow(View contentView, int width, int height) public PopupWindow(View contentView, int width, int height, boolean focusable) 一般我的习惯是选择带有 3 个参数的构造方法 ,原因是PopupWindow是没有默认布局的,它的布局只有通过我们自己设置才行。而PopupWindow 的成功创建需要View contentView,int width, int height ,缺一个都不行 。刚好选择带有 3 个参数的构造方法,可以满足 。如果选择只有一个参数的需要单独设置宽度和搞定,代码如下: PopupWindow popupWindow = new PopupWindow(contentview); popupWindow.setWidth(LayoutParams.WRAP_CONTENT); popupWindow.setHeight(LayoutParams.WRAP_CONTENT); 构造参数的第一个参数contentview:是我们想要展示的内容 。一般我们是把内容写在一个xml布局里,加载的布局代码如下: View layoutLeft=LayoutInflater.from(getContext()).inflate(R.layout.pop_menulist, null); 构造参数的第二个参数width: 就是PopupWindow控件的宽度。构造参数的第三个参数height: 就是PopupWindow控件的高度。 除了构造方法很重要,还有个显示位置的函数也是很重要的,有3个 showAsDropDown(View anchor) showAsDropDown(View anchor, int xoff, int yoff) showAtLocation(View parent, int gravity, int x, int y) 第1个函数是没有偏移的,相对于布局中某个控件的位置(正左下方)第2个函数是有偏移的,相对某个控件的位置。xoff表示相对于X轴的偏移;yoff表示相对于Y轴的偏移。正负值表示方向。第3个函数跟第2个类似,不同的是这个是相对于父控件的位置 。 除了上面讲到的构造方法和显示函数是掌握 PopupWindow 的关键外,它还有几个常用的方法,比如:设置弹出对话框的动画效果:popupWindow.setAnimationStyle(R.style.PopupAnimation);获取焦点:popupWindow.setFocusable(true);对话框消失:popupWindow.dismiss();设置popupwindow外部可点击:popupWindow.setOutsideTouchable(true);设置背景透明:ColorDrawable cd = new ColorDrawable(0x00000000);popupWindow.setBackgroundDrawable(cd); 注意:设置popupwindow外部可点击的时候需要设置背景,不然外部点击的效果是不生效的 。在网络上,我有看到一个解释是这样的:如果有背景,则会在contentView外面包一层PopupViewContainer之后作为mPopupView,如果没有背景,则直接用contentView作为mPopupView。而这个PopupViewContainer是一个内部私有类,它继承了FrameLayout,在其中重写了Key和Touch事件的分发处理 。 4、PopupWindow的示例代码 因为有三个地方都需要用到 PopupWindow ,所以做成一个共用的方法,减少代码重复和冗余 ,有利于代码的复用 。由于这个项目是公司的项目,所以没法给大家搞出完整的代码,只能给出PopupWindow 的核心代码。 //设置条件弹出式方法 public void setConditionPopWindow() { layoutLeft = LayoutInflater.from(getContext()).inflate(R.layout.pop_menulist, null); mRecyclerView = (RecyclerView) layoutLeft .findViewById(R.id.rv_condition); conditonAdpter = new ConditionRAdapter(); conditonAdpter.addDatas(mdata);//添加数据 //1.上下文 2.方向 3.true代表倒序排列,flase代表顺序排 mRecyclerView.setLayoutManager(new GridLayoutManager(getContext(), 3, GridLayoutManager.VERTICAL, false)); mRecyclerView.setAdapter(conditonAdpter); //设置适配器 mRecyclerView.setItemAnimator(new DefaultItemAnimator());//设置动画效果 popupWindow = new PopupWindow(layoutLeft, ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT); ColorDrawable cd = new ColorDrawable(0000); popupWindow.setBackgroundDrawable(cd); popupWindow.setAnimationStyle(R.style.PopupAnimation); popupWindow.update(); popupWindow.setInputMethodMode(PopupWindow.INPUT_METHOD_NEEDED); popupWindow.setTouchable(true); // 设置popupwindow可点击 popupWindow.setOutsideTouchable(true); // 设置popupwindow外部可点击 popupWindow.setFocusable(true); // 获取焦点 // 设置popupwindow的位置(相对tvLeft的位置) //x,y关于原点的偏移量(横坐标,纵坐标) popupWindow.showAsDropDown(llClass, 0, 0); /*设置popupwindow消失的点击事件*/ popupWindow.setOnDismissListener(new PopupWindow.OnDismissListener() { @Override public void onDismiss() { resetFilterStatus(); } }); } 大家之所以都喜欢用PopupWindow ,最主要的还是因为可以设置动画效果。具体步骤如下。在style样式中增加: <style name="PopupAnimation"> <item name="android:windowEnterAnimation">@anim/fade_in</item> <item name="android:windowExitAnimation">@anim/fade_out</item> </style> res下新建anim文件夹,新建两个xml文件,一个是弹出效果一个是收缩效果。fade_in.xml <?xml version="1.0" encoding="UTF-8"?> <set xmlns:android="http://schemas.android.com/apk/res/android"> <scale android:pivotY="0%" android:pivotX="0%" android:toYScale="1.0" android:fromYScale="0.5" android:toXScale="1.0" android:fromXScale="1.0" android:duration="200"/> <alpha android:duration="200" android:toAlpha="1.0" android:interpolator="@android:anim/decelerate_interpolator" android:fromAlpha="0.0"/> </set> fade_out.xml <?xml version="1.0" encoding="UTF-8"?> <set xmlns:android="http://schemas.android.com/apk/res/android"> <scale android:pivotY="0%" android:pivotX="0%" android:toYScale="0.5" android:fromYScale="1.0" android:toXScale="1.0" android:fromXScale="1.0" android:duration="200"/> <alpha android:duration="200" android:toAlpha="0.0" android:interpolator="@android:anim/accelerate_interpolator" android:fromAlpha="1.0"/> </set> 最后调用popupWindow.setAnimationStyle(R.style.PopupAnimation); 5、总结 写了一晚上,有关于PopupWindow 的用法讲完了,可能没有讲的很全面,这只是我个人对于这个控件平时使用的一些心得,如果有需要补充或错误的,欢迎大家留言或者私聊我 。本文属于原创,如有转载,请标注原作者,版权归本公众号所有。如果你喜欢我写的文章请关注程序IT圈 ,欢迎大家继续关注本公众号的技术博文。如果您觉得这篇文章对你有所帮助的话,不妨点个赞或给个赞赏哈,您的支持就是我坚持原创的动力。 推荐阅读福利篇:学习编程视频免费领取 [](http://mp.weixin.qq.com/s?__biz=MzAxMTE3MDkyMA==&mid=2654410864&idx=1&sn=7f383b7cdfb9049ec5a1a87244e2f114&chksm=80869d77b7f1146188195acdd4c56a2e5e00ab1fa20e69af9511c3f8ff49ccc820be936fe953&scene=21#wechat_redirect)Retrofit常见注解全解析 五大步骤快速搭建个人网站