Android中使PopupWindow显示在指定控件的上下左右!

发布时间:2019-08-06 发布网站:脚本宝典
脚本宝典收集整理的这篇文章主要介绍了Android中使PopupWindow显示在指定控件的上下左右!脚本宝典觉得挺不错的,现在分享给大家,也给大家做个参考。

**1.View中的方法getLocationOnScreen(int[] location):获取该控件在屏幕中的绝对坐标并将坐标保存在数组中:如下图所示,A为屏幕的原点,R,C点即为view的绝对坐标的位置
getLocationInWindow(int[] location) :获取控件相对于父窗口的坐标,如c在R中,获取c在R中的坐标**

clipboard.png
2.

public void click1(View v) {
       
        View view = View.inflate(this, R.layout.diaLOG, null);
        //获取PopupWindow中View的
        view.measure(View.MeasureSPEc.UNSPECIFIED, View.MeasureSpec.UNSPECIFIED);
        int measuredWidth = view.getMeasuredWidth();
        int measuredHeight = view.getMeasuredHeight();
        PopupWindow popupWindow = new PopupWindow(view, getResources().getDisplayMetrics().widthPixels
                , getResources().getDisplayMetrics().heightPixels);
        popupWindow.setFocusable(true);//popupwindow设置焦点

        popupWindow.setBackgroundDrawable(new ColorDrawable(0xaa000000));//设置背景
        popupWindow.setOutsideTouchable(true);//点击外面窗口消失
        // popupWindow.showAsDropDown(v,0,0);
        //获取点击View的坐标
        int[] location = new int[2];
        v.getLocationOnScreen(location);
        popupWindow.showAsDropDown(v);//在v的下面
        //显示在上方
        popupWindow.showAtLocation(v,GravITy.NO_GRAVITY,location[0]+v.getWidth()/2,location[1]-measuredHeight);
       //显示在正上方
        popupWindow.showAtLocation(v, Gravity.NO_GRAVITY, (location[0] + v.getWidth() / 2) - measuredWidth / 2, location[1]-measuredHeight);
           //显示在左方
        popupWindow.showAtLocation(v,Gravity.NO_GRAVITY,location[0]-popupWindow.getWidth(),location[1]);
        //显示在下方
        popupWindow.showAtLocation(v,Gravity.NO_GRAVITY,location[0]+v.getWidth(),location[1]);
       popupWindow.setAnimationStyle(andROId.R.style.Animation_Translucent);//设置动画
     
    }

脚本宝典总结

以上是脚本宝典为你收集整理的Android中使PopupWindow显示在指定控件的上下左右!全部内容,希望文章能够帮你解决Android中使PopupWindow显示在指定控件的上下左右!所遇到的问题。

如果觉得脚本宝典网站内容还不错,欢迎将脚本宝典推荐好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。