仿微信右上角弹出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
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值