Android 自定义弹出菜单 PopWindow

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,不过太麻烦了。

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值