自定义弹窗PopWindow

本文介绍了一种自定义PopupWindow的方法,通过创建CustomPopupWindow类实现了弹窗的显示位置控制,包括显示在控件的下右方、下左方及下中方等。同时还提供了设置弹窗触摸、动画样式等功能。

项目中自定义弹框,以前写过,不过好些前的事情了,在网上看到一个源码,copy下来留作备份

public class CustomPopupWindow extends PopupWindow {
	private Activity activity;
	private View contentView;

	// 用于保存PopupWindows的宽度
	private int width;
	// 用于保存PopupWindows的高度
	private int height;

	public CustomPopupWindow(Activity activity) {
		super();
		this.activity = activity;
		this.initPopupWindow();
	}

	private void initPopupWindow() {
		// TODO Auto-generated method stub
		LayoutInflater inflater = (LayoutInflater) activity
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		this.contentView = inflater.inflate(R.layout.popupwindow_custom, null);
		this.setContentView(contentView);
		this.setWidth(ViewGroup.LayoutParams.WRAP_CONTENT);
		this.setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);
		this.setTouchable(true);
		this.setFocusable(true);
		this.setOutsideTouchable(true);
		this.setAnimationStyle(R.style.PopupAnimation);
		ColorDrawable background = new ColorDrawable(0x4f000000);
		this.setBackgroundDrawable(background);
		this.mandatorDraw();
	}

	private void mandatorDraw() {
		this.contentView.measure(View.MeasureSpec.UNSPECIFIED,
				View.MeasureSpec.UNSPECIFIED);
		this.width = this.contentView.getMeasuredWidth();
		this.height = this.contentView.getMeasuredHeight();
	}

	/* 显示在控件下右方 */
	public void showAtDropDownRight(View parent) {
		if (parent.getVisibility() == View.GONE) {
			this.showAtLocation(parent, 0, 0, 0);
		} else {
			int[] location = new int[2];
			parent.getLocationOnScreen(location);
			this.showAtLocation(parent, 0, location[0] + parent.getWidth()
					- this.getWidth(), location[1] + parent.getHeight());
		}
	}
	
	/**
     * 显示在控件的下左方
     *
     * @param parent parent
     */
    public void showAtDropDownLeft(View parent) {
        if (parent.getVisibility() == View.GONE) {
            this.showAtLocation(parent, 0, 0, 0);
        } else {
            // x y
            int[] location = new int[2];
            //获取在整个屏幕内的绝对坐标
            parent.getLocationOnScreen(location);
            this.showAtLocation(parent, 0, location[0], location[1] + parent.getHeight());
        }
    }

    /**
     * 显示在控件的下中方
     *
     * @param parent parent
     */
    public void showAtDropDownCenter(View parent) {
        if (parent.getVisibility() == View.GONE) {
            this.showAtLocation(parent, 0, 0, 0);
        } else {
            // x y
            int[] location = new int[2];
            //获取在整个屏幕内的绝对坐标
            parent.getLocationOnScreen(location);
            this.showAtLocation(parent, 0, location[0] / 2 + parent.getWidth() / 2 - this.width / 6, location[1] + parent.getHeight());
        }
    }
    
    public static class PopupWindowBuilder{
    	private static String activityHashCode;
    	private static CustomPopupWindow popupWindow;
    	public static PopupWindowBuilder ourInstance;
    	
    	 public static PopupWindowBuilder getInstance(Activity activity) {
             if (ourInstance == null) ourInstance = new PopupWindowBuilder();
             String hashCode = String.valueOf(activity.hashCode());
             /**
              * 不同一个Activity
              */
             if (!hashCode.equals(String.valueOf(activityHashCode))) {
                 activityHashCode = hashCode;
                 popupWindow = new CustomPopupWindow(activity);
             }
             return ourInstance;
         }

         public PopupWindowBuilder setTouchable(boolean touchable) {
             popupWindow.setTouchable(touchable);
             return this;
         }

         public PopupWindowBuilder setAnimationStyle(int animationStyle) {
             popupWindow.setAnimationStyle(animationStyle);
             return this;
         }

         public PopupWindowBuilder setBackgroundDrawable(Drawable background) {
             popupWindow.setBackgroundDrawable(background);
             return this;
         }

         public CustomPopupWindow getPopupWindow() {
             popupWindow.update();
             return popupWindow;
         }

     }
}

使用方法:

CustomPopupWindow popupWindow = new CustomPopupWindow(MainActivity.this);
				
popupWindow.showAtDropDownRight(ivShow);


转载于:https://my.oschina.net/reborn87/blog/601571

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值