欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Android 自定义弹出菜单 PopWindow

程序员文章站 2022-05-31 15:38:33
...
public class CustomWin extends PopupWindow {
        private Context context;
        private View view;
        private ListView listView;
        private List<String> list;
        
        public CustomWin(Context context) {
            this(context, ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        }
        public View getView()
        {
            return view;
        }
    
        
        public CustomWin(Context context, int width, int height) {
            this.context = context;
            setWidth(width);
            setHeight(height);
            //设置可以获得焦点
            setFocusable(true);
            //设置弹窗内可点击
            setTouchable(true);
            //设置弹窗外可点击
            setOutsideTouchable(true);
            setBackgroundDrawable(new BitmapDrawable());
            view = LayoutInflater.from(context).inflate(R.layout.custompopwin_layout,null);
            setContentView(view);
            setAnimationStyle(R.style.PopupAnimation);
            initData();
        }
        
        private void initData() {
            listView = (ListView) view.findViewById(R.id.title_list);
            list = new ArrayList<String>();
            list.add("添加好友");
            list.add("扫一扫");
            list.add("支付宝");
            list.add("视频聊天");
            //设置列表的适配器
            listView.setAdapter(new BaseAdapter() {
                @Override
                public View getView(int position, View convertView, ViewGroup parent) {
                    TextView textView = null;
                    
                    if (convertView == null) {
                        textView = new TextView(context);
                        textView.setTextColor(Color.rgb(255,255,255));
                        textView.setTextSize(14);
                        //设置文本居中
                        textView.setGravity(Gravity.LEFT);
                        //设置文本域的范围
                        textView.setPadding(0, 13, 0, 13);
                        //设置文本在一行内显示(不换行)
                        textView.setSingleLine(true);
                    } else {
                        textView = (TextView) convertView;
                    }
                    //设置文本文字
                    textView.setText(list.get(position));
                    //设置文字与图标的间隔
//                textView.setCompoundDrawablePadding(0);
//                //设置在文字的左边放一个图标
//                textView.setCompoundDrawablesWithIntrinsicBounds(R.mipmap., null, null, null);
                    
                    return textView;
                }
                
                @Override
                public long getItemId(int position) {
                    return position;
                }
                
                @Override
                public Object getItem(int position) {
                    return list.get(position);
                }
                
                @Override
                public int getCount() {
                    return list.size();
                }
            });
        }
    }

window.showAsDropDown(view1);

这种现实方式可以直接在锚点view下面弹出,不用计算坐标

如果是需要在锚点的上面弹出,则不适合,需要另外一种计算方法

public static 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 = ScreenUtils.getScreenHeight(anchorView.getContext());
        final int screenWidth = ScreenUtils.getScreenWidth(anchorView.getContext());
        // 测量contentView
        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);
        if (isNeedShowUp) {
            windowPos[0] = screenWidth - windowWidth;
            windowPos[1] = anchorLoc[1] - windowHeight;
        } else {
            windowPos[0] = screenWidth - windowWidth;
            windowPos[1] = anchorLoc[1] + anchorHeight;
        }
        return windowPos;
    }

不过这种计算方式对于listview,计算可能不准,可能需要重载listview onMeasure,不过太麻烦了。

各有利弊,能解决问题不深入研究了

相关标签: PopupWindow