仿微信右上角弹出PopupWindow

import android.app.Activity;
import android.content.Context;
import android.graphics.drawable.ColorDrawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.widget.PopupWindow;

/**
 * 仿微信右上角弹出PopupWindow CREATE BY WANGJINCHENG IN 2016-08-29
 */

public class MyPopWindow extends PopupWindow implements OnClickListener
{
	private View conentView;

	public MyPopWindow(final Activity context)
	{
		LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		conentView = inflater.inflate(R.layout.begin_order_popup, null);

		// DisplayMetrics dm = new DisplayMetrics();// 取得窗口属性
		// context.getWindowManager().getDefaultDisplay().getMetrics(dm);
		// int screenHeight = dm.heightPixels;// 窗口高度
		// int screenWidth = dm.widthPixels;// 窗口的宽度

		this.setContentView(conentView);// 设置PopupWindow的View
		this.setWidth(LayoutParams.WRAP_CONTENT);// 设置PopupWindow弹出窗体的宽
		this.setHeight(LayoutParams.WRAP_CONTENT);// 设置PopupWindow弹出窗体的高
		this.setFocusable(true);// 设置PopupWindow弹出窗体可点击
		this.setOutsideTouchable(true);// 设置PopupWindow其他空白区域可点击
		this.update();// 刷新状态
		ColorDrawable dw = new ColorDrawable(0000000000);// 实例化一个ColorDrawable颜色为半透明
		this.setBackgroundDrawable(dw);// 点back键和其他地方使其消失,设置了这个才能触发OnDismisslistener,设置其他控件变化等操作

		conentView.findViewById(R.id.begin_cancel_order).setOnClickListener(this);
		conentView.findViewById(R.id.begin_assignment_order).setOnClickListener(this);
		conentView.findViewById(R.id.begin_contact).setOnClickListener(this);
		conentView.findViewById(R.id.begin_same_driver).setOnClickListener(this);
	}

	/**
	 * 显示PopupWindow
	 * 
	 * @param parent
	 */
	public void showPopupWindow(View parent)
	{
		if (!this.isShowing())
		{
			// 以下拉方式显示popupwindow
			this.showAsDropDown(parent, parent.getLayoutParams().width / 2, 0);
		} else
		{
			this.dismiss();
		}
	}

	@Override
	public void onClick(View v)
	{
		switch (v.getId())
		{
		case R.id.begin_cancel_order:
			MyPopWindow.this.dismiss();
			break;
		case R.id.begin_assignment_order:
			MyPopWindow.this.dismiss();
			break;
		case R.id.begin_contact:
			MyPopWindow.this.dismiss();
			break;
		case R.id.begin_same_driver:
			MyPopWindow.this.dismiss();
			break;
		default:
			break;
		}
	}
}


在main里面点击的按钮写这两行
MyPopWindow popWindow = new MyPopWindow(this);
popWindow.showPopupWindow(findViewById(R.id.begin_menu));


下载地址:    http://download.csdn.net/download/chinaredking/9614085

你可能感兴趣的:(android)