砖垛之PopupWindow

一、PopupWindow弹出后,当里面有editext时,要弹出键盘,这时键盘会遮挡pop
解决;

popupWindow.setSoftInputMode(PopupWindow.INPUT_METHOD_NEEDED); 
popupWindow.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE); 

注意;这两个设置的顺序不能变,并且要在设置showAtLocation之前设置
二、封装

/**
 * description: pop工具类
 * autour: mo
 * date: 2017/7/28 0028 15:15
 */
public abstract class UtilPop {
    public interface PopCallBack {
        void getData(PopupWindow window, String selectId, int position);
    }

    /**
     * @param context      上下文
     * @param layoutId     布局id
     * @param atLocationId 显示位置id
     * @param hight        高度状态 0 自适应 1 胀满
     * @param upOrDown     0从下往上弹,1 从上往下弹
     */
    public UtilPop(Activity context, int layoutId, int atLocationId, int hight, int upOrDown) {
        View v = ((LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE)).inflate(layoutId, null);//填充视图
        PopupWindow window;
        if (hight == 0) {
            window = new CustomPopupWindow(v, WindowManager.LayoutParams.MATCH_PARENT, WindowManager.LayoutParams.WRAP_CONTENT);//创建window
        } else if (hight == 1) {
            window = new CustomPopupWindow(v, WindowManager.LayoutParams.MATCH_PARENT, WindowManager.LayoutParams.MATCH_PARENT);//创建window
            //遮住状态栏
             fitPopupWindowOverStatusBar(window, true);
        } else {
            window = new CustomPopupWindow(v, WindowManager.LayoutParams.MATCH_PARENT, hight);//创建window
        }


        window.setFocusable(true); // 设置popWindow弹出窗体可点击,这句话必须添加,并且是true
        window.setBackgroundDrawable(new ColorDrawable(0xb000000)); // 实例化一个ColorDrawable颜色为半透明

        if (upOrDown == 0) {
            //解决PopupWindow含有输入框时,点击输入框,软键盘可能会挡住PopupWindow 顺序不能变
            window.setSoftInputMode(PopupWindow.INPUT_METHOD_NEEDED);
            window.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);
            window.setAnimationStyle(R.style.mypopwindow_anim_style); // 设置popWindow的显示和消失动画
            window.showAtLocation(context.findViewById(atLocationId), Gravity.BOTTOM | Gravity.CENTER_HORIZONTAL, 0, 0); // 在底部显示

        } else if (upOrDown == 1) {
            window.setAnimationStyle(R.style.pop_top_anim_style); // 设置popWindow的显示和消失动画
            window.showAsDropDown(context.findViewById(atLocationId)); // 在底部显示
        }
        // 设置背景颜色变暗
        final Window bgWindow = context.getWindow();
        WindowManager.LayoutParams lp = bgWindow.getAttributes();
//        lp.alpha = 0.7f;
        lp.alpha = 0.95f;
        context.getWindow().setAttributes(lp);
        window.setOnDismissListener(new PopupWindow.OnDismissListener() {

            @Override
            public void onDismiss() {
                WindowManager.LayoutParams lp = bgWindow.getAttributes();
                lp.alpha = 1f;
                bgWindow.setAttributes(lp);
            }
        });

        doWhat(v, window, bgWindow);

    }

    protected abstract void doWhat(View v, PopupWindow window, Window bgWindow);



   public static void fitPopupWindowOverStatusBar(PopupWindow mPopupWindow, boolean needFullScreen) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            try {
                Field mLayoutInScreen = PopupWindow.class.getDeclaredField("mLayoutInScreen");
                mLayoutInScreen.setAccessible(needFullScreen);
                mLayoutInScreen.set(mPopupWindow, needFullScreen);
            } catch (NoSuchFieldException e) {
                e.printStackTrace();
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            }
        }
    }
}

三、自定义解决在7.0及其他版本中无法正常在设置的位置显示


public class CustomPopupWindow extends PopupWindow {

    public CustomPopupWindow(View contentView, int width, int height) {
        this(contentView, width, height, false);
    }
    public CustomPopupWindow(View contentView, int width, int height, boolean focusable) {
        super(contentView, width, height, focusable);
    }

    @Override
    public void showAsDropDown(View anchor) {
        if (Build.VERSION.SDK_INT == 24) {
            Rect rect = new Rect();
            anchor.getGlobalVisibleRect(rect);
            int h = anchor.getResources().getDisplayMetrics().heightPixels - rect.bottom;
            setHeight(h);
        }
        super.showAsDropDown(anchor);
    }

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值