PopWindow的使用

一.属性

//直接显示在参照View 的左下方
public void showAsDropDown(View anchor)
// 显示在参照View的左下方,可以通过xoff,yOff,来调节x,y方向的偏移
public void showAsDropDown(View anchor, int xoff, int off)

public void showAsDropDown(View anchor, int xoff, int yoff, int gravity)
//显示在指定位置,相对于整个屏幕的window而言,通过gravity调解显示在左、上、右、下、中. x,y调整两个方向的偏移
public void showAtLocation(View parent, int gravity, int x, int y) 

二.PopWindow的使用

private void init(final Context context) {
        View view = LayoutInflater.from(context).inflate(R.layout.bottom_dialog_layout, null);
        setWidth(ViewGroup.LayoutParams.MATCH_PARENT);
        int height= ScreenUtils.getScreenHeight()*2/3;
        setHeight(height);
        setContentView(view);
        initView(context, view);
        setBackgroundDrawable(new ColorDrawable(0x00000000));
        setOutsideTouchable(true);
        setFocusable(true);
        setAnimationStyle(R.style.PayPopupStyle);
        setOnDismissListener(new OnDismissListener() {
            @Override
            public void onDismiss() {
                WindowManager.LayoutParams param = ((Activity) context).getWindow().getAttributes();
                param.alpha = 1f;
                ((Activity) context).getWindow().setAttributes(param);
            }
        });
    }

 @Override
    public void showAtLocation(View parent, int gravity, int x, int y) {
        super.showAtLocation(parent, gravity, x, y);
        WindowManager.LayoutParams param = ((Activity) context).getWindow().getAttributes();
        param.alpha = 0.7f;
        ((Activity) context).getWindow().setAttributes(param);

三.开源插件

封装了PopWindow,几行代码搞定PopWindow

https://github.com/pinguo-zhouwei/CustomPopwindow

你可能感兴趣的:(Android基础)