PopupWindow彈出框且背景變暗

public void showPopupWindow(View view) {

// 一個自定義的佈局,作爲顯示的內容
View contentView = LayoutInflater.from(this).inflate(
R.layout.xxxx, null);

final PopupWindow popupWindow = new PopupWindow(contentView,
LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT, true);//剛開始沒注意調整寬度和高度,佈局總不對


ImageView detail = (ImageView) contentView.findViewById(R.id.xxxx);
detail.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View v) {
popupWindow.dismiss(); 

}
});

ImageView iKnow = (ImageView) contentView.findViewById(R.id.xxxx);
iKnow.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View v) {
popupWindow.dismiss(); 
}
});

popupWindow.setTouchable(true);

popupWindow.setTouchInterceptor(new OnTouchListener() {


@Override
public boolean onTouch(View v, MotionEvent event) {
// 這裏如果返回true的話,touch事件將被攔截
// 攔截後 PopupWindow的onTouchEvent不被調用,這樣點擊外部區域無法dismiss
return false;
}
});


// 如果不設置PopupWindow的背景,無論是點擊外部區域還是Back鍵都無法dismiss彈框
popupWindow.setBackgroundDrawable(getResources().getDrawable(
R.drawable.xxxx));
//如果沒有背景,則可以用
//popupWindow.setBackgroundDrawable(new BitmapDrawable());
//popupWindow.setOutsideTouchable(true);

//pop window居中顯示
popupWindow.showAtLocation(view, Gravity.CENTER, 0, 0);


ColorDrawable cd = new ColorDrawable(0x000000);
popupWindow.setBackgroundDrawable(cd);
// 產生背景變暗效果
WindowManager.LayoutParams lp = getWindow().getAttributes();
lp.alpha = 0.4f;
getWindow().setAttributes(lp);
popupWindow.setOnDismissListener(new OnDismissListener() {


// 在dismiss中恢復透明度
public void onDismiss() {
WindowManager.LayoutParams lp = getWindow().getAttributes();
lp.alpha = 1f;
getWindow().setAttributes(lp);
}
});


}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章