Android进阶:PopupWindow详解

来源:互联网 发布:粤语听古仔软件下载 编辑:程序博客网 时间:2024/06/06 17:24

PopupWindow可以创建类似对话框风格的窗口

使用步骤
1、调用PopupWindow的构造器初始化PopupWindow对象
2、设置相关参数
3、调用PopupWindow的showAsDown(View v)方法将PopupWindow作为v组件的下拉组件显示出来;或调用ShowAtLocation()方法将PopupWindow在指定位置显示出来

一、初始化:
构造函数

public 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);//contentView要显示的view,width宽度,height高度,focusable是否获得焦点

1、通过构造函数设置View布局,宽度,高度

PopupWindow popupWindow = new PopupWindow(                getLayoutInflater().inflate(R.layout.window, null),//布局                LayoutParams.WRAP_CONTENT,//宽度,也可以为数字如200  像素                LayoutParams.WRAP_CONTENT)//高度,也可以为数字如200  像素

2、手动设置View布局、宽度和高度

PopupWindow popupWindow=new PopupWindow();     popupWindow.setContentView(mConvertView);//布局popupWindow.setWidth(mWidth);//宽度popupWindow.setHeight(mHeight);//高度

二、设置相关参数

mPopupWindow.setAnimationStyle(R.style.anim);//设置进出动画mPopupWindow.setFocusable(true);//设置焦点mPopupWindow.setTouchable(true);//设置mPopupWindow是否可触摸mPopupWindow.setOutsideTouchable(true);//设置外部是否可点击,如果点击外部mPopupWindow消失,必须设置背景mPopupWindow.setBackgroundDrawable(new BitmapDrawable());//设置背景mPopupWindow.setTouchInterceptor(new View.OnTouchListener() {//触摸事件            @Override            public boolean onTouch(View v, MotionEvent event) {                if(event.getAction()==MotionEvent.ACTION_OUTSIDE){                  mPopupWindow.dismiss();//mPopupWindow消失                  return true;                }                return false;            }        });mDirPopupWindow.setOnDismissListener(new PopupWindow.OnDismissListener() {//消失事件监听            @Override            public void onDismiss() {            }        });

三、显示PopupWindow
方法:

public void showAsDropDown(View anchor){}//在anchor的正左下方显示,无偏移public void showAsDropDown(View anchor, int xoff, int yoff){}//anchor为组件,xoff为x轴偏移量,yoff为y轴偏移量public void showAtLocation(View parent, int gravity, int x, int y){}//parent为父控件,gravity为相对parent的位置,x,y为偏移量

实例:

mPopupWindow.showAsDropDown(v);mPopupWindow.showAsDropDown(v,0,0);mPopupWindow.showAtLocation(v,Gravity.CENTER,20,20);

四、疑难解析
1、点击PopupWindow外部空白处,PopupWindow消失解析

mPopupWindow.setFocusable(true);//popupWindow可获得焦点mPopupWindow.setTouchable(true);mPopupWindow.setOutsideTouchable(true);//设置PopupWindow外部可点击mPopupWindow.setBackgroundDrawable(new BitmapDrawable());//设置背景mPopupWindow.setTouchInterceptor(new View.OnTouchListener() {     @Override     public boolean onTouch(View v, MotionEvent event) {         if(event.getAction()==MotionEvent.ACTION_OUTSIDE){              dismiss();              return true;          }          return false;   } });

如果想点击空白处,PopupWindow消失,必须设置setFocusable(true)(PopupWindow获得焦点),setOutsideTouchable(true)(设置外部可点击),设置背景不为空。这些设置必须在showAsDropDown()或showAtLocation()之前完成,

背景影响点击事件的原因:
如果有背景,则会在contentView外面包一层PopupViewContainer之后作为mPopupView
如果没有背景,则直接用contentView作为mPopupView。而这个PopupViewContainer是一个内部私有类,它继承了FrameLayout,在其中重写了Key和Touch事件的分发处理

2、动画
从底部弹出和消失的动画:

mPopupWindow.setAnimationStyle(R.style.popupWindow_anim);
<style name="popupWindow_anim">        <item name="android:windowEnterAnimation">@anim/slide_up</item>        <item name="android:windowExitAnimation">@anim/slide_down</item></style>

slide_up.xml

<?xml version="1.0" encoding="utf-8"?><set xmlns:android="http://schemas.android.com/apk/res/android">    <translate        android:fromXDelta="0"        android:toXDelta="0"        android:fromYDelta="100%"        android:toYDelta="0"        android:duration="200"        /></set>

slide_down.xml

<set xmlns:android="http://schemas.android.com/apk/res/android">    <translate        android:fromXDelta="0"        android:toXDelta="0"        android:fromYDelta="0"        android:toYDelta="100%"        android:duration="200"        /></set>
0 0
原创粉丝点击