服务器之家:专注于VPS、云服务器配置技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|Java教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|VB|R语言|JavaScript|易语言|vb.net|

服务器之家 - 编程语言 - Android - android popupwindow用法详解

android popupwindow用法详解

2022-11-03 15:53我的心里只有你 Android

这篇文章主要为大家详细介绍了android popupwindow用法,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

本文实例为大家分享了android popupwindow的用法,供大家参考,具体内容如下

一、基本用法

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

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
/**
 * 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来讲

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
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()中:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
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对触摸事件做了处理,其源码:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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);
      }
    }

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

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
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用法详解

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。

原文链接:https://blog.csdn.net/y444400/article/details/50301383

延伸 · 阅读

精彩推荐