PopupWindow常见问题

来源:互联网 发布:python算t统计量 编辑:程序博客网 时间:2024/05/20 14:19
  1. popup点击外部或返回键取消
     //popupwindow点击外部和返回键消失mPopup.setFocusable(true);//如果没有这句返回键后窗体泄露ColorDrawable dw = new ColorDrawable(0x00000000);mPopup.setBackgroundDrawable(dw);mPopup.setOutsideTouchable(true);//外部有焦点
  2. showAsDropDown(v, offsetX, offsetY)设置popup的位置   offsetX失效;原因:popup默认是左下角对其,如果控件是靠右边的,offsetX的设置是关键点。给出代码:
       /**     * 显示popup     * @param view 在这个view下显示     * @param activity 在这个activity上显示pop     */    @SuppressLint("NewApi")public static void showPopup(View view, Activity activity) {LayoutInflater inflater = LayoutInflater.from(activity);View popupView = inflater.inflate(R.layout.pop_clear_waitapply, null);PopupWindow mPopup = new PopupWindow(popupView, LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);//mPopup.setBackgroundDrawable(new BitmapDrawable());mPopup.setFocusable(true);ColorDrawable dw = new ColorDrawable(0x00000000);mPopup.setBackgroundDrawable(dw);mPopup.setOutsideTouchable(true);popupView.measure(View.MeasureSpec.UNSPECIFIED, View.MeasureSpec.UNSPECIFIED);int xOffset = -(popupView.getMeasuredWidth() - view.getWidth());mPopup.showAsDropDown(view, xOffset, -14);//popup默认左下角对齐//mPopup.showAtLocation(view, Gravity.NO_GRAVITY, -40, 10);}

1 0
原创粉丝点击