PopupWindow

来源:互联网 发布:linux目录结构命令 编辑:程序博客网 时间:2024/04/29 19:52
一、PopupWindow使用步骤:
step1、创建popupWindow样式的xml
step2、添加代码
popupWindow = new PopupWindow(contentView,ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.WRAP_CONTENT, true);
popupWindow.setBackgroundDrawable(new BitmapDrawable());//点击空白关闭popWindow
popupWindow.setAnimationStyle(R.style.animation);//设置动画
popupWindow.showAtLocation(parent, Gravity.BOTTOM, 0, 0);//设置popupWindwo位置

popupWindow.dismiss();//关闭popupWindow
step3、创建动画out.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android" >

<alpha
android:duration="500"
android:fromAlpha="1.0"
android:toAlpha="0.0" />

<translate
android:duration="500"
android:fromYDelta="0"
android:toYDelta="100%p" />

</set>
和动画enter.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android" >

<alpha
android:duration="500"
android:fromAlpha="0.0"
android:toAlpha="1.0" />

<translate
android:duration="500"
android:fromYDelta="100%p"
android:toYDelta="0" />

</set>
step4、添加style属性
<style name="animation">
<item name="android:windowEnterAnimation">@anim/enter</item>
<item name="android:windowExitAnimation">@anim/out</item>
</style>

二、PopupWindw讲解
1、PopupWindow与AlertDialog的区别
最关键的区别是AlertDialog不能指定显示位置,只能默认显示在屏幕最中间(当然也可以通过设置WindowManager参数来改变位置)。而PopupWindow是可以指定显示位置的,随便哪个位置都可以,更加灵活。

2、PopupWindow的相关函数
(1)、构造函数:
//方法一:
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最基本的三个条件是一定要设置的:View contentView,int width, int height ;少任意一个就不可能弹出来PopupWindow!!!!
一般用四个参数的构造,最后个参数设为true
(2)显示函数
显示函数主要使用下面三个:
//相对某个控件的位置(正左下方),无偏移
showAsDropDown(View anchor):
//相对某个控件的位置,有偏移;xoff表示x轴的偏移,正值表示向左,负值表示向右;yoff表示相对y轴的偏移,正值是向下,负值是向上;
showAsDropDown(View anchor, int xoff, int yoff):
//相对于父控件的位置(例如正中央Gravity.CENTER,下方Gravity.BOTTOM等),可以设置偏移或无偏移
showAtLocation(View parent, int gravity, int x, int y):
(3)、其它函数
public void dismiss() //取消popupWindow

public void setTouchable(boolean touchable) :
设置PopupWindow是否响应touch事件,默认是true,如果设置为false,即会是下面这个结果:(所有touch事件无响应,包括点击事件)

public void setFocusable(boolean focusable) :
该函数的意义表示,PopupWindow是否具有获取焦点的能力,默认为False。一般来讲是没有用的,因为普通的控件是不需要获取焦点的,而对于EditText则不同,如果不能获取焦点,那么EditText将是无法编辑的。

public void setOutsideTouchable(boolean touchable) :
这个函数的意义,就是指,PopupWindow以外的区域是否可点击,即如果点击PopupWindow以外的区域,PopupWindow是否会消失。

public void setBackgroundDrawable(Drawable background) :
这个函数可是吊了,这个函数不只能设置背景,因为你加上它之后,setOutsideTouchable()才会生效;而且,只有加上它之后,PopupWindow才会对手机的返回按钮有响应:即,点击手机返回按钮,可以关闭PopupWindow;如果不加setBackgroundDrawable()将关闭的PopupWindow所在的Activity.

public void setAnimationStyle(int animationStyle) //设置动画

<style name="animation">
<item name="android:windowEnterAnimation">@anim/enter</item>
<item name="android:windowExitAnimation">@anim/out</item>
</style>
(4)、添加阴影效果
其实原理很简单,使PopupWindow的界面充满全屏,把背景用半透明黑色遮罩住,像弹出AlertDialog一
样的效果。

参考文献:http://blog.csdn.net/harvic880925/article/details/49272285
0 0
原创粉丝点击