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; }
时间: 2024-10-14 02:45:59