PopupWindow

来源:互联网 发布:青藏铁路灵异事件知乎 编辑:程序博客网 时间:2024/06/11 02:17

初始化

    //  先加载窗口的布局        View layout = getLayoutInflater().inflate(R.layout.popu_layout,                    null);        layout.findViewById(R.id.menu_01).setOnClickListener(this);        layout.findViewById(R.id.menu_02).setOnClickListener(this);        // 创建了弹出框        window = new PopupWindow(layout, 200,                    ViewGroup.LayoutParams.WRAP_CONTENT);        // 配置弹出框的其他信息        // 设置背景(能够响应返回键)        window.setBackgroundDrawable(new ColorDrawable());        // 设置获取焦点        window.setFocusable(true);        window.setOutsideTouchable(true);

显示窗口

window.showAsDropDown(btnShow, -100, 0);参考某个视图显示    第二个第三个参数分别表示水平和垂直方向的偏移量    //显示在一个容器中(相对的容器,位置,x方向的偏移,y方向的偏移)    window.showAtLocation(parent, gravity, x, y)

隐藏窗口

window.dismiss();

PopupMenu

//创建PopuMenu(第二个参数表示触发该弹出菜单的视图)    PopupMenu pMenu = new PopupMenu(this, btnPopuMenu);    //向popumenu中添加菜单    pMenu.getMenuInflater().inflate(R.menu.main, pMenu.getMenu());    //设置菜单项的点击监听    pMenu.setOnMenuItemClickListener(new OnMenuItemClickListener() {        @Override        public boolean onMenuItemClick(MenuItem item) {            //根据菜单项的id来区分菜单            switch (item.getItemId()) {            case R.id.action_settings:                Toast.makeText(PopuWindowActivity.this, item.getTitle(), 2000).show();                    break;            }            return true;        }    });    pMenu.show(); //显示菜单
0 0
原创粉丝点击