popupwindow 居中

2022-05-18  本文已影响0人  细雨么么

    public void showHintPopUpWindow(View view, Context context){

        View layoutView= LayoutInflater.from(context).inflate(R.layout.t_menu_item_terminallist,null);
        PopupWindow pw=new PopupWindow(layoutView, ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        TextView tv=layoutView.findViewById(R.id.log_item);
        tv.setPadding(4,4,4,4);
        pw.setOutsideTouchable(true);
        pw.setBackgroundDrawable(new BitmapDrawable());
        pw.setFocusable(true);
        pw.setContentView(layoutView);
        layoutView.measure(View.MeasureSpec.UNSPECIFIED, View.MeasureSpec.UNSPECIFIED);
        int popWidth=layoutView.getMeasuredWidth();
        int popHeight=layoutView.getMeasuredHeight();
        int [] location=new int[2];
        view.getLocationInWindow(location);
        pw.showAtLocation(view,Gravity.NO_GRAVITY,
                location[0]+ view.getWidth()/2-popWidth/2,
                    location[1]+popHeight
                );
    }
上一篇 下一篇

猜你喜欢

热点阅读