PopupWindow有点类似于Dialog,相同点在于都是弹出窗口,并且都可以对其进行自定义显示,并且里面的监听组件,进行相应的操作,

但它与Dialog又有很大的区别,PopupWindow只是弹出窗口,不会使宿主Activity组件失去焦点,也就是说PopupWindow弹出后,你仍可以与宿主Activity进行交互,Dialog却不能做到这一点。

参考:http://blog.csdn.net/hlyjunhe/article/details/6572159    http://www.cnblogs.com/noTice520/archive/2011/08/16/2140356.html http://www.2cto.com/kf/201108/100378.html http://www.cnblogs.com/noTice520/archive/2011/02/15/1955541.html

使用PopupWindow可实现弹出窗口效果,,其实和AlertDialog一样,也是一种对话框,两者也经常混用,但是也各有特点。

下面就看看使用方法。 首先初始化一个PopupWindow,指定窗口大小参数。

PopupWindow  mPop = new PopupWindow(getLayoutInflater().inflate(R.layout.window, null), LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
 也可以分开写: LayoutInflater mLayoutInflater = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);
    //自定义布局
ViewGroup menuView = (ViewGroup) mLayoutInflater.inflate( R.layout.window, null, true);
PopupWindow mPop = new PopupWindow(menuView, LayoutParams.WRAP_CONTENT,   LayoutParams.WRAP_CONTENT, true);
 
当然也可以手动设置PopupWindow大小。
          mPop.setContentView(menuView ); //设置包含视图
          mPop.setWidth(int )
         mPop.setHeight(int )//设置弹出框大小

设置进场动画: mPop.setAnimationStyle(R.style.AnimationPreview); // 设置动画样式

mPop.setOutsideTouchable(true);

//这里设置显示PopuWindow之后在外面点击是否有效。如果为false的话,那么点击PopuWindow外面并不会关闭PopuWindow。

当然这里很明显只能在Touchable下才能使用。

当有mPop.setFocusable(false);的时候,说明PopuWindow不能获得焦点,即使设置设置了背景不为空也不能点击外面消失,只能由dismiss()消失,但是外面的View的事件还是可以触发,back键也可以顺利dismiss掉。

当设置为popuWindow.setFocusable(true);的时候,加上下面两行设置背景代码,点击外面和Back键才会消失。

mPop.setFocusable(true);   需要顺利让PopUpWindow dimiss(即点击PopuWindow之外的地方此 或者 back键PopuWindow会消失);PopUpWindow的背景不能为空。必须在popuWindow.showAsDropDown(v); 或者 其它的显示PopuWindow方法之前  设置它的背景不为空:

mPop.setBackgroundDrawable(new ColorDrawable(0));

mPop.showAsDropDown(anchor, 0, 0);     //设置显示PopupWindow的位置位于View的左下方,x,y表示坐标偏移量

mPop.showAtLocation(findViewById(R.id.parent), Gravity.LEFT, 0, -90);   (以某个View为参考),表示弹出窗口以parent组件为参考,位于左侧,偏移-90。

mPop.setOnDismissListenerd(new PopupWindow.OnDismissListener(){})        //设置窗口消失事件

注:window.xml为布局文件

================================

总结:

1、为PopupWindow的view布局,通过LayoutInflator获取布局的view.如:

LayoutInflater inflater =(LayoutInflater)   this.anchor.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);

View  textEntryView =  inflater.inflate(R.layout.paopao_alert_dialog, null);

2、显示位置,可以有很多方式设置显示方式

pop.showAtLocation(findViewById(R.id.ll2), Gravity.LEFT, 0, -90);

或者

pop.showAsDropDown(View anchor, int xoff, int yoff)

3、进出场动画

pop.setAnimationStyle(R.style.PopupAnimation);

4、点击PopupWindow区域外部, PopupWindow消失

this.window = new PopupWindow(anchor.getContext());

this.window.setTouchInterceptor(new OnTouchListener() {

@Override

public boolean onTouch(View v, MotionEvent event) {

if(event.getAction() ==MotionEvent.ACTION_OUTSIDE) {

BetterPopupWindow.this.window.dismiss();

return true;

}

return false;

}

});

======================

1、PopupWindow的隐藏

final PopupWindow window = mPageStatWin;
if(null != window && window.isShowing()) {
    win.dismiss();
}

2、PopupWindow的显示及位置设置

window.showAtLocation(parent, Gravity.RIGHT | Gravity.BOTTOM, 10,10);
第一个参数指定PopupWindow的锚点view,即依附在哪个view上。
第二个参数指定起始点为parent的右下角,第三个参数设置以parent的右下角为原点,向左、上各偏移10像素。
//将PopupWindow作为anchor的下拉窗口显示。即在anchor的左下角显示
window.showAsDropDown(anchor);
//xoff,yoff基于anchor的左下角进行偏移。
window.showAsDropDown(anchor, xoff, yoff);
如果没有充足的空间显示PopupWindow,那么PopupWindow的左下角将位于anchor的左上角来显示。

PopuWindow_1的更多相关文章

随机推荐

  1. HBase 安装过程记录

    http://blog.csdn.net/chenxingzhen001/article/details/7756129 环境: 操作系统Centos 6.4 32-bit 三台节点 ip       ...

  2. request.getParameter与request.getAttribute()

    这里就request为例,不去考虑session. request对象是javax.servlet.http.HttpServletRequest接口的一个实例,request表示调用JSP页面的请求 ...

  3. prtg

    prtg http://www.paessler.com/prtg/features prtg的sensor技术 数据库监视 Flexible Alerting 9 notification tech ...

  4. P1027 木瓜地

    /*=========================================================== 描述 Description Bessie不小心游荡出Farmer John ...

  5. php download断点

    FileDownload.class.php <?php /** php下载类,支持断点续传 *   Date:   2013-06-30 *   Author: fdipzone *   Ve ...

  6. ActionContext表格总结

    用一张表格来总结: 变量 从ActionContext中获得 生命周期 用Ongl来读取值 使用ServletConfigInterceptor来注入 ActionContext类 静态方法Actio ...

  7. CSS命名规则

    头:header  内容:content/container  尾:footer  导航:nav  侧栏:sidebar 栏目:column  页面外围控制整体布局宽度:wrapper  左右中:le ...

  8. PHP使用1个crontab管理多个crontab任务

    http://www.binarytides.com/php-manage-multiple-cronjobs-with-a-single-crontab-entry/ In many php app ...

  9. 在C#中保存Bouncy Castle生成的密钥对

    在用Bouncy Castle的C#版API产生公钥和私钥 中产生了一对密钥对,可以用bouncy caslte提供的API进行保存 公钥方面的3个类,具体代码根据命名空间自行查看其源代码: Org. ...

  10. 51nod1039 x^3 mod p

    X*X*X mod P = A,其中P为质数.给出P和A,求<=P的所有X.   Input 第1行:一个数T,表示后面用作输入测试的数的数量.(1 <= T <= 1000) 第2 ...