最近嘗試使用popupWindow實現背景變暗效果,自己優化了一下,並封裝成一個可以呼叫的方法,預設實現彈出視窗顯示在傳入view的下方,以下程式碼有詳細註釋,有問題可以留言

展示效果如下:
在螢幕中間顯示的popWindow也可以用dialog替代

程式碼展示

佈局中

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

  <TextView
      android:id="@+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"
      />

  <TextView
      android:id="@+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);
      }
    }
  }