Android PopupWindow弹出窗口的完美实现(实现弹出背景变暗效果)

最近尝试使用popupWindow实现背景变暗效果,自己优化了一下,并封装成一个可以调用的方法,默认实现弹出窗口显示在传入view的下方,以下代码有详细注释,有问题可以留言

展示效果如下:
Android PopupWindow弹出窗口的完美实现(实现弹出背景变暗效果)_第1张图片

代码展示

布局中

"http://schemas.android.com/apk/res/android"
    android:layout_width="246dp"
    android:layout_height="132dp"
    android:layout_gravity="center"
    android:gravity="center"
    >

  "@+id/tv_pop_web"
      android:layout_width="wrap_content"
      android:layout_height="32dp"
      android:background="@color/nba_white"
      android:clickable="true"
      android:drawableLeft="@drawable/icon_pop_web"
      android:drawablePadding="11dp"
      android:gravity="center_vertical"
      android:text="观看网页版"
      android:textColor="@color/nba_black"
      android:textSize="16dp"
      />

  "@+id/tv_pop_phone"
      android:layout_width="wrap_content"
      android:layout_height="32dp"
      android:layout_below="@+id/tv_pop_web"
      android:layout_marginTop="16dp"
      android:background="@color/nba_white"
      android:clickable="true"
      android:drawableLeft="@drawable/icon_pop_phone"
      android:drawablePadding="11dp"
      android:gravity="center_vertical"
      android:text="打开腾讯体育观看"
      android:textColor="@color/nba_black"
      android:textSize="16dp"
      />


</RelativeLayout>
/*
  * 弹出选择直播方式的弹框
  * View v :显示在那个父view内
  * int convertViewResource :要填充到popupWindow中的布局文件id
  * int drawbelResource :int drawbelResource
  * */
  private void showPopWindow(View parentView, int convertViewResource, int drawbelResource,
      String matchId) {
    //创建一个popUpWindow
    View popLayout = LayoutInflater.from(this).inflate(convertViewResource, null);
    //给popUpWindow内的空间设置点击事件
    popLayout.findViewById(R.id.tv_pop_web).setOnClickListener(new View.OnClickListener() {
      @Override
      public void onClick(View v) {
        if (mPopupWindow.isShowing()) {
          mPopupWindow.dismiss();
        }
        loadHtml(mQqSportWebUrl);
      }
    });
    popLayout.findViewById(R.id.tv_pop_phone).setOnClickListener(new View.OnClickListener() {
      @Override
      public void onClick(View v) {
        if (mPopupWindow.isShowing()) {
          mPopupWindow.dismiss();
        }
        loadHtml(String.format(Api.SKIP_TO_QQSPORT_LIVE_VIDEO, matchId));
      }
    });
    if (mPopupWindow == null) {
      //实例化一个popupWindow
      mPopupWindow =
          new PopupWindow(popLayout, DensityUtil.dp2px(this, 246), DensityUtil.dp2px(this, 132));
      //产生背景变暗效果
      WindowManager.LayoutParams lp = getWindow().getAttributes();
      lp.alpha = 0.4f;
      getWindow().setAttributes(lp);
      //点击外面popupWindow消失
      mPopupWindow.setOutsideTouchable(true);
      //popupWindow获取焦点
      mPopupWindow.setFocusable(true);
      //popupWindow设置背景图
      Drawable drawable = getResources().getDrawable(drawbelResource);
      mPopupWindow.setBackgroundDrawable(drawable);
      //popupWindow设置开场动画风格
      //popupWindow.setAnimationStyle(R.style.popupWindow_anim);
      //刷新popupWindow
      //popupWindow.update();

      //设置popupWindow消失时的监听
      mPopupWindow.setOnDismissListener(new PopupWindow.OnDismissListener() {
        //在dismiss中恢复透明度
        public void onDismiss() {
          WindowManager.LayoutParams lp = getWindow().getAttributes();
          lp.alpha = 1f;
          getWindow().setAttributes(lp);
        }
      });
      mPopupWindow.showAtLocation(parentView, Gravity.CENTER, 0, 0);
    } else {
      //如果popupWindow正在显示,接下来隐藏
      if (mPopupWindow.isShowing()) {
        mPopupWindow.dismiss();
      } else {
        //产生背景变暗效果
        WindowManager.LayoutParams lp = getWindow().getAttributes();
        lp.alpha = 0.4f;
        getWindow().setAttributes(lp);
        mPopupWindow.showAtLocation(parentView, Gravity.CENTER, 0, 0);
      }
    }
  }

你可能感兴趣的:(Android PopupWindow弹出窗口的完美实现(实现弹出背景变暗效果))