Android popupwindow usage details

This example shares the usage of Android popupwindow for your reference. The specific contents are as follows

1、 Basic usage

Generally, a new class inherits popupwindow. example

/**
 * popupwindow基本用法
 * Created by Administrator on 2015/11/25.
 */
public class DemoBasePop extends PopupWindow {
  private LinearLayout linear_layout;
  private TextView dbp_text;
  private Context context;
  public DemoBasePop(final Activity context) {
    super(context);
    this.context = context;
    View view = LayoutInflater.from(context).inflate(R.layout.demo_base_pop,null);

    setContentView(view);
    setWidth(ViewGroup.LayoutParams.MATCH_PARENT);
    setHeight(200);
//    setHeight(ViewGroup.LayoutParams.MATCH_PARENT);

    setFocusable(true);
    setBackgroundDrawable(new BitmapDrawable());
    setTouchable(true);
    setOutsideTouchable(true);
    setAnimationStyle(R.style.popwin_anim_style);
//    setAnimationStyle(0);   0是没有animation

    initView(view);

  }

  private void initView(View view) {
    dbp_text = (TextView) view.findViewById(R.id.dbp_text);
  }

}

Study the popupwindow source code, taking showasdropdown as an example

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

    registerForScrollChanged(anchor,xoff,yoff);

    mIsShowing = true;
    mIsDropdown = true;

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

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

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

    p.windowAnimations = computeAnimationResource();

    invokePopup(p);
  }

Line 11 creates WindowManager. Layoutparams. In line 12 preparepopup():

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.setBackgroundDrawable(mBackground);
      popupViewContainer.addView(mContentView,listParams);

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

If setbackgrounddrawable (New bitmapdrawable()) is made; If mbackground is not empty, popupviewcontainer will be used as mpopupview (i.e. content view). PopupViewContainer's dispatchKeyEvent handles the return key, and the dismiss () method is called after pressing the return key. Its ontouchevent handles touch events. Its source code is:

public boolean onTouchEvent(MotionEvent event) {
      final int x = (int) event.getX();
      final int y = (int) event.getY();
      <span style="font-family: 宋体; font-size: 9pt;">//点击外部隐藏</span>
      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);
      }
    }

After the system has done these processing, there comes a problem. What should we do if we want to listen for the physical return key. After reading the above process, we can think of

setBackgroundDrawable(null);然后通过设置view的key监听,监听到后做相应的处理。
view.setOnKeyListener(new View.OnKeyListener() {
      @Override
      public boolean onKey(View v,int keyCode,KeyEvent event) {
        if (event.getKeyCode() == KeyEvent.KEYCODE_BACK) {
          if (event.getAction() == KeyEvent.ACTION_DOWN
              && event.getRepeatCount() == 0) {
            outAnimator.start();
            return true;
          }
        }
        return false;
      }
    });

design sketch:

The above is the whole content of this article. I hope it will help you in your study, and I hope you will support us a lot.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>