自定義PopupWindow的時候發現一個問題:系統狀態欄沒有被遮蓋,給人的感受不是很友好。java
解決方案:ide
關鍵代碼:動畫
// 設置SelectPicPopupWindow彈出窗體的高 this.setHeight(ScreenUtil.getScreenHeight(context));//屏幕的高
以前這裏設置的是:this
this.setHeight(WindowManager.LayoutParams.WRAP_CONTENT);//屏幕的高
可是sdk>21,依舊沒有被遮蓋,加上code
//sdk > 21 解決 標題欄沒有辦法遮罩的問題 this.setClippingEnabled(false);
詳細代碼:ip
public class ReportPopuWindow extends PopupWindow implements View.OnClickListener { TextView news_delete, new_cancel; private Context context; private View mMenuView; private LayoutInflater inflater; private int position = 0; //位置 private Handler del_handler; private Bundle bundle; public ReportPopuWindow(final Context context,Handler del_handler, Bundle bundle) {//, int position, super(context); this.context = context; // this.position = position; this.del_handler = del_handler; this.bundle = bundle; inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); mMenuView = inflater.inflate(R.layout.dialog_news_delete, null); news_delete = (TextView) mMenuView.findViewById(R.id.news_delete); new_cancel = (TextView) mMenuView.findViewById(R.id.new_cancel); news_delete.setText(context.getString(R.string.report));//舉報 news_delete.setOnClickListener(this); new_cancel.setOnClickListener(this); this.setContentView(mMenuView); //sdk > 21 解決 標題欄沒有辦法遮罩的問題 this.setClippingEnabled(false); // 設置SelectPicPopupWindow彈出窗體的寬 this.setWidth(WindowManager.LayoutParams.MATCH_PARENT); // 設置SelectPicPopupWindow彈出窗體的高 this.setHeight(ScreenUtil.getScreenHeight(context));//屏幕的高 // 設置SelectPicPopupWindow彈出窗體可點擊 this.setFocusable(true); // 設置SelectPicPopupWindow彈出窗體動畫效果 this.setAnimationStyle(R.style.AnimationWindow); // 實例化一個ColorDrawable顏色爲半透明 ColorDrawable dw = new ColorDrawable(0x80000000); // 設置SelectPicPopupWindow彈出窗體的背景 this.setBackgroundDrawable(dw); // mMenuView添加OnTouchListener監聽判斷獲取觸屏位置若是在選擇框外面則銷燬彈出框 mMenuView.setOnTouchListener(new View.OnTouchListener() { public boolean onTouch(View v, MotionEvent event) { int height = mMenuView.findViewById(R.id.pop_layout2).getTop(); int y = (int) event.getY(); if (event.getAction() == MotionEvent.ACTION_UP) { if (y < height) { dismiss(); } } return true; } }); } public ReportPopuWindow(final Context context, final Handler del_handler, final Bundle bundle, int delete) {//, int position, super(context); this.context = context; // this.position = position; this.del_handler = del_handler; this.bundle = bundle; inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); mMenuView = inflater.inflate(R.layout.dialog_news_delete, null); news_delete = (TextView) mMenuView.findViewById(R.id.news_delete); new_cancel = (TextView) mMenuView.findViewById(R.id.new_cancel); news_delete.setText(context.getString(R.string.delete));//刪除 if (delete==1){ news_delete.setOnClickListener(this); }else { news_delete.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Message msg = new Message(); msg.what = 9; msg.setData(bundle); del_handler.sendMessage(msg); ReportPopuWindow.this.dismiss(); } }); } new_cancel.setOnClickListener(this); this.setContentView(mMenuView); // 設置SelectPicPopupWindow彈出窗體的寬 this.setWidth(WindowManager.LayoutParams.MATCH_PARENT); // 設置SelectPicPopupWindow彈出窗體的高 this.setHeight(WindowManager.LayoutParams.WRAP_CONTENT); // 設置SelectPicPopupWindow彈出窗體可點擊 this.setFocusable(true); // 設置SelectPicPopupWindow彈出窗體動畫效果 this.setAnimationStyle(R.style.AnimationWindow); // 實例化一個ColorDrawable顏色爲半透明 ColorDrawable dw = new ColorDrawable(0x80000000); // 設置SelectPicPopupWindow彈出窗體的背景 this.setBackgroundDrawable(dw); // mMenuView添加OnTouchListener監聽判斷獲取觸屏位置若是在選擇框外面則銷燬彈出框 mMenuView.setOnTouchListener(new View.OnTouchListener() { public boolean onTouch(View v, MotionEvent event) { int height = mMenuView.findViewById(R.id.pop_layout2).getTop(); int y = (int) event.getY(); if (event.getAction() == MotionEvent.ACTION_UP) { if (y < height) { dismiss(); } } return true; } }); } @Override public void onClick(View arg0) { // TODO Auto-generated method stub switch (arg0.getId()) { case R.id.news_delete: //舉報 Message msg = new Message(); msg.what = 10; msg.setData(bundle); del_handler.sendMessage(msg); ReportPopuWindow.this.dismiss(); break; case R.id.new_cancel: //取消 ReportPopuWindow.this.dismiss(); break; default: break; } } }