zoukankan      html  css  js  c++  java
  • PopupWindow的使用


            PopupWindow popupWindow = new PopupWindow(this);
            popupWindow.setContentView(LayoutInflater.from(this).inflate(R.layout.activity_main, null));
            popupWindow.setWidth(110);
            popupWindow.setHeight(500);
            popupWindow.showAtLocation(viewPager, Gravity.BOTTOM, 0, 0);    

    即可

    使用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之外的地方此或者backPopuWindow会消失);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为布局文件


    总结:

    1PopupWindowview布局,通过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;

    }

    });

  • 相关阅读:
    HDU 2639 Bone Collector II (01背包,第k解)
    POJ 2184 Cow Exhibition 奶牛展(01背包,变形)
    hihoCoder #1165 : 益智游戏 (挑战赛11 B题)
    UVA 562 Dividing coins 分硬币(01背包,简单变形)
    POJ Charm Bracelet 挑饰品 (常规01背包)
    hiho一下 第四十四周 博弈游戏·Nim游戏(直接公式解)
    UVA 624 CD(01背包,要记录路径)
    118 Pascal's Triangle 帕斯卡三角形 杨辉三角形
    117 Populating Next Right Pointers in Each Node II 每个节点的右向指针 II
    116 Populating Next Right Pointers in Each Node 每个节点的右向指针
  • 原文地址:https://www.cnblogs.com/lzh-Linux/p/4451767.html
Copyright © 2011-2022 走看看