前言:有人问我,即便梦想成真了又能怎样,或许不能怎样,但这是梦想。
相关文章:
1、《PopUpWindow使用详解(一)——基本使用》
2、《PopUpWindow使用详解(二)——进阶及答疑》
上篇为大家基本讲述了有关PopupWindow的基本使用,但还有几个相关函数还没有讲述,我们这篇将着重看看这几个函数的用法并结合源码来讲讲具体原因,最后是有关PopupWindow在使用时的疑问,给大家讲解一下。
一、常用函数讲解
这段将会给大家讲下下面几个函数的意义及用法,使用上篇那个带背景的例子为基础。
public void setTouchable(boolean touchable)
public void setFocusable(boolean focusable)
public void setOutsideTouchable(boolean touchable)
public void setBackgroundDrawable(Drawable background)
1、setTouchable(boolean touchable)
设置PopupWindow是否响应touch事件,默认是true,如果设置为false,即会是下面这个结果:(所有touch事件无响应,包括点击事件)
对应代码:
private void showPopupWindow() {
View contentView = LayoutInflater.from(MainActivity.this).inflate(R.layout.popuplayout, null);
mPopWindow = new PopupWindow(contentView);
mPopWindow.setWidth(ViewGroup.LayoutParams.FILL_PARENT);
mPopWindow.setHeight(ViewGroup.LayoutParams.FILL_PARENT);
mPopWindow.setTouchable(false);
………………//单项点击
mPopWindow.showAsDropDown(mMenuTv);
}
2、setFocusable(boolean focusable)
该函数的意义表示,PopupWindow是否具有获取焦点的能力,默认为False。一般来讲是没有用的,因为普通的控件是不需要获取焦点的,而对于EditText则不同,如果不能获取焦点,那么EditText将是无法编辑的。
所以,我们在popuplayout.xml最底部添加一个EditText,分别演示两段不同的代码,即分别将setFocusable设置为false和设置为true;看看有什么不同:
(1)setFocusable(true)
代码如下:
private void showPopupWindow() {
View contentView = LayoutInflater.from(MainActivity.this).inflate(R.layout.popuplayout, null);
mPopWindow = new PopupWindow(contentView);
mPopWindow.setWidth(ViewGroup.LayoutParams.FILL_PARENT);
mPopWindow.setHeight(ViewGroup.LayoutParams.FILL_PARENT);
//是否具有获取焦点的能力
mPopWindow.setFocusable(true);
…………//各item点击响应
mPopWindow.showAsDropDown(mMenuTv);
}
明显在点击EditText的时候,会弹出编辑框。
(2)setFocusable(false)
同样上面一段代码,那我们将setFocusable设置为false,会是怎样呢?
private void showPopupWindow() {
View contentView = LayoutInflater.from(MainActivity.this).inflate(R.layout.popuplayout, null);
mPopWindow = new PopupWindow(contentView);
mPopWindow.setWidth(ViewGroup.LayoutParams.FILL_PARENT);
mPopWindow.setHeight(ViewGroup.LayoutParams.FILL_PARENT);
//是否具有获取焦点的能力
mPopWindow.setFocusable(false);
…………//各item点击响应
mPopWindow.showAsDropDown(mMenuTv);
}
效果图下:
可见,点击EditText没有出现任何反应!所以如果PopupWindow没有获取焦点的能力,那么它其中的EditText当然是没办法获取焦点的,EditText无法获取焦点,那对它而言整个EditText控件就是不可用的。
3、setOutsideTouchable(boolean touchable)
这个函数的意义,就是指,PopupWindow以外的区域是否可点击,即如果点击PopupWindow以外的区域,PopupWindow是否会消失。
下面这个是点击会消息的效果图:
看看它对应的代码:
private void showPopupWindow() {
View contentView = LayoutInflater.from(MainActivity.this).inflate(R.layout.popuplayout, null);
mPopWindow = new PopupWindow(contentView);
mPopWindow.setWidth(ViewGroup.LayoutParams.FILL_PARENT);
mPopWindow.setHeight(ViewGroup.LayoutParams.FILL_PARENT);
//外部是否可以点击
mPopWindow.setBackgroundDrawable(new BitmapDrawable());
mPopWindow.setOutsideTouchable(true);
…………//各ITEM点击响应
mPopWindow.showAsDropDown(mMenuTv);
}
这里要非常注意的一点:
mPopWindow.setBackgroundDrawable(new BitmapDrawable());
mPopWindow.setOutsideTouchable(true);
大家可能要疑问,为什么要加上mPopWindow.setBackgroundDrawable(new BitmapDrawable());这句呢,从代码来看没并没有真正设置Bitmap,而只是new了一个空的bitmap,好像并没起到什么作用。那如果我们把这句去掉会怎样:
把代码改成这样子:(只使用setOutsideTouchable)
private void showPopupWindow() {
View contentView = LayoutInflater.from(MainActivity.this).inflate(R.layout.popuplayout, null);
m