PopupWindow的使用:
- 调用PopupWindow的构造器创建PopupWindow对象,并完成一些初始化设置。
- 调用PopupWindow的showAsDropDown(View view)将PopupWindow作为View组件的下拉组件显示出来;或调用PopupWindow的showAtLocation()方法将PopupWindow在指定位置显示出来。
基本应用代码:
PopupWindow popupWindow = new PopupWindow(MainActivity.this);
popupWindow.setWidth(ViewGroup.LayoutParams.WRAP_CONTENT);
popupWindow.setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);
//要展示的布局
View view = LayoutInflater.from(MainActivity.this).inflate(R.layout.popup_layout, null);
popupWindow.setContentView(view);
//必须添加
popupWindow.setBackgroundDrawable(new ColorDrawable(0x00000000));
//点击内容区域外的区域是否关闭窗口,那么设置为true应该就是表示关闭,设置为false就是表示不关闭
popupWindow.setOutsideTouchable(false);
//让PopupWindow获得焦点,如果PopupWindow中有Editor的话,focusable必须要为true
popupWindow.setFocusable(true);
popupWindow.showAsDropDown(button);
PopupWindow的显示:
- popupWindow.showAsDropDown(button); 在button组件正下方显示,popupWindow同button左下角对齐
- popupWindow.showAsDropDown(button, 100, 100); 在button组件正下方显示,x、y方向
- popupWindow.showAtLocation(ll_mian_container, Gravity.NO_GRAVITY, 0, 0); 在左上角显示
- popupWindow.showAtLocation(tvContent, Gravity.LEFT | Gravity.TOP, 0, 0)在左上角显示
注意:当popupWindow位置偏移出屏幕时,popupWindow会自动调整其位置,使其全部显示在当前屏幕中
以下是自定义的BasePopupWindow:
package com.et.apppopupwindows;
import android.animation.ValueAnimator;
import android.app.Activity;
import android.content.Context;
import android.graphics.drawable.ColorDrawable;
import android.graphics.drawable.Drawable;
import android.view.KeyEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.PopupWindow;
/**
* 项目名称:AppPopupWindows
* 类描述:
* 创建人:Administrator
* 创建时间:2016/6/8 0008 上午 11:32
* 修改人:Administrator
* 修改时间:2016/6/8 0008 上午 11:32
* 修改备注:
*/
public class BasePopupWindow extends PopupWindow {
private Context mContext;
private float mShowAlpha = 0.88f;
private Drawable mBackgroundDrawable;
public BasePopupWindow(Context context) {
this.mContext = context;
initBasePopupWindow();
}
@Override
public void setOutsideTouchable(boolean touchable) {
super.setOutsideTouchable(touchable);
if(touchable) {
if(mBackgroundDrawable == null) {
mBackgroundDrawable = new ColorDrawable(0x00000000);
}
super.setBackgroundDrawable(mBackgroundDrawable);
} else {
super.setBackgroundDrawable(null);
}
}
@Override
public void setBackgroundDrawable(Drawable background) {
mBackgroundDrawable = background;
setOutsideTouchable(isOutsideTouchable());
}
/**
* 初始化BasePopupWindow的一些信息
* */
private void initBasePopupWindow() {
setAnimationStyle(android.R.style.Animation_Dialog);
setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);
setWidth(ViewGroup.LayoutParams.WRAP_CONTENT);
setOutsideTouchable(true); //默认设置outside点击无响应
setFocusable(true);
}
@Override
public void setContentView(View contentView) {
if(contentView != null) {
contentView.measure(View.MeasureSpec.UNSPECIFIED, View.MeasureSpec.UNSPECIFIED);
super.setContentView(contentView);
addKeyListener(contentView);
}
}
public Context getContext() {
return mContext;
}
@Override
public void showAtLocation(View parent, int gravity, int x, int y) {
super.showAtLocation(parent, gravity, x, y);
showAnimator().start();
}
@Override
public void showAsDropDown(View anchor) {
super.showAsDropDown(anchor);
showAnimator().start();
}
@Override
public void showAsDropDown(View anchor, int xoff, int yoff) {
super.showAsDropDown(anchor, xoff, yoff);
showAnimator().start();
}
@Override
public void showAsDropDown(View anchor, int xoff, int yoff, int gravity) {
super.showAsDropDown(anchor, xoff, yoff, gravity);
showAnimator().start();
}
@Override
public void dismiss() {
super.dismiss();
dismissAnimator().start();
}
/**
* 窗口显示,窗口背景透明度渐变动画
* */
private ValueAnimator showAnimator() {
ValueAnimator animator = ValueAnimator.ofFloat(1.0f, mShowAlpha);
animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
@Override
public void onAnimationUpdate(ValueAnimator animation) {
float alpha = (float) animation.getAnimatedValue();
setWindowBackgroundAlpha(alpha);
}
});
animator.setDuration(360);
return animator;
}
/**
* 窗口隐藏,窗口背景透明度渐变动画
* */
private ValueAnimator dismissAnimator() {
ValueAnimator animator = ValueAnimator.ofFloat(mShowAlpha, 1.0f);
animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
@Override
public void onAnimationUpdate(ValueAnimator animation) {
float alpha = (float) animation.getAnimatedValue();
setWindowBackgroundAlpha(alpha);
}
});
animator.setDuration(320);
return animator;
}
/**
* 为窗体添加outside点击事件
* */
private void addKeyListener(View contentView) {
if(contentView != null) {
contentView.setFocusable(true);
contentView.setFocusableInTouchMode(true);
contentView.setOnKeyListener(new View.OnKeyListener() {
@Override
public boolean onKey(View view, int keyCode, KeyEvent event) {
switch (keyCode) {
case KeyEvent.KEYCODE_BACK:
dismiss();
return true;
default:
break;
}
return false;
}
});
}
}
/**
* 控制窗口背景的不透明度
* */
private void setWindowBackgroundAlpha(float alpha) {
Window window = ((Activity)getContext()).getWindow();
WindowManager.LayoutParams layoutParams = window.getAttributes();
layoutParams.alpha = alpha;
window.setAttributes(layoutParams);
}
}
BasePopupWindow basePopupWindow = new BasePopupWindow(MainActivity.this);
basePopupWindow.setOutsideTouchable(true);
basePopupWindow.setFocusable(true);
View view = LayoutInflater.from(MainActivity.this).inflate(R.layout.popup_layout, null);
basePopupWindow.setContentView(view);
basePopupWindow.showAtLocation(ll_mian_container, Gravity.CENTER, 0, 0);
参考文章地址: http://www.jianshu.com/p/825d1cc9fa79