Android PopupWindow的使用和分析
- 格式:doc
- 大小:160.15 KB
- 文档页数:8
本文由我司收集整编,推荐下载,如有疑问,请与我司联系
【Android】PopupWindow使用小结
2012/09/04 2887
PopupWindow的很多用法网上比较多,我就不做过多解释了,只说下可能会遇到的问题,以及解决办法:
1、PopupWindow中的listview无响应
这个主要是因为show写在了setFocusable前面
2、点击PopupWindow外面区域,不会自动dismiss
这个主要可能是没有调用setBackgroundDrawable以及setOutsideTouchable,
当然了,你肯定还得写响应监听这个动作,如下面代码
mPopupWindow.setTouchInterceptor(new OnTouchListener() { @Override public boolean onTouch(View v, MotionEvent event) { if (event.getAction() == MotionEvent.ACTION_OUTSIDE) { mPopupWindow.dismiss(); Log.i(“test”, “test”); return true; } return false; } }); 3、将默认的箭头放到右边
int width = getWindowManager().getDefaultDisplay().getWidth(); mListView.setIndicatorBounds(width-40, width-10);
tips:感谢大家的阅读,本文由我司收集整编。
仅供参阅!。
AndroidPopUpWindow使⽤详解⽬录概述声明构造⽅法显⽰函数正常声明⼀个PopupWindow代码设置需要载⼊的布局创建PopupWindow设置显⽰位置完整代码概述最关键的区别是AlertDialog不能指定显⽰位置,只能默认显⽰在屏幕最中间(当然也可以通过设置WindowManager参数来改变位置)。
⽽PopupWindow是可以指定显⽰位置的,随便哪个位置都可以,更加灵活。
声明构造⽅法//⼀:public PopupWindow (Context context)//⼆:public PopupWindow(View contentView)//三:public PopupWindow(View contentView, int width, int height)//四:public PopupWindow(View contentView, int width, int height, boolean focusable)⼀个窗⼝标准的PopupWindow应该有三个参数上下⽂宽、⾼显⽰函数//相对某个控件的位置(正左下⽅),⽆偏移showAsDropDown(View anchor)://相对某个控件的位置,有偏移;xoff表⽰x轴的偏移showAsDropDown(View anchor, int xoff, int yoff)://正中央Gravity.CENTER,下⽅Gravity.BOTTOM等showAtLocation(View parent, int gravity, int x, int y):这⾥有两种显⽰⽅式:1、显⽰在某个指定控件的下⽅showAsDropDown(View anchor):showAsDropDown(View anchor, int xoff, int yoff);2、指定⽗视图,显⽰在⽗控件的某个位置(Gravity.TOP,Gravity.RIGHT等)showAtLocation(View parent, int gravity, int x, int y);3、view可以是任意组件正常声明⼀个PopupWindow代码设置需要载⼊的布局<?xml version="1.0" encoding="utf-8"?><LinearLayoutxmlns:android="/apk/res/android"android:layout_width="match_parent"android:layout_height="match_parent"><TextViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:textSize="20sp"android:textColor="#000"android:text="我是⼀个PopupWindow"/></LinearLayout>创建PopupWindow//将xml⽂件转成viewView view = LayoutInflater.from(MainActivity.this).inflate(yout.popupwindow,null); //创建popupWindowPopupWindow popupWindow = new PopupWindow(MainActivity.this);//载⼊布局popupWindow.setContentView(view);//设置宽⾼popupWindow.setWidth(youtParams.WRAP_CONTENT);popupWindow.setHeight(youtParams.WRAP_CONTENT);设置显⽰位置//设置显⽰位置正左下⽅⽆偏移popupWindow.showAsDropDown(mTvShow);完整代码import androidx.appcompat.app.AppCompatActivity;import android.os.Bundle;import youtInflater;import android.view.View;import android.view.ViewGroup;import android.widget.PopupWindow;import android.widget.TextView;public class MainActivity extends AppCompatActivity {TextView mTvShow;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(yout.activity_main);mTvShow = findViewById(show);mTvShow.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View v) {myPopupWindow();}});}private void myPopupWindow(){//将xml⽂件转成viewView view = LayoutInflater.from(MainActivity.this).inflate(yout.popupwindow,null); PopupWindow popupWindow = new PopupWindow(MainActivity.this);popupWindow.setContentView(view);popupWindow.setWidth(youtParams.WRAP_CONTENT);popupWindow.setHeight(youtParams.WRAP_CONTENT);//PopupWindow popupWindow = new PopupWindow(MainActivity.this,youtParams.WRAP_CONTENT,youtParams.WRAP_CONTENT,true); //进⾏正常页⾯设置//设置显⽰位置正左下⽅⽆偏移popupWindow.showAsDropDown(mTvShow);//popupwindow 点击外围页⾯不会⾃动消失因此需要⼿动设置⼀个关闭view.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View v) {popupWindow.dismiss();}});}}到此这篇关于Android PopUpWindow使⽤详解的⽂章就介绍到这了,更多相关Android PopUpWindow内容请搜索以前的⽂章或继续浏览下⾯的相关⽂章希望⼤家以后多多⽀持!。
PopupWindow使用PopupWindow这个类用来实现一个弹出框,可以使用任意布局的View作为其内容,这个弹出框是悬浮在当前activity之上的。
PopupWindow使用Demo这个类的使用,不再过多解释,直接上代码吧。
比如弹出框的布局:<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="/apk/res/android"android:layout_width="match_parent"android:layout_height="wrap_content"android:background="#FFBBFFBB"android:orientation="vertical"><TextViewandroid:layout_width="match_parent"android:layout_height="wrap_content"android:padding="10dp"android:text="Hello My Window"android:textSize="20sp"/><Buttonandroid:id="@+id/button1"android:layout_width="match_parent"android:layout_height="wrap_content"android:padding="10dp"android:text="Button"android:textSize="20sp"/></LinearLayout>Activity的布局中只有一个按钮,按下后会弹出框,Activity代码如下:package com.example.hellopopupwindow;import android.os.Bundle;import android.app.Activity;import android.content.Context;import android.util.Log;import youtInflater;import android.view.MotionEvent;import android.view.View;import android.view.View.OnClickListener;import android.view.View.OnTouchListener;import youtParams;import android.widget.Button;import android.widget.PopupWindow;import android.widget.Toast;public class MainActivity extends Activity {private Context mContext = null;@Overrideprotected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState);setContentView(yout.activity_main);mContext = this;Button button = (Button) findViewById(R.id.button);button.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View view) {showPopupWindow(view);}});}private void showPopupWindow(View view) {// 一个自定义的布局,作为显示的内容View contentView = LayoutInflater.from(mContext).inflate(yout.pop_window, null);// 设置按钮的点击事件Button button = (Button) contentView.findViewById(R.id.button1);button.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v) {Toast.makeText(mContext, "button is pressed",Toast.LENGTH_SHORT).show();}});final PopupWindow popupWindow = new PopupWindow(contentView,LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, true);popupWindow.setTouchable(true);popupWindow.setTouchInterceptor(new OnTouchListener() {@Overridepublic boolean onTouch(View v, MotionEvent event) {Log.i("mengdd", "onTouch : ");return false;// 这里如果返回true的话,touch事件将被拦截// 拦截后 PopupWindow的onTouchEvent不被调用,这样点击外部区域无法dismiss }});// 如果不设置PopupWindow的背景,无论是点击外部区域还是Back键都无法dismiss弹框 // 我觉得这里是API的一个bugpopupWindow.setBackgroundDrawable(getResources().getDrawable(R.drawable.selectmenu_bg_downward));// 设置好参数之后再showpopupWindow.showAsDropDown(view);}}弹出框的布局中有一个TextView和一个Button,Button点击后显示Toast,如图:第一次实现的时候遇到了问题,就是弹出框不会在按下Back键的时候消失,点击弹框外区域也没有正常消失,搜索了一下,都说只要设置背景就好了。
android popupwindow attrs参数使用-回复Android的PopupWindow是一个强大的工具,可以用来在应用程序中显示一个浮动窗口,用于展示一些额外的内容或交互。
PopupWindow 的attrs参数提供了更多的自定义选项,可以让我们更加灵活地控制PopupWindow的外观和行为。
在本文中,我们将一步一步回答关于PopupWindow的attrs参数使用的问题。
首先,让我们来了解一下PopupWindow的基本使用方法。
要创建一个PopupWindow,我们需要先创建一个布局文件,用于定义PopupWindow的内容。
接下来,我们可以使用LayoutInflater从布局文件中获取视图对象。
然后,我们可以使用PopupWindow的构造函数来创建一个实例,并将视图对象作为参数传入。
现在,让我们来看一下PopupWindow的attrs参数。
PopupWindow 的attrs参数是一个整数类型的值,用于指定PopupWindow的各种属性。
这些属性可以通过布局文件的theme和style属性来指定。
下面是一些常用的attrs参数及其含义:- android:popupBackground:指定PopupWindow的背景颜色或背景图片。
- android:popupAnimationStyle:指定PopupWindow的进出动画效果。
- android:popupEnterTransition:指定PopupWindow的进入过渡效果。
- android:popupExitTransition:指定PopupWindow的退出过渡效果。
- android:popupWidth:指定PopupWindow的宽度。
- android:popupHeight:指定PopupWindow的高度。
- android:popupGravity:指定PopupWindow的位置属性,比如左上角、右上角等。
popupwindow简介PopupWindow简介PopupWindow是Android中的一个类,主要用于显示一个浮动视图,可以在任何视图组件上浮在前面,由用户触发,并在完成后自动关闭。
它可以用来显示用户输入数据的选择弹出窗口,例如下拉列表、菜单和进度条,也可以用在突出显示某些元素和提供额外信息的背景之上。
PopupWindow示例应用场景:1.显示下拉菜单当用户点击某个按钮时,弹出菜单进行选择,用PopupWindow比使用普通的menu item更灵活也容易处理。
2.屏幕提示在应用程序使用教学或者介绍中,我们可以使用PopupWindow,把需要重点讲解的地方高亮显示出来,并在那里进行提示。
PopupWindow的常用功能:1.控制弹出位置它具有指定窗口的位置的功能,例如:可以指定它相对于屏幕上任何位置的像素坐标以及在另一个视图的边缘上的对齐方式等等。
2. 背景设置设置弹出窗口的背景。
可以设置为系统自带的风格,也可以通过定义自己的Drawable背景来创建。
3. 固定大小PopupWindow还允许您强制弹出窗口的大小,使其维持固定的宽度和高度。
4. 取消、显示Show()和dismiss()方法是使用PopupWindow最常见的方式之一。
它允许您在任何时候取消显示PopupWindow。
5. 触摸事件处理PopupWindow具有可自定义触摸事件处理的功能(touch interceptor),这意味着您可以在用户尝试在视图上进行操作时捕获触摸事件。
PopupWindow的主要优点:1.功能灵活PopupWindow很灵活,几乎可以完美地拉伸设计和布局未知大小天然元素的UI。
2.数据展示可以将PopupWindow用于数据的展示,如动态表格、图片、视频等。
3.交互响应触控、手势等交互行为相对更灵活和全面。
4.节省空间PopupWindow可以节约界面空间,使得UI设计更加简介合理,也使得更多的信息可以展示。
android popupwindow 弹框kotlin 用法下载提示:该文档是本店铺精心编制而成的,希望大家下载后,能够帮助大家解决实际问题。
文档下载后可定制修改,请根据实际需要进行调整和使用,谢谢!本店铺为大家提供各种类型的实用资料,如教育随笔、日记赏析、句子摘抄、古诗大全、经典美文、话题作文、工作总结、词语解析、文案摘录、其他资料等等,想了解不同资料格式和写法,敬请关注!Download tips: This document is carefully compiled by this editor. I hope that after you download it, it can help you solve practical problems. The document can be customized and modified after downloading, please adjust and use it according to actual needs, thank you! In addition, this shop provides you with various types of practical materials, such as educational essays, diary appreciation, sentence excerpts, ancient poems, classic articles, topic composition, work summary, word parsing, copy excerpts, other materials and so on, want to know different data formats and writing methods, please pay attention!Android中的PopupWindow是一种常见的弹框控件,可以在应用程序中实现各种弹出框的效果。
popupwindow简介PopupWindow是Android中常用的一个弹出式窗口,它可以在当前界面上方弹出一个新的窗口,用于显示一些额外的信息或者提供一些额外的操作。
PopupWindow可以自定义布局和样式,非常灵活,因此在很多应用场景中都得到了广泛的应用。
PopupWindow的使用非常简单,首先需要创建一个PopupWindow对象,然后设置它的布局和样式,最后调用showAsDropDown或者showAtLocation方法即可将PopupWindow 显示在界面上。
其中showAsDropDown方法可以将PopupWindow 显示在某个View的下方,而showAtLocation方法可以将PopupWindow显示在屏幕的任意位置。
PopupWindow的布局可以使用XML文件进行定义,也可以使用代码进行动态创建。
在布局中可以添加各种控件,例如TextView、Button、ImageView等等,以满足不同的需求。
同时,PopupWindow也支持设置背景、动画、宽度、高度等属性,以便更好地适应不同的场景。
PopupWindow的应用场景非常广泛,例如在聊天界面中,可以使用PopupWindow显示表情、图片等内容;在列表界面中,可以使用PopupWindow显示筛选条件、排序方式等选项;在地图应用中,可以使用PopupWindow显示地点信息、导航路线等内容。
总之,只要需要在当前界面上方显示一些额外的信息或者提供一些额外的操作,都可以考虑使用PopupWindow。
需要注意的是,PopupWindow虽然功能强大,但是也存在一些问题。
例如在某些机型上,PopupWindow可能会出现遮挡、位置偏移等问题;在某些情况下,PopupWindow可能会被系统的虚拟按键遮挡。
因此,在使用PopupWindow时需要注意这些问题,并进行相应的处理。
PopupWindow是Android中非常实用的一个组件,它可以方便地实现弹出式窗口的功能,适用于各种应用场景。
Android入门第十篇之PopupWindow介绍过AlertDialog之后,接下来就介绍一下PopupWindow这种对话框。
PopupWindow是阻塞对话框,只有在外部线程或者PopupWindow本身做退出操作才行。
PopupWindow完全依赖Layout做外观,在常见的开发中,PopupWindow应该会与AlertDialog常混用。
贴出本例中运行的结果图:main.xml的源码如下:view plainprint?< xmlns:android="/apk/res/android">android:orientation="vertical"android:layout_width="fill_parent"android:layout_height="fill_parent">下图是PopupWindow弹出的截图,这里的PopupWindow是个登录框,点“确定”则自动填写,点“取消”则关闭PopupWindow。
popupwindow.xml的源码:view plainprint?< xmlns:android="/apk/res/android">android:layout_width="fill_parent" android:layout_height="wrap_content"android:orientation="vertical" android:background="#000000">< android:id="@+id/username_view">android:layout_height="wrap_content"android:layout_marginLeft="20dip"android:layout_marginRight="20dip" android:text="用户名"android:textAppearance="?android:attr/textAppearanceMedium" android:layout_width="fill_parent"/>< android:id="@+id/username_edit">android:layout_height="wrap_content"android:layout_width="fill_parent" android:layout_marginLeft="20dip"android:layout_marginRight="20dip" android:capitalize="none"android:textAppearance="?android:attr/textAppearanceMedium" />< android:id="@+id/password_view">android:layout_height="wrap_content"android:layout_marginLeft="20dip"android:layout_marginRight="20dip" android:text="密码"android:textAppearance="?android:attr/textAppearanceMedium" android:layout_width="fill_parent"/> < android:id="@+id/password_edit">android:layout_height="wrap_content"android:layout_width="fill_parent" android:layout_marginLeft="20dip"android:layout_marginRight="20dip" android:capitalize="none"android:password="true"android:textAppearance="?android:attr/textAppearanceMedium" />接下来是程序源码:view plainprint?package com.testAlertDialog;import android.app.Activity;import android.app.AlertDialog;import android.content.Context;import android.content.DialogInterface;import android.os.Bundle;import android.text.Editable;import android.view.Gravity;import youtInflater;import android.view.View;import android.view.View.OnClickListener;import android.widget.Button;import android.widget.EditText;import android.widget.PopupWindow;public class testAlertDialog extends Activity {Button btnPopupWindow;/** Called when the activity is first created. */@Overridepublic void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentV iew(yout.main);//定义按钮btnPopupWindow=(Button)this.findV iewById(R.id.Button01);btnPopupWindow.setOnClickListener(new ClickEvent());}//统一处理按键事件class ClickEvent implements OnClickListener{@Overridepublic void onClick(View v) {// TODO Auto-generated method stubif(v==btnPopupWindow){showPopupWindow(testAlertDialog.this,testAlertDialog.this.findV iewById(R.id.Button01));}}}public void showPopupWindow(Context context,V iew parent){LayoutInflater inflater = (LayoutInflater)context.getSystemService(YOUT_INFLA TER_SERVICE);final V iew vPopupWindow=inflater.inflate(yout.popupwindow, null, false);final PopupWindow pw= new PopupWindow(vPopupWindow,300,300,true);//OK按钮及其处理事件Button btnOK=(Button)vPopupWindow.findV iewById(R.id.BtnOK);btnOK.setOnClickListener(new OnClickListener(){@Overridepublic void onClick(View v) {//设置文本框内容EditText edtUsername=(EditText)vPopupWindow.findV iewById(ername_edit); edtUsername.setText("username");EditText edtPassword=(EditText)vPopupWindow.findV iewById(R.id.password_edit); edtPassword.setText("password");}});//Cancel按钮及其处理事件Button btnCancel=(Button)vPopupWindow.findV iewById(R.id.BtnCancel); btnCancel.setOnClickListener(new OnClickListener(){@Overridepublic void onClick(View v) {pw.dismiss();//关闭}});//显示popupWindow对话框pw.showAtLocation(parent, Gravity.CENTER, 0, 0);}}。
android popupwindow基本使用方法-回复如何在Android中使用PopupWindow一. 介绍PopupWindow是Android中一个非常强大且常用的组件,它可以在屏幕上弹出一个浮动窗口,通常用于显示额外的内容或用户交互。
PopupWindow非常灵活,可以根据需求定制不同的形状、大小和位置,为用户提供更好的体验。
二. 创建PopupWindow1. 在布局文件中定义PopupWindow的内容视图:首先,在res/layout文件夹中创建一个布局文件,作为PopupWindow的内容视图。
例如,我们可以创建一个名为"popupwindow_layout.xml"的布局文件,定义PopupWindow内部的UI元素。
2. 在代码中实例化PopupWindow:使用LayoutInflater从布局文件中加载视图,然后将其传递给PopupWindow的构造函数。
例如:javaLayoutInflater inflater = (LayoutInflater) getSystemService(YOUT_INFLATER_SERVICE);View contentView =inflater.inflate(yout.popupwindow_layout, null);PopupWindow popupWindow = newPopupWindow(contentView,youtParams.WRAP_CONTENT,youtParams.WRAP_CONTENT);这里,我们使用了LayoutInflater加载了布局文件中的视图,并将其传递给PopupWindow的构造函数。
第二个和第三个参数是PopupWindow的宽度和高度,可以根据需要设置。
三. 设置PopupWindow的属性1. 设置背景:PopupWindow的背景默认是透明的,如果要为PopupWindow设置背景,可以使用setBackgroundDrawable(Drawable drawable)方法。
Android PopupWindow的使用和分析PopupWindow使用PopupWindow这个类用来实现一个弹出框,可以使用任意布局的View作为其内容,这个弹出框是悬浮在当前activity之上的。
PopupWindow使用Demo这个类的使用,不再过多解释,直接上代码吧。
比如弹出框的布局:弹出框布局Activity的布局中只有一个按钮,按下后会弹出框,Activity代码如下:package com.example.hellopopupwindow;import android.os.Bundle;import android.app.Activity;importandroid.content.Context;import android.util.Log;importyoutInflater;import android.view.MotionEvent;importandroid.view.View;import android.view.View.OnClickListener;importandroid.view.View.OnTouchListener;importyoutParams;import android.widget.Button;import android.widget.PopupWindow;import android.widget.Toast;public class MainActivity extends Activity {private Context mContext = null;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(yout.activity_main);mContext = this;Button button = (Button) findViewById(R.id.button);button.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View view) {showPopupWindow(view);}});}private void showPopupWindow(View view) {// 一个自定义的布局,作为显示的内容View contentView = LayoutInflater.from(mContext).inflate(yout.pop_window, null);// 设置按钮的点击事件Button button = (Button) contentView.findViewById(R.id.button1);button.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v) {Toast.makeText(mContext, "button is pressed",Toast.LENGTH_SHORT).show();}});final PopupWindow popupWindow = new PopupWindow(contentView,LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, true); popupWindow.setTouchable(true);popupWindow.setTouchInterceptor(new OnTouchListener() {@Overridepublic boolean onTouch(View v, MotionEvent event) {Log.i("mengdd", "onTouch : ");return false;// 这里如果返回true的话,touch事件将被拦截// 拦截后 PopupWindow的onTouchEvent不被调用,这样点击外部区域无法dismiss }});// 如果不设置PopupWindow的背景,无论是点击外部区域还是Back键都无法dismiss 弹框// 我觉得这里是API的一个bugpopupWindow.setBackgroundDrawable(getResources().getDrawable(R.drawable.selectmenu_bg_downward));// 设置好参数之后再show popupWindow.showAsDropDown(view);}}弹出框的布局中有一个TextView和一个Button,Button点击后显示Toast,如图:第一次实现的时候遇到了问题,就是弹出框不会在按下Back键的时候消失,点击弹框外区域也没有正常消失,搜索了一下,都说只要设置背景就好了。
然后我就找了个图片,果然弹框能正常dismiss了(见注释)。
PopupWindow源码分析为了解答一下上面的问题,看看源码(最新API Level 19,Android 4.4.2)。
1.显示方法显示提供了两种形式:showAtLocation()显示在指定位置,有两个方法重载:public void showAtLocation(View parent, int gravity, int x, int y)public void showAtLocation(IBinder token, int gravity, int x, int y)showAsDropDown()显示在一个参照物View的周围,有三个方法重载:public void showAsDropDown(View anchor)public void showAsDropDown(View anchor, int xoff, int yoff)public void showAsDropDown(View anchor, int xoff, int yoff, int gravity) 最后一种带Gravity参数的方法是API 19新引入的。
弹出的方法中首先需要preparePopup(),最后再invokePopup()。
prepare的方法中可以看到有无背景的分别:/*** <p>Prepare the popup by embedding in into a new ViewGroup if the * background drawable is not null. If embedding is required, the layout * parameters' height is mnodified to take into account the background's * padding.</p>** @param p the layout parameters of the popup's content view*/private void preparePopup(youtParams 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 youtParams layoutParams =mContentView.getLayoutParams();int height = youtParams.MATCH_PARENT;if (layoutParams != null &&layoutParams.height == youtParams.WRAP_CONTENT) {height = youtParams.WRAP_CONTENT;}// when a background is available, we embed the content view// within another view that owns the background drawablePopupViewContainer popupViewContainer = newPopupViewContainer(mContext);youtParams listParams = new youtParams(youtParams.MATCH_PARENT, height);popupViewContainer.setBackgroundDrawable(mBackground);popupViewContainer.addView(mContentView, listParams);mPopupView = popupViewContainer;} else {mPopupView = mContentView;}mPopupViewInitialLayoutDirectionInherited =(mPopupView.getRawLayoutDirection() ==YOUT_DIRECTION_INHERIT);mPopupWidth = p.width;mPopupHeight = p.height;}2.背景是否为空对Touch事件的影响如果有背景,则会在contentView外面包一层PopupViewContainer之后作为mPopupView,如果没有背景,则直接用contentView作为mPopupView。
而这个PopupViewContainer是一个内部私有类,它继承了FrameLayout,在其中重写了Key和Touch事件的分发处理:@Overridepublic boolean dispatchKeyEvent(KeyEvent event) {if (event.getKeyCode() == KeyEvent.KEYCODE_BACK) {if (getKeyDispatcherState() == null) {return super.dispatchKeyEvent(event);}if (event.getAction() == KeyEvent.ACTION_DOWN && event.getRepeatCount() == 0) {KeyEvent.DispatcherState state = getKeyDispatcherState();if (state != null) {state.startTracking(event, this);}return true;} else if (event.getAction() == KeyEvent.ACTION_UP) {KeyEvent.DispatcherState state = getKeyDispatcherState();if (state != null && state.isTracking(event) && !event.isCanceled()) {dismiss();return true;}}return super.dispatchKeyEvent(event);} else {return super.dispatchKeyEvent(event);}}@Overridepublic boolean dispatchTouchEvent(MotionEvent ev) {if (mTouchInterceptor != null && mTouchInterceptor.onTouch(this, ev)) {return true;}return super.dispatchTouchEvent(ev);}@Overridepublic 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);}}由于PopupView本身并没有重写Key和Touch事件的处理,所以如果没有包这个外层容器类,点击Back键或者外部区域是不会导致弹框消失的。