本文共 3726 字,大约阅读时间需要 12 分钟。
// 获取弹出的PopupWindow的界面View mContentView = View.inflate(context, R.layout.popupwindow, null);// 创建一个PopupWindow并默认获取焦点(如果没有焦点view无法监听到点击事件)PopupWindow mPopupWindow = new PopupWindow(mContentView, WindowManager.LayoutParams.WRAP_CONTENT, WindowManager.LayoutParams.WRAP_CONTENT, true);// ***给PopupWindow设置背景mPopupWindow.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));mPopupWindow.update();
/** * 显示PopupWindow * * @param parent 父控件 */public void showPopupWindow(View parent) { // 获取父控件在窗体中的位置 int[] location = new int[2]; parent.getLocationInWindow(location); // 将PopupWindow显示在指定位置上 mPopupWindow.showAtLocation(parent, Gravity.TOP | Gravity.START, location[0], location[1] + parent.getHeight());}
/** * 关闭PopupWindow */public void dismiss() { if (null != mPopupWindow && mPopupWindow.isShowing()) { mPopupWindow.dismiss(); }}
// 设置PopupWindow之外的其他位置消失mPopupWindow.setOutsideTouchable(true);
// 监听点击返回键mContentView.setOnKeyListener(new View.OnKeyListener() { @Override public boolean onKey(View v, int keyCode, KeyEvent event) { switch (keyCode) { case KeyEvent.KEYCODE_BACK: // 按返回键关闭PopupWindow dismiss(); return true; default: return false; } }});
/** * 开关 * @param parent 父控件 */public void toggle(View parent) { if (mPopupWindow.isShowing()) { dismiss(); } else { showPopupWindow(parent); }}
package kong.qingwei.combinedchartdemo.view;import android.content.Context;import android.view.Gravity;import android.view.KeyEvent;import android.view.View;import android.view.WindowManager;import android.widget.PopupWindow;import kong.qingwei.combinedchartdemo.R;/** * Created by kqw on 2016/5/17. * MyPopupWindow */public class MyPopupWindow extends PopupWindow implements View.OnKeyListener { private PopupWindow mPopupWindow; private final View mContentView; public MyPopupWindow(Context context) { // 获取弹出的PopupWindow的界面 mContentView = View.inflate(context, R.layout.popupwindow, null); // 监听点击返回键 mContentView.setOnKeyListener(this); // 创建一个PopupWindow并默认获取焦点(如果没有焦点view无法监听到点击事件) mPopupWindow = new PopupWindow(mContentView, WindowManager.LayoutParams.WRAP_CONTENT, WindowManager.LayoutParams.WRAP_CONTENT, true); // 设置PopupWindow之外的其他位置消失 mPopupWindow.setOutsideTouchable(true); } /** * 显示PopupWindow * * @param parent 父控件 */ public void showPopupWindow(View parent) { // 获取父控件在窗体中的位置 int[] location = new int[2]; parent.getLocationInWindow(location); // 将PopupWindow显示在指定位置上 mPopupWindow.showAtLocation(parent, Gravity.TOP | Gravity.START, location[0], location[1] + parent.getHeight()); } /** * 关闭PopupWindow */ public void dismiss() { if (null != mPopupWindow && mPopupWindow.isShowing()) { mPopupWindow.dismiss(); } } /** * 开关 * @param parent 父控件 */ public void toggle(View parent) { if (mPopupWindow.isShowing()) { dismiss(); } else { showPopupWindow(parent); } } @Override public boolean onKey(View v, int keyCode, KeyEvent event) { switch (keyCode) { case KeyEvent.KEYCODE_BACK: // 按返回键关闭PopupWindow dismiss(); return true; default: return false; } }}
// PopupWindowMyPopupWindow myPopupWindow = new MyPopupWindow(context);// 显示 myPopupWindow.showPopupWindow(parentView);// 开关myPopupWindow.toggle(parentView);