此篇博客實現的功能是:點擊界面中的圖片,跳出一個PopupWindow,PopupWindow中含有相應的文字和圖標,並且在顯示PopupWindow的時候,背景爲半透明。
看圖描述:點擊加號,跳出PopupWindow,其中包含三個圖片,點擊叉號PopupWindow消失;當PopupWindow顯示的時候,背景爲半透明
顯示PopupWindow的代碼
private void showPopupWindow() {
View view = (LinearLayout) getLayoutInflater().inflate(R.layout.popup_window_layout, null);
ImageView ivP = (ImageView) view.findViewById(R.id.ivP);
ImageView ivX = (ImageView) view.findViewById(R.id.ivX);
ImageView ivClose = (ImageView) view.findViewById(R.id.ivClose);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT);
ivP.setLayoutParams(params);
ivX.setLayoutParams(params);
ivClose.setLayoutParams(params);
ivClose.setOnClickListener(this);
popupWindow = new PopupWindow(WindowManager.LayoutParams.WRAP_CONTENT, WindowManager.LayoutParams.WRAP_CONTENT);
popupWindow.setContentView(view);
popupWindow.setFocusable(true);
popupWindow.setTouchable(true);
popupWindow.setOutsideTouchable(false);
popupWindow.showAsDropDown(ivAdd, 0, 0);
backgroundAlpha(0.4f);
}
private void backgroundAlpha(float f) {
WindowManager.LayoutParams lp =getWindow().getAttributes();
lp.alpha = f;
getWindow().setAttributes(lp);
}
backgroundAlpha()方法用於設置PopupWindow顯示後的背景半透明,參數 f 的範圍是0.0~1.0,數值越大透明度越高。