91超碰碰碰碰久久久久久综合_超碰av人澡人澡人澡人澡人掠_国产黄大片在线观看画质优化_txt小说免费全本

溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

在android使用PopupWindow時出現返回鍵消失如何解決

發布時間:2020-11-24 16:23:34 來源:億速云 閱讀:304 作者:Leah 欄目:移動開發

在android使用PopupWindow時出現返回鍵消失如何解決?針對這個問題,這篇文章詳細介紹了相對應的分析和解答,希望可以幫助更多想解決這個問題的小伙伴找到更簡單易行的方法。

preparePopup方法。

 public void showAsDropDown(View anchor, int xoff, int yoff, int gravity) {
    if (isShowing() || mContentView == null) {
      return;
    }

    registerForScrollChanged(anchor, xoff, yoff, gravity);

    mIsShowing = true;
    mIsDropdown = true;

    WindowManager.LayoutParams p = createPopupLayout(anchor.getWindowToken());
    preparePopup(p);

    updateAboveAnchor(findDropDownPosition(anchor, p, xoff, yoff, gravity));

    if (mHeightMode < 0) p.height = mLastHeight = mHeightMode;
    if (mWidthMode < 0) p.width = mLastWidth = mWidthMode;

    p.windowAnimations = computeAnimationResource();

    invokePopup(p);
 }

再看preparePopup方法

  /**
   * <p>Prepare the popup by embedding in into a new ViewGroup if the
   * background drawable is not null. If embedding is required, the layout
   * parameters' height is modified to take into account the background's
   * padding.</p>
   *
   * @param p the layout parameters of the popup's content view
   */
  private void preparePopup(WindowManager.LayoutParams p) {
    if (mContentView == null || mContext == null || mWindowManager == null) {
      throw new IllegalStateException("You must specify a valid content view by "
          + "calling setContentView() before attempting to show the popup.");
    }

    if (mBackground != null) {
      final ViewGroup.LayoutParams layoutParams = mContentView.getLayoutParams();
      int height = ViewGroup.LayoutParams.MATCH_PARENT;
      if (layoutParams != null &&
          layoutParams.height == ViewGroup.LayoutParams.WRAP_CONTENT) {
        height = ViewGroup.LayoutParams.WRAP_CONTENT;
      }

      // when a background is available, we embed the content view
      // within another view that owns the background drawable
      PopupViewContainer popupViewContainer = new PopupViewContainer(mContext);
      PopupViewContainer.LayoutParams listParams = new PopupViewContainer.LayoutParams(
          ViewGroup.LayoutParams.MATCH_PARENT, height
      );
      popupViewContainer.setBackground(mBackground);
      popupViewContainer.addView(mContentView, listParams);

      mPopupView = popupViewContainer;
    } else {
      mPopupView = mContentView;
    }

    mPopupView.setElevation(mElevation);
    mPopupViewInitialLayoutDirectionInherited =
        (mPopupView.getRawLayoutDirection() == View.LAYOUT_DIRECTION_INHERIT);
    mPopupWidth = p.width;
    mPopupHeight = p.height;
  }

上面可以看到mBackground不為空的時候,會PopupViewContainer作為mContentView的Parent,下面看看PopupViewContainer到底干了什么

  private class PopupViewContainer extends FrameLayout {
    private static final String TAG = "PopupWindow.PopupViewContainer";

    public PopupViewContainer(Context context) {
      super(context);
    }

    @Override
    protected int[] onCreateDrawableState(int extraSpace) {
      if (mAboveAnchor) {
        // 1 more needed for the above anchor state
        final int[] drawableState = super.onCreateDrawableState(extraSpace + 1);
        View.mergeDrawableStates(drawableState, ABOVE_ANCHOR_STATE_SET);
        return drawableState;
      } else {
        return super.onCreateDrawableState(extraSpace);
      }
    }

    @Override
    public boolean dispatchKeyEvent(KeyEvent event) {  // 這個方法里面實現了返回鍵處理邏輯,會調用dismiss
      if (event.getKeyCode() == KeyEvent.KEYCODE_BACK) {
        if (getKeyDispatcherState() == null) {
          return super.dispatchKeyEvent(event);
        }

        if (event.getAction() == KeyEvent.ACTION_DOWN
            && event.getRepeatCount() == 0) {
          KeyEvent.DispatcherState state = getKeyDispatcherState();
          if (state != null) {
            state.startTracking(event, this);
          }
          return true;
        } else if (event.getAction() == KeyEvent.ACTION_UP) {
          KeyEvent.DispatcherState state = getKeyDispatcherState();
          if (state != null && state.isTracking(event) && !event.isCanceled()) {
            dismiss();
            return true;
          }
        }
        return super.dispatchKeyEvent(event);
      } else {
        return super.dispatchKeyEvent(event);
      }
    }

    @Override
    public boolean dispatchTouchEvent(MotionEvent ev) {
      if (mTouchInterceptor != null && mTouchInterceptor.onTouch(this, ev)) {
        return true;
      }
      return super.dispatchTouchEvent(ev);
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) { // 這個方法里面實現點擊消失邏輯
      final int x = (int) event.getX();
      final int y = (int) event.getY();
      
      if ((event.getAction() == MotionEvent.ACTION_DOWN)
          && ((x < 0) || (x >= getWidth()) || (y < 0) || (y >= getHeight()))) {
        dismiss();
        return true;
      } else if (event.getAction() == MotionEvent.ACTION_OUTSIDE) {
        dismiss();
        return true;
      } else {
        return super.onTouchEvent(event);
      }
    }

    @Override
    public void sendAccessibilityEvent(int eventType) {
      // clinets are interested in the content not the container, make it event source
      if (mContentView != null) {
        mContentView.sendAccessibilityEvent(eventType);
      } else {
        super.sendAccessibilityEvent(eventType);
      }
    }
  }

看到上面紅色部分的標注可以看出,這個內部類里面封裝了處理返回鍵退出和點擊外部退出的邏輯,但是這個類對象的構造過程中(preparePopup方法中)卻有個mBackground != null的條件才會創建

而mBackground對象在setBackgroundDrawable方法中被賦值,看到這里應該就明白一切了。

  /**
   * Specifies the background drawable for this popup window. The background
   * can be set to {@code null}.
   *
   * @param background the popup's background
   * @see #getBackground()
   * @attr ref android.R.styleable#PopupWindow_popupBackground
   */
  public void setBackgroundDrawable(Drawable background) {
    mBackground = background;
    // 省略其他的
  }

setBackgroundDrawable方法除了被外部調用,構造方法中也會調用,默認是從系統資源中取的

  /**
   * <p>Create a new, empty, non focusable popup window of dimension (0,0).</p>
   * 
   * <p>The popup does not provide a background.</p>
   */
  public PopupWindow(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
    mContext = context;
    mWindowManager = (WindowManager)context.getSystemService(Context.WINDOW_SERVICE);

    final TypedArray a = context.obtainStyledAttributes(
        attrs, R.styleable.PopupWindow, defStyleAttr, defStyleRes);
    final Drawable bg = a.getDrawable(R.styleable.PopupWindow_popupBackground);
    mElevation = a.getDimension(R.styleable.PopupWindow_popupElevation, 0);
    mOverlapAnchor = a.getBoolean(R.styleable.PopupWindow_overlapAnchor, false);

    final int animStyle = a.getResourceId(R.styleable.PopupWindow_popupAnimationStyle, -1);
    mAnimationStyle = animStyle == R.style.Animation_PopupWindow &#63; -1 : animStyle;

    a.recycle();

    setBackgroundDrawable(bg);
  }

有些版本沒有,android6.0版本preparePopup如下: 

  /**
   * Prepare the popup by embedding it into a new ViewGroup if the background
   * drawable is not null. If embedding is required, the layout parameters'
   * height is modified to take into account the background's padding.
   *
   * @param p the layout parameters of the popup's content view
   */
  private void preparePopup(WindowManager.LayoutParams p) {
    if (mContentView == null || mContext == null || mWindowManager == null) {
      throw new IllegalStateException("You must specify a valid content view by "
          + "calling setContentView() before attempting to show the popup.");
    }

    // The old decor view may be transitioning out. Make sure it finishes
    // and cleans up before we try to create another one.
    if (mDecorView != null) {
      mDecorView.cancelTransitions();
    }

    // When a background is available, we embed the content view within
    // another view that owns the background drawable.
    if (mBackground != null) {
      mBackgroundView = createBackgroundView(mContentView);
      mBackgroundView.setBackground(mBackground);
    } else {
      mBackgroundView = mContentView;
    }

    mDecorView = createDecorView(mBackgroundView);

    // The background owner should be elevated so that it casts a shadow.
    mBackgroundView.setElevation(mElevation);

    // We may wrap that in another view, so we'll need to manually specify
    // the surface insets.
    final int surfaceInset = (int) Math.ceil(mBackgroundView.getZ() * 2);
    p.surfaceInsets.set(surfaceInset, surfaceInset, surfaceInset, surfaceInset);
    p.hasManualSurfaceInsets = true;

    mPopupViewInitialLayoutDirectionInherited =
        (mContentView.getRawLayoutDirection() == View.LAYOUT_DIRECTION_INHERIT);
    mPopupWidth = p.width;
    mPopupHeight = p.height;
  }

這里實現返回鍵監聽的代碼是mDecorView = createDecorView(mBackgroundView),這個并沒有受到那個mBackground變量的控制

關于在android使用PopupWindow時出現返回鍵消失如何解決問題的解答就分享到這里了,希望以上內容可以對大家有一定的幫助,如果你還有很多疑惑沒有解開,可以關注億速云行業資訊頻道了解更多相關知識。

向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

河池市| 和硕县| 徐州市| 阿鲁科尔沁旗| 郑州市| 新平| 朝阳区| 准格尔旗| 乳源| 嘉黎县| 临清市| 阿巴嘎旗| 麻栗坡县| 潜山县| 定襄县| 墨竹工卡县| 炉霍县| 灵宝市| 贞丰县| 兴化市| 武汉市| 宁南县| 家居| 潞城市| 阜宁县| 竹溪县| 陇川县| 安康市| 孟村| 军事| 津南区| 阿拉尔市| 隆林| 盐源县| 新乡市| 本溪市| 丰原市| 从江县| 伊金霍洛旗| 苏州市| 广安市|