private Activity mContext;
private PopupWindow popupWindow;
private View anchor;
private TextView tvContent;
private ImageView pop_tv_img;
private TextView pop_tv_title;
private ImageView pop_tv_imgtitle;
PoponDismissListener mDismissListener;
public PopupWindow getPopupWindow() {
return popupWindow;
}
public void setPopupWindow(PopupWindow popupWindow) {
this.popupWindow = popupWindow;
}
public PopupwindowTip(Activity context, View view, String msg,String msgtitle) {
this.mContext = context;
this.anchor = view;
initView(msg,msgtitle);
}
private void initView(String msg,String msgtitle) {
View view = LayoutInflater.from(mContext).inflate(
R.layout.popupwindow_tip, null);
// 设置内容
pop_tv_img = (ImageView) view.findViewById(R.id.pop_tv_img);
pop_tv_imgtitle = (ImageView) view.findViewById(R.id.pop_tv_imgtitle);
pop_tv_title = (TextView) view.findViewById(R.id.pop_tv_title);
tvContent = (TextView) view.findViewById(R.id.pop_tv_content);
tvContent.setText(" "+msg);
pop_tv_title.setText(msgtitle);
if(msgtitle.equals("保障")){
pop_tv_imgtitle.setBackgroundResource(R.drawable.baozhang);
}else if (msgtitle.equals("随时退")) {
pop_tv_imgtitle.setBackgroundResource(R.drawable.suishitui);
}else if(msgtitle.equals("写美丽经返现")){
pop_tv_imgtitle.setBackgroundResource(R.drawable.fanxian);
}
// 创建悬浮窗
popupWindow = new PopupWindow(view, LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT, true);
popupWindow.setTouchable(true);
popupWindow.setBackgroundDrawable(new BitmapDrawable());
backgroundAlpha(1f);
pop_tv_img.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
popupWindow.dismiss();
}
});
popupWindow.setOnDismissListener(new OnDismissListener() {
@Override
public void onDismiss() {
onDismissPop();
}
});
popupWindow.setTouchInterceptor(new OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
return false;
}
});
// popupWindow.setOnDismissListener(mDismissListener);
// 如果不设置PopupWindow的背景,无论是点击外部区域还是Back键都无法dismiss弹框
// 我觉得这里是API的一个bug
popupWindow.setBackgroundDrawable(new ColorDrawable(-000000));
// 设置在控件的上方显示
view.measure(MeasureSpec.UNSPECIFIED, MeasureSpec.UNSPECIFIED);
// int popupWidth = view.getMeasuredWidth();
int popupHeight = view.getMeasuredHeight();
int[] location = new int[2];
anchor.getLocationOnScreen(location);
// popupWindow.showAtLocation(anchor, Gravity.NO_GRAVITY, location[0],
// location[1] - popupHeight);
popupWindow.showAtLocation(view, Gravity.CENTER, Gravity.CENTER,
Gravity.CENTER);
}
public void popDismiss() {
if (popupWindow != null) {
popupWindow.dismiss();
popupWindow = null;
}
}
public void backgroundAlpha(float bgAlpha) {
// WindowManager.LayoutParams lp = mContext.getWindow().getAttributes();
// lp.alpha = bgAlpha; // 0.0-1.0
// mContext.getWindow().setAttributes(lp);
ColorDrawable cd = new ColorDrawable(0x000000);
popupWindow.setBackgroundDrawable(cd);
WindowManager.LayoutParams lps=mContext.getWindow().getAttributes();
lps.alpha = 0.4f;
mContext.getWindow().setAttributes(lps);
// popupWindow.setAnimationStyle(R.style.animation);
}
public void onDismissPop(){
WindowManager.LayoutParams lp=mContext.getWindow().getAttributes();
lp.alpha = 1f;
mContext.getWindow().setAttributes(lp);
}
class PoponDismissListener implements PopupWindow.OnDismissListener{
@Override
public void onDismiss() {
onDismiss();
}
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/bgdialog"
android:padding="5dp"
android:orientation="vertical" >
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:paddingLeft="8dp"
android:paddingRight="8dp"
>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:orientation="horizontal"
>
<ImageView
android:id="@+id/pop_tv_imgtitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:layout_gravity="center_vertical"
/>
<TextView
android:id="@+id/pop_tv_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="5dp"
android:layout_gravity="center_vertical"
android:background="@color/transparent"
android:gravity="center"
android:singleLine="true"
android:textSize="18sp"
android:textColor="@color/text_666"
android:text="提示标题" />
</LinearLayout>
<ImageView
android:id="@+id/pop_tv_img"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="5dp"
android:layout_alignParentRight="true"
android:src="@drawable/guanbi"
android:layout_gravity="center_vertical|right" />
</RelativeLayout>
<TextView
android:id="@+id/pop_tv_content"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="16sp"
android:textColor="@color/text_666"
android:background="@color/transparent"
android:gravity="center_vertical|left"
android:padding="8dp"
android:text="提示内容"
/>
</LinearLayout>