Android PopupWindow彈出窗口的完美實現(實現彈出背景變暗效果)

最近嘗試使用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);
      }
    }
  }
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章