Dismiss PopupWindow on touch outside popup, without using deprecated constructor

▼魔方 西西 提交于 2019-12-09 09:03:57

问题


I have a PopupWindow and I wanted it to dismiss when the user touches outside, so I looked into and found out that I had to use popup.setBackgroundDrawable(new BitmapDrawable());. The problem is that the constructor new BitmpaDrawable() is deprecated. I Would like to find a solution without using it.

Anybody knows how to solve this?

Thanks!

                final PopupWindow popup = new PopupWindow(sortByView,
                                          ViewGroup.LayoutParams.WRAP_CONTENT,
                                          ViewGroup.LayoutParams.WRAP_CONTENT,            
                                          true);
                popup.setBackgroundDrawable(new BitmapDrawable());
                popup.setOutsideTouchable(true);
                popup.showAsDropDown(v);

回答1:


What I had to do to get it to work:

popup.setBackgroundDrawable(new ColorDrawable(ContextCompat.getColor(context, android.R.color.transparent)));
popup.setOutsideTouchable(true);



回答2:


Hmm setBackgroundDrawable don't dissmiss popup window. I think that default behavior of popup window is to dismiss on touching outside but you may add onDismiss listener like that

popup.setOnDismissListener(new PopupWindow.OnDismissListener() {

    @Override
    public void onDismiss() {
        popup.dismiss();
        // end may TODO anything else                   
    }
});



回答3:


You could try.

popup.setBackgroundDrawable(new BitmapDrawable(getResources(),
            ""));


来源:https://stackoverflow.com/questions/19178069/dismiss-popupwindow-on-touch-outside-popup-without-using-deprecated-constructor

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!