Android PopupWindow弹窗动态显示在View的上下方,

序、周末不加班,

效果图如下。

我们要弹出的PopupWindow在View的下方,如果下方区域不够,则弹出在上方。

实现方案思路

我们在显示的时候,首先去计算一下弹窗高度。使用屏幕的高 - popupwind的高并且和popup的高做对比,如果大于的话,则显示在下方,如果小于的话,则显示在上方。

java 复制代码
 /**
     *
     * @param anchorView 锚点View
     * @param contentView 弹出的View
     * @return
     */
    private int[] calculatePopWindowPos(final View anchorView, final View contentView) {
        final int[] windowPos = new int[2];
        final int[] anchorLoc = new int[2];
        // 获取锚点View在屏幕上的左上角坐标位置
        anchorView.getLocationOnScreen(anchorLoc);
        final int anchorHeight = anchorView.getHeight();
        // 获取屏幕的高宽
        final int screenHeight = getScreenHeight(anchorView.getContext());
        final int screenWidth = getScreenWidth(anchorView.getContext());
        contentView.measure(View.MeasureSpec.UNSPECIFIED, View.MeasureSpec.UNSPECIFIED);
        // 计算contentView的高宽
        final int windowHeight = contentView.getMeasuredHeight();
        final int windowWidth = contentView.getMeasuredWidth();
        // 判断需要向上弹出还是向下弹出显示
        final boolean isNeedShowUp = (screenHeight - anchorLoc[1] - anchorHeight < windowHeight);
        Log.i("AAAA", "screenWidth = "+screenWidth+"---"+"windowWidth="+windowWidth);
        if (isNeedShowUp) {
            windowPos[0] = (screenWidth - windowWidth)/2;
            windowPos[1] = anchorLoc[1] - windowHeight;
        } else {
            windowPos[0] = (screenWidth - windowWidth)/2;
            windowPos[1] = anchorLoc[1] + anchorHeight;
        }
        return windowPos;
    }

显示popupwindow如下

java 复制代码
 Button btn1 = findViewById(R.id.btn1);
        btn1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                View contentView = LayoutInflater.from(ListActivity.this).inflate(R.layout.layout_window, null);
                PopupWindow popupWindow = new PopupWindow(contentView,ViewGroup.LayoutParams.WRAP_CONTENT , ViewGroup.LayoutParams.WRAP_CONTENT);
                // 设置PopupWindow可以获得焦点,否则无法响应点击事件
                popupWindow.setFocusable(true);
//                popupWindow.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));

                int[] windowPos = calculatePopWindowPos(view, contentView);
                // 3. 显示PopupWindow
           
                popupWindow.showAtLocation(view,Gravity.TOP|Gravity.START, windowPos[0], windowPos[1]);
            }
        });

完毕。

相关推荐
码农的小菜园2 小时前
Android的Locale学习笔记
android·笔记·学习
帅次2 小时前
链路到端上:HTTPS 之后安全题还在考什么
android·okhttp·glide·zygote·retrofit
游戏开发爱好者82 小时前
深入理解iOSTime Profiler:提升iOS应用性能的关键工具
android·ios·小程序·https·uni-app·iphone·webview
帅次3 小时前
Android 高级工程师面试参考答案:架构设计、Jetpack 与 Compose
android·面试·职场和发展·架构·composer·jetpack
limingade3 小时前
Dialer3.0智能拨号器Android版功能说明书
android·蓝牙电话·手机转sip·手机蓝牙·智能拨号器
JJay.3 小时前
Android BLE 的 notify 和 indicate 到底有什么区别
android
橙子199110163 小时前
Android 异步任务和消息机制
android
被开发耽误的大厨3 小时前
5、Integer缓存池里同一个对象指的是什么?Integer 和String 内存结构逻辑完全一样?
android·java·哈希算法
NoSi EFUL12 小时前
MySQL中ON DUPLICATE KEY UPDATE的介绍与使用、批量更新、存在即更新不存在则插入
android·数据库·mysql
安小牛14 小时前
Android 开发汉字转带声调的拼音
android·java·学习·android studio