首页 > 移动平台 > 详细

android PopupWindow 自适应宽度

时间:2015-07-05 08:23:07      阅读:1043      评论:0      收藏:0      [点我收藏+]

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;  
}





android PopupWindow 自适应宽度

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

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!