此篇博客實現的功能是:點擊界面中的圖片,跳出一個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,數值越大透明度越高。
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持VEVB武林網。
新聞熱點
疑難解答