- 浏览: 73189 次
- 性别:
- 来自: 北京
文章分类
最新评论
-
wangkuichina:
此event坐标单位是什么呢?
android view的触摸事件坐标 -
coolismonster:
正好用得到 谢谢分享
android文件夹管理器 -
shim:
HashMap不是线程安全的,这么搞有风险
Gallery异步加载图片 -
Lifehopeness:
谢谢分享
android自定义带多选框的listview并避免出现自动选中另外选项 -
czbnlzd920706:
Acitivity中 44行 stopS ...
android音乐播放器拖放播放进度设计
PopupWindow
- 博客分类:
- android
PopupWindow,实质是使用WindowManager添加的一个顶层View。
PopupWindow::preparePopup会判断是否有背景,有则创建自己的容器,没有则直接用传入的view。
PopupViewContainer 是PopupWindow的内部类,里面有触摸事件的判断:
这就是为什么PopupWindow设置了背景setBackgroundDrawable后点击popupwindow外能隐去popupwindow。
触摸事件可以进行拦截,使用PopupWindow::setTouchInterceptor方法,这个对应于内部类PopupViewContainer 中的:
出入的拦截接听的返回结果很重要,true是直接消耗掉触摸事件,false则继续调用后面的触摸事件。
PopupWindow在退出activity前,如果没有dismiss方法,会报错。
例子(功能:PopWindow显示时,点击屏幕外,点击菜单键,返回键隐去):
PopupWindow::preparePopup会判断是否有背景,有则创建自己的容器,没有则直接用传入的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.setBackgroundDrawable(mBackground); popupViewContainer.addView(mContentView, listParams); mPopupView = popupViewContainer; } else { mPopupView = mContentView; } mPopupWidth = p.width; mPopupHeight = p.height; }
PopupViewContainer 是PopupWindow的内部类,里面有触摸事件的判断:
@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); } }
这就是为什么PopupWindow设置了背景setBackgroundDrawable后点击popupwindow外能隐去popupwindow。
触摸事件可以进行拦截,使用PopupWindow::setTouchInterceptor方法,这个对应于内部类PopupViewContainer 中的:
public boolean dispatchTouchEvent(MotionEvent ev) { if (mTouchInterceptor != null && mTouchInterceptor.onTouch(this, ev)) { return true; } return super.dispatchTouchEvent(ev); }
出入的拦截接听的返回结果很重要,true是直接消耗掉触摸事件,false则继续调用后面的触摸事件。
PopupWindow在退出activity前,如果没有dismiss方法,会报错。
例子(功能:PopWindow显示时,点击屏幕外,点击菜单键,返回键隐去):
package com.example.popwindow; import android.app.Activity; import android.graphics.drawable.BitmapDrawable; import android.os.Bundle; import android.view.Gravity; import android.view.KeyEvent; import android.view.View; import android.view.View.OnClickListener; import android.view.View.OnKeyListener; import android.view.ViewGroup.LayoutParams; import android.widget.Button; import android.widget.PopupWindow; public class PopupWindowActivity extends Activity implements OnClickListener { PopupWindow pop; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_popup_window); Button btn = (Button) findViewById(R.id.btn); btn.setOnClickListener(this); } public void onClick(View v) { switch(v.getId()){ case R.id.btn: popMenu(); break; } } private void popMenu() { if(pop == null){ View view = getLayoutInflater().inflate(R.layout.pop, null); //下面两句是必须添加滴,只有添加了这两个,才能设置key事件响应 view.setFocusable(true); view.setFocusableInTouchMode(true); view.setOnKeyListener(new OnKeyListener() { public boolean onKey(View v, int keyCode, KeyEvent event) { int action = event.getAction(); if(action == KeyEvent.ACTION_UP){ if(keyCode == KeyEvent.KEYCODE_MENU){ pop.dismiss(); System.out.println("弹出层的view响应菜单键,隐藏pop层!"); return true; } } return false; } }); pop = new PopupWindow(view, LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT, true); //一定要有背景,PopWindow源码设计就是这样,有背景则响应弹出层外的点击,没有则不响应 pop.setBackgroundDrawable(new BitmapDrawable()); } pop.showAtLocation(getWindow().getDecorView(), Gravity.CENTER_HORIZONTAL|Gravity.BOTTOM, 0, 0); } }
发表评论
-
DrawLayout几个注意点
2014-03-08 22:55 1431package com.example.navigationd ... -
layout_weight解惑
2014-02-20 13:25 0http://www.chess-ix.com/blog/th ... -
LayoutInflater注意事项
2014-02-20 10:52 0http://www.doubleencore.com/201 ... -
混淆掉Log.i输出
2013-12-26 15:42 0混淆配置文件加入 -assumenosideeffects c ... -
android TextView字体大小对应
2013-11-30 16:36 699设备信息:小米1 density=1.5, width=480 ... -
PopupWindow使用注意3点
2013-10-18 19:32 509使用PopupWindow注意点: ...... View v ... -
android 数据库同步
2013-10-14 13:35 1289package org.jerry; import or ... -
android singleTask几个注意点
2013-07-06 11:42 815android官网地址:http://developer.an ... -
转:WindowManager和WindowManager.LayoutParams的使用以及实现悬浮窗口的方法
2013-05-02 00:59 937文章原址:http://www.jcodecraeer.com ... -
Android onTouchEvent, onClick及onLongClick的调用机制
2013-04-28 02:21 0http://blog.csdn.net/ddna/artic ... -
Touch一些东西
2013-04-28 02:03 0package com.example.touchdemo; ... -
电话状态变化
2013-04-27 23:39 1809电话拨出,随后挂断 04- ... -
Toast重复利用
2013-04-26 00:10 1141package com.example.toastdemo; ... -
dp sp与px的相互转化
2013-04-23 01:19 0TextView.java /** * ... -
Android手机分辨率基础知识(DPI,DIP计算)
2013-04-22 22:57 0原文地址:http://blog.csdn.net/morui ... -
9.png NinePatchDrawable
2013-04-17 00:27 0地址: docs/tools/help/draw9patch. ... -
BaseAdapter
2013-04-16 09:43 0http://blog.csdn.net/pkxiuluo01 ... -
Android Intent的几种用法全面总结
2013-04-11 01:21 0Intent应该算是Android中特有的东西。你可以在Int ... -
Android中的Intent详细讲解
2013-04-11 01:18 0在一个Android应用中,主要是由四种组件组成的,这四种组件 ... -
android view的触摸事件坐标
2013-04-26 00:11 1101android view的触摸事件坐标不是屏幕坐标,是相对于v ...
相关推荐
实现的功能有弹出的PopupWindow上有EditText能与软键盘并存不冲突,弹出PopupWindow的时候背景自动改变灰度为半透明黑色,消失后背景灰度复原,PopupWindow还能根据自己的实际需求制定自己想要的弹出动画,以及...
android popupwindow 底部灰色背景
PopupWindow之显示顶层对话框代码
android 封装一个通用PopupWindow,避免重复造轮子,简化代码,方便管理。
仿微信右上角弹出PopupWindow的Demo,
popupwindow弹窗实现点击按钮,下方弹出popwindow,并且只是按钮下方有阴影,按钮以及上方没有阴影
Android源码——PopupWindow实现弹出菜单.zip
Android从屏幕底部滑动弹出PopupWindow,有动画效果,类似于sharesdk的分享页面滑动弹出效果。
让popupwindow显示在view的上方并与该view水平居中对齐
popupwindow 遮罩 窗帘式 从上往下 半显示 带动画 漂亮
和大家分享一下用一个PopupWindow来做一个指引蒙板的效果。鉴于网上对于“指引蒙板”的实现例子不是太多(或者是我找不到更多的),而且好像有很多的例子都是用一个Frame布局+Fragment去完成,对于这种做法自己不太...
AS写的PopupWindow demo blog:http://blog.csdn.net/shenyuanqing/article/details/48136639
点击按钮在屏幕下方会出现PopupWindow,PopupWindow中的PickerView可以用来选择时间。
Android PopupWindow与Activity之间的数据传递