转 PopupWindow详解
来源:互联网 发布:美军网络司令部 编辑:程序博客网 时间:2024/05/15 07:58
原文地址:点击打开链接
构造方法
public PopupWindow(View contentView, int width, int height, boolean focusable)
contentView为要显示的view,width和height为宽和高,值为像素值,也可以是MATCHT_PARENT和WRAP_CONTENT。
还可以
public PopupWindow (Context context)
或者
public PopupWindow(View contentView, int width, int height)
或者
public PopupWindow(View contentView)
其中第一种最省事,构造函数中设置了要显示的View,宽度 ,高度以及是否能获得焦点。以上是几种用的比较常见的构造方法。
改变PopupWindow的视图内容
可以通过
public void setContentView(View contentView)
来改变popup的显示内容,也可以用来初始化PopupWindow的View,比如使用构造函数public PopupWindow (Context context)
获得的Popupwindow就只能用setContentView来设置内容。
PopupWindow popupWindow =
new
PopupWindow(context);
popupWindow.setContentView(contentview);
获得PopupWindow的视图内容
public View getContentView()
显示PopupWindow:
showAsDropDown(View anchor):相对某个控件的位置(正左下方),无偏移
showAsDropDown(View anchor, int xoff, int yoff):相对某个控件的位置,有偏移
showAtLocation(View parent, int gravity, int x, int y):相对于父控件的位置(例如正中央Gravity.CENTER,下方Gravity.BOTTOM等),可以设置偏移或无偏移
其实我发现showAtLocation的parent参数可以很随意,只要是activity中的view都可以。
大小:
有两种方法设置PopupWindow的大小:
调用有宽高参数的构造函数:
LayoutInflater inflater = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View contentview = inflater.inflate(R.layout.popup_process,
null
);
PopupWindow popupWindow =
new
PopupWindow(contentview,LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT);
通过setWidth和setHeight设置
PopupWindow popupWindow =
new
PopupWindow(contentview);
popupWindow.setWidth(LayoutParams.WRAP_CONTENT);
popupWindow.setHeight(LayoutParams.WRAP_CONTENT);
两种办法是等效的,不管采用何种办法,必须设置宽和高,否则不显示任何东西.
这里的WRAP_CONTENT可以换成fill_parent 也可以是具体的数值,它是指PopupWindow的大小,也就是contentview的大小,注意popupwindow根据这个大小显示你的View,如果你的View本身是从xml得到的,那么xml的第一层view的大小属性将被忽略。相当于popupWindow的width和height属性直接和第一层View相对应。
设想下面一种场景:
popupWindow 设置为WRAP_CONTENT ,我想得到的是一个宽150dip 高80dip的popupwindow,需要额外加一层
LinearLayout ,这个LinearLayout 的layout_width和layout_height为任意值。而我们真正想显示的View 放在第二层,并且 android:layout_width="150.0dip" android:layout_height="80.0dip"
如
<?xml version=
"1.0"
encoding=
"utf-8"
?>
<LinearLayout xmlns:android=
"http://schemas.android.com/apk/res/android"
android:layout_width=
"wrap_content"
android:layout_height=
"wrap_content"
>
<LinearLayout
android:background=
"@drawable/shape_ret_loading_bg"
android:layout_width=
"150.0dip"
android:layout_height=
"80.0dip"
android:orientation=
"vertical"
android:gravity=
"center"
>
<TextView
android:textSize=
"14dip"
android:textColor=
"@color/white"
android:layout_width=
"wrap_content"
android:layout_height=
"wrap_content"
android:layout_marginLeft=
"10.0dip"
android:text=
"加载中..."
/>
</LinearLayout>
</LinearLayout>
PopUpWindow的焦点:
setFocusable设置PopupWindow的焦点,一般资料对此的解释都是:是否让Popupwindow可以点击但是这揭示了本质却与直观现象不符。实际上,
如果:
setFocusable(true);
则PopUpWindow本身可以看作一个类似于模态对话框的东西(但有区别),PopupWindow弹出后,所有的触屏和物理按键都有PopupWindows处理。其他任何事件的响应都必须发生在PopupWindow消失之后, (home 等系统层面的事件除外)。比如这样一个PopupWindow出现的时候,按back键首先是让PopupWindow消失,第二次按才是退出activity,准确的说是想退出activity你得首先让PopupWindow消失,因为不并是任何情况下按back PopupWindow都会消失,必须在PopupWindow设置了背景的情况下 。
如果PopupWindow中有Editor的话,focusable要为true。
而setFocusable(false);
则PopUpWindow只是一个浮现在当前界面上的view而已,不影响当前界面的任何操作。
是一个“没有存在感”的东西。
一般情况下setFocusable(true);
点击空白处的时候让PopupWindow消失
关于PopupWindow最搞笑的地方是setOutsideTouchable方法,原本以为如果你setOutsideTouchable(true)则点击PopupWindow之外的地方PopupWindow会消失,其实这玩意儿好像一点用都没有。
要让点击PopupWindow之外的地方PopupWindow消失你需要调用setBackgroundDrawable(new BitmapDrawable());
设置背景,为了不影响样式,这个背景是空的。还可以这样写,觉得这样要保险些:
setBackgroundDrawable(new ColorDrawable(0x00000000));
背景不为空但是完全透明。如此设置还能让PopupWindow在点击back的时候消失。其实一直觉得很奇怪,不明白为什么一个背景会影响点击事件,只知道这样用可行。
用PopupWindow实现一个真正的模态对话框:
刚才说到“popupWindow.setFocusable(true);
则PopUpWindow本身可以看作一个类似于模态对话框的东西”
其实这句话有一些细微的错误,在android中一个模态对话框应该是这样的:
阻止屏幕上的其他View事件,且点击PopupWindow外面不会消失,但是能响应back事件(点击back消失),所以如果要让
PopupWindow有模态对话框的表现,则不能调用setBackgroundDrawable,因为setBackgroundDrawable会让点击PopupWindow外面PopupWindow消失。但是如果不调用setBackgroundDrawable则点击back键也不会消失,比模态对话框还变态。不过还是有解决的办法:
LayoutInflater inflater = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View contentview = inflater.inflate(R.layout.popup_process,
null
);
contentview.setFocusable(
true
);
// 这个很重要
contentview.setFocusableInTouchMode(
true
);
final PopupWindow popupWindow =
new
PopupWindow(contentview,LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
popupWindow.setFocusable(
true
);
popupWindow.setOutsideTouchable(
false
);
contentview.setOnKeyListener(
new
OnKeyListener() {
@Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
if
(keyCode == KeyEvent.KEYCODE_BACK) {
popupWindow.dismiss();
return
true
;
}
return
false
;
}
});
popupWindow.showAtLocation(view, Gravity.CENTER|Gravity.CENTER_HORIZONTAL, 0, 0);
其中要注意的是PopupWindow 的视图contentview的设置,contentview.setFocusable(true); 这个很重要,注意不是PopupWindow的setFocusable,同时setOnKeyListener也是contentview调用的,而不是PopupWindow。setOnKeyListener方法中的代码很好理解,但是为啥是contentview调用就不是很清楚了,貌似没人去研究。其实为了是代码的相关性更低contentview.setOnKeyListener
可以用popupWindow
.getContentView.setOnKeyListener
来代替。
话说如果PopupWindow需要如此这番周折才能实现一个模态对话框何不直接用Dialog呢?其实我只是将这些现象讲清楚,你可以根据这些知识将PopupWindow 应用在很苛刻的场景中。
PopupWindow的动画
很多时候我们把PopupWindow用作自定义的菜单,需要一个从底部向上弹出的效果,这就需要为PopupWindow添加动画。
设置动画的方法:
public void setAnimationStyle(int animationStyle)
在res/value/styles.xml添加一个sytle
<style name=
"anim_menu_bottombar"
>
<item name=
"android:windowEnterAnimation"
>@anim/menu_bottombar_in</item>
<item name=
"android:windowExitAnimation"
>@anim/menu_bottombar_out</item>
</style>
在工程res下新建anim文件夹,在anim文件夹先新建两个xml文件
menu_bottombar_in.xml
<?xml version=
"1.0"
encoding=
"utf-8"
?>
<set xmlns:android=
"http://schemas.android.com/apk/res/android"
>
<translate
android:duration=
"250"
android:fromYDelta=
"100.0%"
android:toYDelta=
"0.0"
/>
</set>
menu_bottombar_out.xml
<?xml version=
"1.0"
encoding=
"utf-8"
?>
<set xmlns:android=
"http://schemas.android.com/apk/res/android"
>
<translate
android:duration=
"250"
android:fromYDelta=
"0.0"
android:toYDelta=
"100%"
/>
</set>
mPopupWindow.setAnimationStyle(R.style.menu_anim_bottombar);
关于Popupwindow介绍的差不多了,如果你觉得还需要补充请在下面留言。
- 转 PopupWindow详解
- (转)Android PopupWindow详解
- PopUpWindow详解
- PopupWindow 详解
- PopupWindow详解
- Android中的PopupWindow详解
- popupwindow入门详解
- Android中的PopupWindow详解
- Android中的PopupWindow详解
- Android中的PopupWindow详解
- Android中的PopupWindow详解
- Android中的PopupWindow详解
- Android中的PopupWindow详解
- Android PopupWindow详解
- Android PopupWindow详解
- Android PopupWindow详解
- Android PopupWindow使用详解
- popupWindow使用详解
- PHP5.3, PHP5.4, PHP5.5新特性
- windows下安装各种版本的memcache
- 线程池之ScheduledThreadPool(周期、定时操作)
- LaTeX 的简单使用
- java中HashMap详解
- 转 PopupWindow详解
- 【BZOJ 1295】 [SCOI2009]最长距离
- 关于强制类型转换
- nyoj451 光棍节的快乐
- SpringMVC之参数传递
- CSS3 Background-size 详解
- ios-touch无效的解决办法
- Single Number
- Oracle表空间扩展