今天工作不忙,所以就把这些年做Android开发的一些经验技巧陆续写到博客上,一来是做自己的备忘,二来是可以帮助大家提高自己的技能。

大家在做下面的弹出层时用的应该都是android的PopupWindow吧(不排除有人直接用ListView盖在上面的),

那么PopupWindow是怎么显示在“全部商圈”的正下方呢?这里就要计算一下了,但是并不是每个人都擅长去计算的。

那我把自己写的计算的代码贴出来,可能写的不够好,如果你有更好的算法,欢迎跟帖。

Android在指定View的下方显示PopupWindow_第1张图片

    public static final int defaultBotom = -100;    /**     * @param activity      Activity     * @param attachOnView  显示在这个View的下方     * @param popView       被显示在PopupWindow上的View     * @param popShowHeight 被显示在PopupWindow上的View的高度,可以传默认值defaultBotom     * @param popShowWidth  被显示在PopupWindow上的View的宽度,一般是传attachOnView的getWidth()     * @return PopupWindow     */    public static PopupWindow show(Activity activity, View attachOnView, View popView, final int popShowHeight, final int popShowWidth) {        if (popView != null && popView.getParent() != null) {            ((ViewGroup) popView.getParent()).removeAllViews();        }        PopupWindow popupWindow = null;        Rect frame = new Rect();        activity.getWindow().getDecorView().getWindowVisibleDisplayFrame(frame);        int location[] = new int[2];        int x, y;        int popHeight = 0, popWidth = 0;        attachOnView.getLocationOnScreen(location);        x = location[0];        y = location[1];        int h = attachOnView.getHeight();        int screenHeight = DeviceInfoManager.getInstance().getScreenHeight();        if (popShowHeight == defaultBotom) {            popHeight = screenHeight / 6;            popHeight = Math.abs(screenHeight - (h + y)) - popHeight;        } else if (popHeight == ViewGroup.LayoutParams.WRAP_CONTENT) {            popHeight = ViewGroup.LayoutParams.WRAP_CONTENT;        } else {            popHeight = popShowHeight;        }        if (popShowWidth == ViewGroup.LayoutParams.WRAP_CONTENT) {            popWidth = attachOnView.getWidth();        } else {            popWidth = popShowWidth;        }        popupWindow = new PopupWindow(popView, popWidth, popHeight, true);        //这行代码时用来让PopupWindow点击区域之外消失的,这个应该是PopupWindow的一个bug。        //但是利用这个bug可以做到点击区域外消失        popupWindow.setBackgroundDrawable(new BitmapDrawable());        popupWindow.setAnimationStyle(R.style.PopupAnimationDown);        popupWindow.showAtLocation(attachOnView, Gravity.NO_GRAVITY, x, h + y);        popupWindow.update();        return popupWindow;    }


用的时候大致是这么用:

PopupWindow pop_window = ShowPopList.show(mActivity, mWhichView, mCategoryFilterView, ShowPopList.defaultBotom, -1);


有问题联系我的新浪微博:@弹棉花的孩子

更多相关文章

  1. 代码混淆—android被反编译的两种解决方案
  2. Android开发之如何手写代码进行页面布局
  3. Android跳转淘宝商品详情页代码
  4. android通过Intent调用手机图片,音频,视频录音拍照等代码
  5. Android开源项目:微信打飞机游戏源代码
  6. android 开发常用代码备查[更新20150520]

随机推荐

  1. Android开发者网址导航
  2. Android事件处理
  3. Android之——自动挂断电话的实现
  4. android animation动画
  5. Android编译系统分析大全
  6. Android(安卓)TextView属性详解
  7. Android中以JAR形式封装控件 或者类库
  8. 2019最新Android常用开源库总结(附带githu
  9. android 是通过 ksoap 连接 java xfire w
  10. 我所理解的Android模块化(二)——模块通信