android popupwindow用法详解

 更新时间:2019年10月28日 08:59:11   作者:我的心里只有你  
这篇文章主要为大家详细介绍了android popupwindow用法,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

本文实例为大家分享了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 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);
      }
    }

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

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 使用压缩纹理的方案

    Android 使用压缩纹理的方案

    这篇文章主要介绍了Android 使用压缩纹理,本文介绍了什么是压缩纹理,以及加载压缩纹理的核心步骤,并在 Android OpenGLES 平台上实现了压缩纹理的显示,需要的朋友可以参考下
    2022-09-09
  • activitygroup 切换动画效果如何实现

    activitygroup 切换动画效果如何实现

    本文将详细介绍activitygroup 切换动画效果实现过程,需要聊解的朋友可以参考下
    2012-12-12
  • Android中底部菜单被输入法顶上去的解决方案

    Android中底部菜单被输入法顶上去的解决方案

    我们一般的解决方法是获取焦点,底部隐藏,失去焦点,底部菜单出现,但是,有些人会点击这个按钮收起键牌。这篇文章主要介绍了Android中底部菜单被输入法顶上去的解决方案,需要的朋友参考下吧
    2017-01-01
  • android实现汉字转拼音功能 带多音字识别

    android实现汉字转拼音功能 带多音字识别

    这篇文章主要介绍了android实现汉字转拼音功能,带多音字识别,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2017-02-02
  • Android组件实现列表选择框功能

    Android组件实现列表选择框功能

    android提供的列表选择框(Spinner)相当于web端用户注册时的选择下拉框,比如注册候选择省份城市等。这篇文章主要介绍了Android组件实现列表选择框功能,需要的朋友可以参考下
    2017-02-02
  • Android服务应用ClockService实现闹钟功能

    Android服务应用ClockService实现闹钟功能

    这篇文章主要为大家详细介绍了Android服务应用ClockService实现闹钟功能,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2020-11-11
  • android检查手机和无线是否连接的方法

    android检查手机和无线是否连接的方法

    这篇文章主要介绍了android检查手机和无线是否连接的方法,以两种不同的方法实现了该功能,是Android程序开发中非常常见的实用技巧,需要的朋友可以参考下
    2014-10-10
  • Android移动端touch实现下拉刷新功能

    Android移动端touch实现下拉刷新功能

    这篇文章主要介绍了移动端touch实现下拉刷新功能,非常不错,具有参考借鉴价值,需要的朋友可以参考下
    2017-02-02
  • Android自定义控件实现支付宝记账饼图

    Android自定义控件实现支付宝记账饼图

    这篇文章主要为大家详细介绍了Android自定义控件实现支付宝记账饼图,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2018-04-04
  • 完美解决安卓jni项目会删除其他so文件的问题

    完美解决安卓jni项目会删除其他so文件的问题

    下面小编就为大家带来一篇完美解决安卓jni项目会删除其他so文件的问题。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2016-12-12

最新评论