没有遮罩层
View view1 = getLayoutInflater().inflate(R.layout.pop_discord, null);
PopupWindow popupWindow = new PopupWindow(view1, ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
popupWindow.setOutsideTouchable(true);//点击空白是否关闭
popupWindow.setAttachedInDecor(true);
popupWindow.showAtLocation(view1,Gravity.CENTER,0,0);
pop_discord
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/password_pop"
android:paddingStart="15dp"
android:paddingTop="12dp"
android:paddingEnd="15dp"
android:paddingBottom="12dp"
android:text="两次新密码不一致"
android:textColor="@color/white">
</TextView>
设置遮罩层
private void backgroundAlpha(float f) {
WindowManager.LayoutParams lp =getWindow().getAttributes();
lp.alpha = f;
getWindow().setAttributes(lp);
}
backgroundAlpha()方法用于设置PopupWindow显示后的背景半透明,参数 f 的范围是0.0~1.0,数值越大透明度越高。
But 关闭弹窗时会出现点击事件穿透的情况。目前我不会解决。。。建议用dialog,哈哈哈。
PopupWindow控制位置
showAsDropDown(View anchor)://相对某个控件的位置(正左下方),无偏移
showAsDropDown(View anchor, int xoff, int yoff)://相对某个控件的位置,有偏移
showAtLocation(View parent, int gravity, int x, int y)://相对于父控件的位置(例如正中央Gravity.CENTER,下方Gravity.BOTTOM等),//可以设置偏移或无偏移