温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

android中popupwindow怎么用

发布时间:2021-09-28 10:29:09 来源:亿速云 阅读:151 作者:小新 栏目:编程语言

这篇文章将为大家详细讲解有关android中popupwindow怎么用,小编觉得挺实用的,因此分享给大家做个参考,希望大家阅读完这篇文章后可以有所收获。

一、基本用法

一般做法,新建类继承popupwindow。例

/** * 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);  }}

研究下popupwindow源码,以showAsDropDown来讲

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, xoff, yoff));    if (mHeightMode < 0) p.height = mLastHeight = mHeightMode;    if (mWidthMode < 0) p.width = mLastWidth = mWidthMode;    p.windowAnimations = computeAnimationResource();    invokePopup(p);  }

第11行创建WindowManager.LayoutParams。第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;    }

如果做了setBackgroundDrawable(new BitmapDrawable());那么mBackground则不为空,则会用PopupViewContainer作为mPopupView(即内容view)。而PopupViewContainer的dispatchKeyEvent对返回键做了处理,按返回键后其中调用dismiss()方法。其onTouchEvent对触摸事件做了处理,其源码:

public boolean onTouchEvent(MotionEvent event) {      final int x = (int) event.getX();      final int y = (int) event.getY();      <span >//点击外部隐藏</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);      }    }

系统做了这些处理,随之而来一个问题,如果我们要监听物理返回键该怎么办。看了上面的过程,我们可以想到将

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;      }    });

关于“android中popupwindow怎么用”这篇文章就分享到这里了,希望以上内容可以对大家有一定的帮助,使各位可以学到更多知识,如果觉得文章不错,请把它分享出去让更多的人看到。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI