工作中已经使用很多次popwindow了。但是一直没有好好总结,今天乘此机会把popwindow过一遍。在activity中我们可以选择使用popwindow和dialog弹出某一个小界面,但是默认情况下dialog是居中显示的,popwindow支持任何位置,先比较而言更新灵活。大致分一下几个方面吧。
一、popwindow的基本方法
二、popwindow使用实例
三、popwindow动画
四、popwindow几个方法解析
一、popwindow的基本方法
首先我们来看看构造函数
public PopupWindow()
public PopupWindow(View contentView)
public PopupWindow(int width, int height)
public PopupWindow(View contentView, int width, int height)
public PopupWindow(View contentView, int width, int height, boolean focusable)
平时我们使用最多的就是第三个构造函数。请注意,在使用popwindow时,有三要素,缺一不可,contextView、width、height。
再看看显示的方法。
pop.showAsDropDown(anchor);
pop.showAsDropDown(anchor, xoff, yoff);
pop.showAsDropDown(anchor, xoff, yoff, gravity);
pop.showAtLocation(parent, gravity, x, y);
显示在某一个控件下方。或者是基于某一个控件显示。其中偏移量要注意一下,x大于0,是向左边,x小于0,是向右边。y大于0是向上边,y小于0是向下边。
二、popwindow使用实例
1. 比如在屏幕的底部显示出来。核心代码我贴出。
View contentView = LayoutInflater.from(this).inflate(R.layout.pop, null);
PopupWindow pop = new PopupWindow();
pop.setContentView(contentView);
pop.setWidth(ViewGroup.LayoutParams.WRAP_CONTENT);
pop.setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);
pop.setBackgroundDrawable(new BitmapDrawable());
pop.setOutsideTouchable(true);
View rootView = LayoutInflater.from(this).inflate(R.layout.main, null);
pop.showAtLocation(rootView, Gravity.BOTTOM, 0, 0);
我们来看看popwindow对应的布局
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<View
android:layout_width="match_parent"
android:layout_height="3dp"
android:background="#ff0000" />
<TextView
android:gravity="center"
android:id="@+id/tv_study"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="10dp"
android:text="学习" />
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="#333333" />
<TextView
android:gravity="center"
android:id="@+id/tv_life"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="10dp"
android:text="生活" />
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="#333333" />
<TextView
android:gravity="center"
android:id="@+id/tv_work"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="10dp"
android:text="工作" />
</LinearLayout>
根布局LinearLayout是指定大小了。为啥在创建popwindow时还需要指定宽和高呢。后边说明。
2. 模仿diglog的效果,弹出popwindow时,背景是有阴影效果的。
View contentView = LayoutInflater.from(this).inflate(R.layout.pop2, null);
PopupWindow pop = new PopupWindow();
pop.setContentView(contentView);
pop.setWidth(ViewGroup.LayoutParams.MATCH_PARENT);
pop.setHeight(ViewGroup.LayoutParams.MATCH_PARENT);
pop.setOutsideTouchable(true);
pop.showAsDropDown(btnShow);
三、popwindow动画
我们需要在style文件定义好动画
<style name="contextMenuAnim" parent="@android:style/Animation.Activity">
<item name="android:windowEnterAnimation">@anim/context_menu_enter</item>
<item name="android:windowExitAnimation">@anim/context_menu_exit</item>
</style>
context_menu_enter文件内容:
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate
android:duration="@android:integer/config_shortAnimTime"
android:fromXDelta="0"
android:fromYDelta="100%p"
android:interpolator="@android:anim/accelerate_decelerate_interpolator"
android:toXDelta="0"
android:toYDelta="0"/>
</set>
context_menu_exit文件内容:
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android" >
<translate
android:duration="@android:integer/config_shortAnimTime"
android:fromXDelta="0"
android:fromYDelta="0"
android:interpolator="@android:anim/accelerate_decelerate_interpolator"
android:toXDelta="0"
android:toYDelta="100%p" />
</set>
通过代码pop.setAnimationStyle(R.style.contextMenuAnim);设置动画。
四、popwindow几个方法解析
<span style="font-family:Microsoft YaHei;">public void setTouchable(boolean touchable)
public void setFocusable(boolean focusable)
public void setOutsideTouchable(boolean touchable)
public void setBackgroundDrawable(Drawable background)</span>
1. setTouchable
popwindow是否响应touch事件。设置为ture的话,表示响应。为fouse的话,表示不响应,那么这时候在contentView上边的view的单击事件都没有了。
2. setFocusable
popwindow是否具有获取焦点的能力。默认是为false。一般情况下我们也用不到。只有在popwindow的contentView中具有Edittext时,是需要焦点的话,如果没有焦点的话,就可能没法正常输入。
3. setOutsideTouchable
这个方法的意思就是popwindow以外的区域是否可以点击。设置为true以后,点击popwindow以外的区域,popwindow就会消失。但是要注意单独设置个属性是没有作用的。必须要同时设置两个属性才行。
pop.setBackgroundDrawable(new BitmapDrawable());
pop.setOutsideTouchable(true);
4. setBackgroundDrawable
设置这个方法以后,按下返回键,pop也能消失了。点击popwindow中的某一行也是能够消失的。具体原理可参考;http://blog.csdn.net/harvic880925/article/details/49278705