转于:http://my.oschina.net/xsjayz/blog/184848#OSC_h2_5
----------------------------------------------------------------------------------------------------
一、概述
1、构造方法
部分常用:
1
2
3
4
|
PopupWindow(View contentView, int width, int height)
Create a new non focusable popup window which can display the contentView.
PopupWindow(View contentView, int width, int height, boolean focusable)
Create a new popup window which can display the contentView.
|
contentView为要显示的view,width和height为宽和高,值为像素值,也可以是MATCHT_PARENT和WRAP_CONTENT。
focusable为是否可以获得焦点,也可以通过set方法设置:
1
2
|
// Changes the focusability of the popup window setFocusable( boolean focusable)
|
根据网上的资料,focusable参数的主要作用是:
如果focusable为false,在一个Activity弹出一个PopupWindow,按返回键,由于PopupWindow没有焦点,会直接退出Activity。如果focusable为true,PopupWindow弹出后,所有的触屏和物理按键都由PopupWindow处理。
如果PopupWindow中有Editor的话,focusable要为true。
二、初始化PopupWindow
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
|
/** * 初始化rightPopupWindow
*/
private PopupWindow initPopupWindow() {
LayoutInflater inflater = LayoutInflater.from( this );
View popView = inflater.inflate(R.layout.popupwindow_content, null );
TextView item = (TextView) popView.findViewById(R.id.text_item);
// 创建PopupWindow
final PopupWindow popupWindow = createPopupWindow(popView);
popView.setOnTouchListener( new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
if (rightPop != null && rightPop.isShowing()) {
popupWindow.dismiss();
}
return true ;
}
});
item.setOnClickListener( new View.OnClickListener() {
@Override
public void onClick(View v) {
popupWindow.dismiss();
// ...
}
});
return popupWindow;
} |
三、创建一个PopupWindow
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
|
/** * 创建PopupWindow
*
* @param popView
* 指定PopupWindow的内容
*/
private PopupWindow createPopupWindow(View popView) {
PopupWindow popupInstance = new PopupWindow(popView, LayoutParams.MATCH_PARENT,
LayoutParams.MATCH_PARENT);
popupInstance.setFocusable( true );
popupInstance.setBackgroundDrawable( new ColorDrawable( 0 ));
popupInstance.setOutsideTouchable( true );
// 监听器
popupInstance.setOnDismissListener( new PopupWindow.OnDismissListener() {
@Override
public void onDismiss() {
// ...
}
});
return popupInstance;
} |
四、显示
例如一个button,点击该按钮,显示popupWindow,并且要求popupWindow显示在该按钮下面,则
1
2
3
4
5
6
7
|
btn.setOnClickListener( new View.OnClickListener() {
@Override
public void onClick(View v) {
PopupWindow popupWindow = ...;
popupWindow.showAsDropDown(v);
}
}); |
另外,还支持指定位+置偏移量显示:
1
|
public void showAsDropDown(View anchor, int xoff, int yoff);
|
1
2
3
4
5
6
7
8
9
|
public void showAtLocation (View parent, int gravity, int x, int y)
Display the content view in a popup window at the specified location. If the popup window cannot fit on screen, it will be clipped. See WindowManager.LayoutParams for more information on how gravity and the x and y parameters are related. Specifying a gravity of NO_GRAVITY is similar to specifying Gravity.LEFT | Gravity.TOP.
Parameters parent a parent view to get the getWindowToken() token from gravity the gravity which controls the placement of the popup window x the popup's x location offset y the popup's y location offset |
五、popupWindow显示优化
1、动画效果
例如显示的时候,从底部出现或者从顶部出现等,可以添加自定义动画效果。
1
2
3
4
5
6
7
8
9
|
<? 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 >
|
1
2
3
4
5
6
7
8
9
|
<? 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 >
|
2、其他效果
默认的popupWindow显示的时候,是没有像Dialog那样的背景变成灰色半透明效果的。可以自己添加类似Dialog或ios那样的效果。
例如,设置popupWindow的显示内容的布局文件的background属性为灰色半透明颜色,然后popupWindow在创建的时候指定宽、高属性为MATCH_PARENT即可实现除了popupWindow以外区域变成灰色半透明效果。
当然,这个灰色区域,其实也属于popupWindow,只是没有真正的内容。这时候如果需要设置点击popupWindow以外区域让它自动消失的话,就需要额外处理。因为点击灰色部分,你仍然点击的是popupWindow本身。
一种方法是给popupWindow的整个布局区域添加onTouch事件监听器,手动让popupWindow去dismiss()掉。
相关推荐
Android: PopupWindow 上显示 ListView 并相应 Demo (仿QQ登录界面,待改进)
NULL 博文链接:https://elingwange.iteye.com/blog/1613197
android popupwindow 底部灰色背景
android中popupWindow使用案列。点击按钮,显示出菜单按钮。
android自定义PopupWindow,实现类似微信弹框类别选项。
android 封装一个通用PopupWindow,避免重复造轮子,简化代码,方便管理。
Android Spinner PopupWindow 实现弹出框
Android自定义PopupWindow日历 日期时间选择器 可自由搭配年月日时分显示,可以设置只显示年月日,不显示时分等,
Android PopupWindow与Activity之间的数据传递
Android开发之PopupWindow实现弹窗
AS写的PopupWindow demo blog:http://blog.csdn.net/shenyuanqing/article/details/48136639
android通过使用popupwindow控件实现自定义menu菜单
Android从屏幕底部滑动弹出PopupWindow,有动画效果,类似于sharesdk的分享页面滑动弹出效果。
Android PopupWindow实现右侧、左侧和底部弹出菜单 效果图:http://blog.csdn.net/jianfengwen/article/details/48629217
android使用PopUpWindow自定义下拉框
Android之用PopupWindow实现弹出listview形式菜单.rar
Android TV 开发有关PopupWindow的KeyListener
实现在android的popupwindow中显示listview.docx
Android弹窗PopupWindow,实现代码
Android PopupWindow泡泡效果(有listview)