PopupWindow点击外部和返回键消失遇到的坑

在实现popupwindow显示及消失的功能时,功能代码如下:
if (mViewReleasePop == null) {
mViewReleasePop = LayoutInflater.from(mActivity).inflate(R.layout.layout_relaease_secret, null);
}
if (mReleasePopupWindow == null) {

        mReleasePopupWindow = new PopupWindow(mViewReleasePop, LinearLayout.LayoutParams.MATCH_PARENT
                , LinearLayout.LayoutParams.WRAP_CONTENT);
        mReleasePopupWindow.setFocusable(true);
        mReleasePopupWindow.setOutsideTouchable(true);

        mReleasePopupWindow.setOnDismissListener(new PopupWindow.OnDismissListener() {
            @Override
            public void onDismiss() {
                WindowManager.LayoutParams lp = mActivity.getWindow().getAttributes();
                lp.alpha = 1.0f;
                mActivity.getWindow().addFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
                mActivity.getWindow().setAttributes(lp);
            }
        });
    }

    if (mReleasePopupWindow != null && mReleasePopupWindow.isShowing()) {
        return;
    }

    mReleasePopupWindow.setAnimationStyle(R.style.showPopupAnimation);
    mReleasePopupWindow.showAtLocation(mParentLinearl, Gravity.BOTTOM, 0, 0);

    WindowManager.LayoutParams lp = mActivity.getWindow().getAttributes();
    lp.alpha = 0.3f;
    mActivity.getWindow().addFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
    mActivity.getWindow().setAttributes(lp);

功能测试时发现在6.0以上版本功能是西安正常,但是低于6.0的安卓手机上,点击外部不会小时,并且点击返回键的onKeyDown事件都被屏蔽掉了,对比之前写的功能正常的代码,发现少了一句代码,

        mReleasePopupWindow.setBackgroundDrawable();
        因为目前没有背景,就写成
        mReleasePopupWindow.setBackgroundDrawable(new PaintDrawable(Color.TRANSPARENT));

添加之后,在4.4,5.0,6.0机器上测试,功能都恢复了正常,特此记录。

猜你喜欢

转载自blog.csdn.net/gerryrun/article/details/79360404