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]);
            }
        });

完毕。

相关推荐
大猫熊猫27 分钟前
Android Studio 加载多个FLutter项目
android·ide·android studio
!!!+++27 分钟前
Android Studio打开Modem模块出现:The project ‘***‘ is not a Gradle-based project
android·ide·android studio
Tom哈哈2 小时前
Android命令行查看CPU频率和温度
android
帅得不敢出门2 小时前
安卓framework美化手势导航侧滑返回UI
android·java·ui·framework·安卓·开发·定制
AI原吾3 小时前
探索SVG的奥秘:Python中的svgwrite库
android·开发语言·python·svgwrite
仙魁XAN7 小时前
Unity 之 【Android Unity FBO渲染】之 [Unity 渲染 Android 端播放的视频] 的一种方法简单整理
android·unity·共享纹理·fbo·视频渲染
陆业聪7 小时前
从状态管理到性能优化:全面解析 Android Compose
android·ui
轻口味10 小时前
Android JobScheduler介绍
android
技术无疆10 小时前
TitleBar:打造高效Android标题栏的新选择
android·java·ui·android studio·android-studio
时差freebright14 小时前
【Visual Studio 报错】vs 在使用二进制写入文件时弹窗报错:使用简体中文 gb2312 编码加载文件
android·java·visual studio