<span style="color: rgb(70, 70, 70); font-family: simsun; font-size: 14px; line-height: 21px; background-color: rgb(234, 247, 253);">弹出窗口(PopupWindow)用来显示一个的任意的视图(view),通常浮动在当前Acitivty之上。 </span>
<ol><li>
</li></ol>
<pre name="code" class="java"> /**
* 菜单显示popupwindow
*/
private void showMenuView() {
if (menuWindow == null) {
View view = LayoutInflater.from(mActivity).inflate(
R.layout.popup_menu_view, null);
view.findViewById(R.id.one_week_btn).setOnClickListener(this);
view.findViewById(R.id.three_week_btn).setOnClickListener(this);
view.findViewById(R.id.one_month_btn).setOnClickListener(this);
view.findViewById(R.id.three_month_btn).setOnClickListener(this);
//实例化PopupWindow对象
menuWindow = new PopupWindow(view, UnitUtil.dip2Px(
mActivity, 150), LayoutParams.WRAP_CONTENT);
menuWindow.setAnimationStyle(R.style.popop_menu_anim_style);
menuWindow.setBackgroundDrawable(new BitmapDrawable());
menuWindow.setOutsideTouchable(true);
menuWindow.setFocusable(true);
menuWindow.update();
menuWindow.showAsDropDown(menuBtn);
} else {
menuWindow.showAsDropDown(menuBtn);
}
}
构造方法1:
public PopupWindow(Context context, AttributeSet attrs) {
this(context, attrs, com.android.internal.R.attr.popupWindowStyle);
}
//defStyle 窗口样式
public PopupWindow(Context context, AttributeSet attrs, int defStyle
构造方法2:
public PopupWindow(View contentView, int width, int height, boolean focusable)
2、PopupWindow显示和隐藏的动画设置
<!-- PopupWindow窗口淡入淡出动画 -->
<style name="AnimationFade">
<item name="android:windowEnterAnimation">@anim/fade_in</item>
<item name="android:windowExitAnimation">@anim/fade_out</item>
</style>
<!-- 动画属性-->
<set xmlns:android="http://schemas.android.com/apk/res/android" >
<scale
android:duration="300"
android:fromXScale="1.0"
android:fromYScale="0.0"
android:pivotX="100%"
android:pivotY="0%"
android:toXScale="1.0"
android:toYScale="1.0" />
<alpha
android:duration="300"
android:fromAlpha="0.0"
android:interpolator="@android:anim/decelerate_interpolator"
android:toAlpha="1.0" />
</set>
<set xmlns:android="http://schemas.android.com/apk/res/android" >
<scale
android:duration="300"
android:fromXScale="1.0"
android:fromYScale="1.0"
android:pivotX="100%"
android:pivotY="0%"
android:toXScale="1.0"
android:toYScale="0.0" />
<alpha
android:duration="300"
android:fromAlpha="1.0"
android:interpolator="@android:anim/accelerate_interpolator"
android:toAlpha="0.0" />
</set>
3、PopupWindow常用方法
//改变弹出窗口的背景,当然也可以设置为NULL。
public void setBackgroundDrawable(Drawable background)
//获得弹出窗口背景。
public Drawable getBackground()
//改变窗口显示与消失时的动态样式
public void setAnimationStyle(int animationStyle)
注:如果窗口已经显示过,更改此值只能在下一次显示时起作用,或者调用update()方法。
//获得弹出窗口显示与消失时的动态样式the animation style
public int getAnimationStyle()
//设置弹出窗口包含的视图
public void setContentView(View contentView)
//获得弹出窗口包含的视图
public View getContentView()
//返回当前弹出窗口是否可获得焦点
public boolean isFocusable()
//设置弹出窗口是否可获得焦点,默认false
public void setFocusable(boolean focusable)
//判断当前窗口是否已显示
public boolean isShowing()
/*
*指定弹出窗口显示的位置。
*第一个参数指定PopupWindow的锚点view,即依附在哪个view上。
*第二个参数指定起始点为parent的右下角,第三个参数设置以parent的右下角为原点,向左、上各偏移10像素。
*/
public void showAtLocation(View parent, int gravity, int x, int y)
//将PopupWindow作为anchor的下拉窗口显示。即在anchor的左下角显示
window.showAsDropDown(anchor);
//xoff,yoff基于anchor的左下角进行偏移。
window.showAsDropDown(anchor, xoff, yoff);
//更新弹出窗口的状态, 一些状态:
public void update()
setClippingEnabled(boolean)
//设置PopupWindow可获得焦点
setFocusable(boolean)
setIgnoreCheekPress()
//设置PopupWindow可触摸
setTouchable(boolean)
setAnimationStyle(int)
setInputMethodMode(int)
setAnimationStyle(int)