本文主要是介绍android 实现点击屏幕其他地方popupwindow消失,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
Popupwindow如果需要点击空白处自动消失,需要设置两个函数
1、customPopWindow.setFocusable(true);该函数也可以在构造函数中设置,如:mPopupWindow = new PopupWindow(popunwindwow,LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT,true);最后一个参数true即为设定Focusable属性。
2、customPopWindow.setBackgroundDrawable(new BitmapDrawable());
此处需要注意:上述两个方法必须放在showAtLocation()方法之前,才能起作用。
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(); } }); }
这篇关于android 实现点击屏幕其他地方popupwindow消失的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!