PopupWindow

来源:互联网 发布:网络交流环境 编辑:程序博客网 时间:2024/05/20 06:50

PopupWindow显示位置、设置半透明及兼容华为设置半透明背景

  • popupWindow设置半透明背景

    /** * popupWindow设置半透明背景 * @param bgAlpha 透明值 0.0 - 1.0 */public void backgroundAlpha(float bgAlpha) {    WindowManager.LayoutParams lp = getActivity().getWindow().getAttributes();    lp.alpha = bgAlpha;     if (bgAlpha == 1) {        //不移除该Flag的话,在有视频的页面上的视频会出现黑屏的bug        getActivity().getWindow().clearFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);    } else {        //此行代码主要是解决在华为手机上半透明效果无效的bug        getActivity().getWindow().addFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);    }    getActivity().getWindow().setAttributes(lp);}
    • popupWindow从底部弹出
      findViewById(R.id.bttton).setOnClickListener(new View.OnClickListener() {        @Override        public void onClick(View view) {            initBottomPopupWindow();        }    });/** * 从底部直接弹出 */protected void initBottomPopupWindow() {    View popupWindowView = inflater.inflate(R.layout.popupwindow, null);    //内容,高度,宽度    WindowManager wm = (WindowManager) mActivity            .getSystemService(Context.WINDOW_SERVICE);    int width = (int) (wm.getDefaultDisplay().getHeight() * 0.5);    popupWindow = new PopupWindow(popupWindowView, RelativeLayout.LayoutParams.MATCH_PARENT,            width, true);    //动画效果    popupWindow.setAnimationStyle(R.style.AnimationBottomFade);    //菜单背景色    ColorDrawable dw = new ColorDrawable(0xffffffff);    popupWindow.setBackgroundDrawable(dw);    //显示位置    popupWindow.showAtLocation(inflater.inflate(R.layout.activity_main, null), Gravity.BOTTOM | Gravity.CENTER_HORIZONTAL, 0, 0);    //设置背景半透明    backgroundAlpha(0.5f);    //关闭事件    popupWindow.setOnDismissListener(new popupDismissListener());    popupWindowView.setOnTouchListener(new View.OnTouchListener() {        @Override        public boolean onTouch(View v, MotionEvent event) {            return false;        }    });}
  • 出来关闭的动画style

    <style name="AnimationBottomFade">    <item name="android:windowEnterAnimation">@anim/in_bottomtotop</item>    <item name="android:windowExitAnimation">@anim/out_toptobottom</item></style>
  • 显示动画in_bottomtotop.xml

<?xml version="1.0" encoding="utf-8"?><set xmlns:android="http://schemas.android.com/apk/res/android">    <translate        android:fromYDelta="100%"        android:toYDelta="0"        android:duration="500"/></set>
  • 关闭动画out_toptobottom.xml
<?xml version="1.0" encoding="utf-8"?><set xmlns:android="http://schemas.android.com/apk/res/android">    <translate        android:fromYDelta="0"        android:toYDelta="100%"</set>
  • 显示在ListView item上下
 ...... // listView长按 listView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {            @Override            public boolean onItemLongClick(AdapterView<?> adapterView, View view, int i, long l) {                showPopupWindow(view);                return true;            }        });/** * 显示popupwindow * @param parent listView item view */private void showPopupWindow(View parent) {        LayoutInflater layoutInflater = (LayoutInflater) getActivity().getSystemService(Context.LAYOUT_INFLATER_SERVICE);        View view = layoutInflater.inflate(R.layout.put_storage_item_particulars, null);        WindowManager wm = (WindowManager) getActivity()                .getSystemService(Context.WINDOW_SERVICE);        int width = (int) (wm.getDefaultDisplay().getWidth() * 0.75);       PopupWindow popupWindow = new PopupWindow(view, width, WindowManager.LayoutParams.WRAP_CONTENT);        popupWindow.setFocusable(true);        popupWindow.setOutsideTouchable(true);        // 这个是为了点击“返回Back”也能使其消失,并且并不会影响你的背景        ColorDrawable dw = new ColorDrawable(0xFFFFFFFF);        popupWindow.setBackgroundDrawable(dw);        WindowManager windowManager = (WindowManager) getActivity().getSystemService(Context.WINDOW_SERVICE);        int popupWidth = view.getMeasuredWidth();        int popupHeight = view.getMeasuredHeight();        int[] location = new int[2];        parent.getLocationOnScreen(location);        popupWindow.showAtLocation(parent, Gravity.NO_GRAVITY, (wm.getDefaultDisplay().getWidth() - width) / 2,                location[1] + parent.getHeight());        backgroundAlpha(0.5f);        popupWindow.setOnDismissListener(new PopupWindow.OnDismissListener() {            @Override            public void onDismiss() {                backgroundAlpha(1f);            }        });    }
0 0
原创粉丝点击