方法一:
在PopupWindow依附的主界面的布局文件里面加上
<LinearLayout
android:id="@+id/cover"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:alpha="0.3"
android:background="#000000"
android:orientation="horizontal"
android:visibility="gone" />
PopupWindow里面不要设置this.setFocusable(true);设置这个PopupWindow关闭的时候我们的LinearLayout黑影不消失,我们可以手动关闭
Popupwindow.setOutsideTouchable(true);
Popupwindow.setTouchInterceptor(new OnTouchListener() {
@Override
public boolean onTouch(View arg0, MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_OUTSIDE) {
Popupwindow.dismiss();
mCover.setVisibility(View.GONE);
return true;
}
return false;
}
});
@Override
public boolean onBackPressed() {
if(Popupwindow.isShowing()){
Popupwindow.dismiss();
mCover.setVisibility(View.GONE);
return true;
}
}
不设置this.setFocusable(true);是针对不需要弹出软键盘的情况
方法二:
// 设置背景颜色变暗(使用这个做黑影)
WindowManager.LayoutParams lp = mActivity.getWindow().getAttributes();
lp.alpha = 0.7f;
mActivity.getWindow().setAttributes(lp);
可以设置this.setFocusable(true);可以弹出键盘