Android进阶篇-PopupWindow的使用

/**
 * @author gongchaobin
 *
 * 自定义popuwindow
 * @version 2012-12-12
 */
public class MyPopuWindow {
	private LayoutInflater mInflater;
	private PopupWindow mPopupWindow;
	private View mView;
	private Button mBtn;
	
	/**
	 * @param context 上下文
	 * @param resId layout资源ID
	 * @param width popuwindow的宽
	 * @param height popuwindw的高
	 * @param view 显示位置相对的view 
	 */
	public MyPopuWindow(Context context,int resId,int width,int height,OnClickListener clickListener,View view){
		mInflater = LayoutInflater.from(context);
		mView = mInflater.inflate(resId, null);
		mPopupWindow = new PopupWindow(mView, width, height);
		
		mPopupWindow.setBackgroundDrawable(new BitmapDrawable());
        /*设置触摸外面时消失*/  
		mPopupWindow.setOutsideTouchable(true);
//        /*设置系统动画*/  
//        popupWindow.setAnimationStyle(R.style.PopupAnimation);
		mPopupWindow.update();
		mPopupWindow.setTouchable(true);
	      /*设置点击menu以外其他地方以及返回键退出*/  
		mPopupWindow.setFocusable(true);
		mPopupWindow.showAsDropDown(view);
		
		mBtn = (Button) mView.findViewById(R.id.button1);
		mBtn.setOnClickListener(clickListener);
		
	     /** 
         * 解决再次点击MENU键无反应问题   
         */
		mView.setFocusableInTouchMode(true);
		mView.setOnKeyListener(new OnKeyListener() {  
            public boolean onKey(View v, int keyCode, KeyEvent event) {  
                // TODO Auto-generated method stub  
                if ((keyCode == KeyEvent.KEYCODE_MENU)&&(mPopupWindow.isShowing())) { 
                	mPopupWindow.dismiss();
                    return true;  
                }else if(event.getAction() == KeyEvent.ACTION_DOWN && keyCode == KeyEvent.KEYCODE_BACK){
                	mPopupWindow.dismiss();
                    return false;
                }
                return false;  
            }  
        });
	}
}

  

转载于:https://www.cnblogs.com/gongcb/archive/2012/05/23/2514830.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值