android PopupWindow 自适应宽度

时间:2015-07-05 08:23:07   收藏:0   阅读:1044

PopupWindow弹出的菜单随内容的宽度决定,目前找到自认为比较好的办法是,重写ListView的onMeasure()方法。然后就可以实现了,在此mark一下。

@Override 
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {        
     int maxWidth = meathureWidthByChilds() + getPaddingLeft() + getPaddingRight(); super.onMeasure(MeasureSpec.makeMeasureSpec(maxWidth,MeasureSpec.EXACTLY),heightMeasureSpec);   
}   
   public int meathureWidthByChilds() {     
      int maxWidth = 0;    
      View view = null;
      for (int i = 0; i < getAdapter().getCount(); i++) { 
           view = getAdapter().getView(i, view, this);     
           view.measure(MeasureSpec.UNSPECIFIED, MeasureSpec.UNSPECIFIED);     
      if (view.getMeasuredWidth() > maxWidth){        
         maxWidth = view.getMeasuredWidth();    
      }       
  }       
   return maxWidth;  
}





原文:http://my.oschina.net/piaofudeyu/blog/474657

评论(0
© 2014 bubuko.com 版权所有 - 联系我们:wmxa8@hotmail.com
打开技术之扣,分享程序人生!