http://www.jcodecraeer.com/a/anzhuokaifa/androidkaifa/2014/0702/1627.html
http://www.jb51.net/article/77870.htm
//弹出框popupwindow
private void initMyPopWindow(View v) {
View view = LayoutInflater.from(mContext).inflate(R.layout.item_popup_comment1, null, false);
TextView tvDelete = (TextView) view.findViewById(R.id.tv_delete);
TextView tvCancel = (TextView) view.findViewById(R.id.tv_cancel);
//1.构造一个PopupWindow,参数依次是加载的View,宽高
final PopupWindow popWindow = new PopupWindow(view,
ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT, true);
//popWindow.setAnimationStyle(R.anim.anim_pop); //设置加载动画
//这些为了点击非PopupWindow区域,PopupWindow会消失的,如果没有下面的
//代码的话,你会发现,当你把PopupWindow显示出来了,无论你按多少次后退键
//PopupWindow并不会关闭,而且退不出程序,加上下述代码可以解决这个问题
popWindow.setTouchable(true);
popWindow.setTouchInterceptor(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
return false;
// 这里如果返回true的话,touch事件将被拦截
// 拦截后 PopupWindow的onTouchEvent不被调用,这样点击外部区域无法dismiss
}
});
popWindow.setBackgroundDrawable(new ColorDrawable(0x00000000));//要为popWindow设置一个背景才有效
//pop设置弹出时背景半透明及取消恢复透明度
setBackgroundAlpha(0.5f);
popWindow.setOnDismissListener(new PopupWindow.OnDismissListener() {
@Override
public void onDismiss() {
setBackgroundAlpha(1.0f);
}
});
//设置popupWindow显示的位置,参数依次是参照View,x轴的偏移量,y轴的偏移量
//popWindow.showAsDropDown(v, -153, 10);
popWindow.showAtLocation(activityRootView, Gravity.BOTTOM, 0, 0);
// TODO: 26/6/16 设置popupWindow里的按钮的事件
tvDelete.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO: 26/6/16 删除评论
popWindow.dismiss();
}
});
tvCancel.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
popWindow.dismiss();
}
});
}
设置背景透明度的方法
/**
* 设置添加屏幕的背景透明度
*
* @param bgAlpha 屏幕透明度0.0-1.0 1表示完全不透明
*/
public void setBackgroundAlpha(float bgAlpha) {
WindowManager.LayoutParams lp = ((Activity) mContext).getWindow()
.getAttributes();
lp.alpha = bgAlpha;
((Activity) mContext).getWindow().setAttributes(lp);
}