怎么使点击一个popupwindow窗口之外的区域popupwindow消失
展开全部
只需要设置proupwindows的setOutsideTouchable属性即可。
以下为示例代码:
private void showPopupWindow(View parent) { if (popupWindow == null) { LayoutInflater layoutInflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE); view = layoutInflater.inflate(R.layout.group_list, null); lv_group = (ListView) view.findViewById(R.id.lvGroup); Collections.reverse(groups); GroupAdapter groupAdapter = new GroupAdapter(this, groups); lv_group.setAdapter(groupAdapter); popupWindow = new PopupWindow(view, 200, 220); } popupWindow.setFocusable(true); popupWindow.setOutsideTouchable(true); //设置点击屏幕其它地方弹出框消失 popupWindow.setBackgroundDrawable(new BitmapDrawable()); WindowManager windowManager = (WindowManager) getSystemService(Context.WINDOW_SERVICE); int xPos = -popupWindow.getWidth() / 2 + getCustomTitle().getCenter().getWidth() / 2; popupWindow.showAsDropDown(parent, xPos, 4); lv_group.setOnItemClickListener(new OnItemClickListener() { @Override public void onItemClick(AdapterView<?> adapterView, View view, int position, long id) { loadNew(((StringItem)(groups.get(position))).getId()); if (popupWindow != null) popupWindow.dismiss(); } }); }
以下为示例代码:
private void showPopupWindow(View parent) { if (popupWindow == null) { LayoutInflater layoutInflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE); view = layoutInflater.inflate(R.layout.group_list, null); lv_group = (ListView) view.findViewById(R.id.lvGroup); Collections.reverse(groups); GroupAdapter groupAdapter = new GroupAdapter(this, groups); lv_group.setAdapter(groupAdapter); popupWindow = new PopupWindow(view, 200, 220); } popupWindow.setFocusable(true); popupWindow.setOutsideTouchable(true); //设置点击屏幕其它地方弹出框消失 popupWindow.setBackgroundDrawable(new BitmapDrawable()); WindowManager windowManager = (WindowManager) getSystemService(Context.WINDOW_SERVICE); int xPos = -popupWindow.getWidth() / 2 + getCustomTitle().getCenter().getWidth() / 2; popupWindow.showAsDropDown(parent, xPos, 4); lv_group.setOnItemClickListener(new OnItemClickListener() { @Override public void onItemClick(AdapterView<?> adapterView, View view, int position, long id) { loadNew(((StringItem)(groups.get(position))).getId()); if (popupWindow != null) popupWindow.dismiss(); } }); }
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询