PopupWindow

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);
            }
        });

    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值