步骤如下:
- 写一个布局
- 创建也就是new一个popupwindow,传入大小(match,wrap或者具体大小)
- 调用set方法
- 若想要加上动画, style里面设定android:windowEnterAnimation和android:windowExitAnimation,然后执行第三步setStyle()
- showAtLocation在某个控件下面或者showAsDropDown相对于当前窗口;
-
private void initPayPopuptWindow() { popupWindow_view = LayoutInflater.from(getActivity()).inflate(R.layout.popupwindow, null); Button sure = (Button) popupWindow_view.findViewById(R.id.btnLogout); llGroup = (LinearLayout) popupWindow_view.findViewById(R.id.llGroup); sure.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { mPopupWindow.dismiss(); backgroundAlpha(1f); } }); mPopupWindow = new PopupWindow(popupWindow_view, WindowManager.LayoutParams.MATCH_PARENT, WindowManager.LayoutParams.WRAP_CONTENT, true); mPopupWindow.setTouchable(true);// 设置是否可被点击 mPopupWindow.setOutsideTouchable(true);// 若为true点击popupwindow外区域可以取消popupwindow mPopupWindow.setBackgroundDrawable(new BitmapDrawable(getResources(), (Bitmap) null));// 设置背// 设置动画效果 mPopupWindow.setAnimationStyle(R.style.PopupAnimation);//动画就是 平移 旋转 缩放那些 mPopupWindow.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);//设置不让虚拟键盘遮挡住popupwindow mPopupWindow.setOnDismissListener(new PopupWindow.OnDismissListener() { @Override// popupWindow的消失监听器 public void onDismiss() { backgroundAlpha(1f);// 设置屏幕的亮度,可以在弹出popupwindow的时候设置为0.6f;消失的时候在还原 } }); }
-
-
/** * 设置添加屏幕的背景透明度 */ public void backgroundAlpha(float bgAlpha) { WindowManager.LayoutParams lp = getActivity().getWindow().getAttributes(); lp.alpha = bgAlpha; //0.0-1.0 getActivity().getWindow().setAttributes(lp); } 显示方法:从屏幕底部弹出
mPopupWindow.showAtLocation(getActivity().findViewById(R.id.view), Gravity.BOTTOM, 0, 0); backgroundAlpha(0.6f);也可以设置为依附某一个控件,使用 showAsDropDown()
// 进入的动画
<set xmlns:android="http://schemas.android.com/apk/res/android" > <translate android:duration="250" android:fromYDelta="100.0%" android:toYDelta="0.0" /> </set> // 退出的动画
<set xmlns:android="http://schemas.android.com/apk/res/android" > <translate android:duration="250" android:fromYDelta="0.0" android:toYDelta="100%" />