PopWindow使用总结

谈起popwindow的显示问题,显示在view上方:

    public class ImChatItemPopWindow extends PopupWindow{
    private Context mContext;
    private View mParentView;
    private View convertView;
     public ImChatItemPopWindow(Context context)
    {
        super(context);
        mContext = context;
        convertView = View.inflate(context, R.layout.im_chat_item_popup, null);
        this.setContentView(convertView);
        setWidth(WindowManager.LayoutParams.WRAP_CONTENT);
        setHeight(WindowManager.LayoutParams.WRAP_CONTENT);
        this.setOutsideTouchable(true);
        /** 为其设置背景,使得其内外焦点都可以获得 */
        this.setBackgroundDrawable(new ColorDrawable());
        this.setFocusable(true);
      }
      public void showPopWindow(View v){
        int[] location = new int[2];
        v.getLocationOnScreen(location);
        View popupView = LayoutInflater.from(UiApplication.getCurrentContext()).inflate(R.layout.im_chat_item_popup, null);
        PopupWindow popupWindow = new PopupWindow(popupView, ViewGroup.LayoutParams.WRAP_CONTENT,
                ViewGroup.LayoutParams.WRAP_CONTENT, true);
        popupWindow.setBackgroundDrawable(new BitmapDrawable());
        popupView.measure(View.MeasureSpec.UNSPECIFIED, View.MeasureSpec.UNSPECIFIED);
        int popupWidth = popupView.getMeasuredWidth();
        int popupHeight =  popupView.getMeasuredHeight();

        popupWindow.showAtLocation(v, Gravity.NO_GRAVITY, location[0],//(location[0]+v.getWidth()/2)-popupWidth/2,
                location[1]-popupHeight);
        }   
}            

特别注意高度处 是减去 popupHeight,不是parentView.getHeight(),网上很多事减去 popwindow.getHeight(),在这里即为 减去this.getHeight(),小编试了一下,效果不正常(-this.getHeight())。

你可能感兴趣的:(android)