public class PopuUtils {
public static PopupWindow popupWindow;
public static View sPopLayout;
private static Activity mActivity;
/**
* 封装的这个popupwindow的工具类
* @param v 显示在哪个控件下面
* @param y 显示的y轴的位置
* @param convertViewResource 填充到popupWindow中的布局文件id
* @param activity 依附的activity
* @param gravity 在布局的位置
*/
public static void showPopWindow(View v, int y, int convertViewResource, final Activity activity,int gravity) {
if (popupWindow == null || mActivity != activity) {
sPopLayout = LayoutInflater.from(activity).inflate(convertViewResource, null);
popupWindow = new PopupWindow(sPopLayout, LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT, true);
WindowManager.LayoutParams lp = activity.getWindow().getAttributes();
lp.alpha = 0.4f;
activity.getWindow().setAttributes(lp);
popupWindow.setOutsideTouchable(true);
popupWindow.setFocusable(true);
popupWindow.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
} else {
if (popupWindow.isShowing()) {
popupWindow.dismiss();
} else {
WindowManager.LayoutParams lp = activity.getWindow().getAttributes();
lp.alpha = 0.4f;
activity.getWindow().setAttributes(lp);
}
}
mActivity = activity;
popupWindow.setOnDismissListener(()-> {
WindowManager.LayoutParams lp = activity.getWindow().getAttributes();
lp.alpha = 1f;
activity.getWindow().setAttributes(lp);
});
popupWindow.setSoftInputMode(PopupWindow.INPUT_METHOD_NEEDED);
popupWindow.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);
popupWindow.showAtLocation(v,gravity , 0, y);
}
/**
* 返回popupWindow的view
* @return
*/
public static View getPopLayout() {
return sPopLayout;
}
/**
* 返回popupWindow对象
* @return
*/
public static PopupWindow getPopupWindow() {
return popupWindow;
}
}
代码粘贴完成,希望能对你有所帮助,如果有什么疑问或问题可以留言,大家一家交流……