@Override protected void onMeasure(int widthSpec, int heightSpec) { final int widthMode = MeasureSpec.getMode(widthSpec); final int heightMode = MeasureSpec.getMode(heightSpec); if (DEBUG && widthMode != MeasureSpec.AT_MOST) { Log.w(TAG, "onMeasure: widthSpec " + MeasureSpec.toString(widthSpec) + " should be AT_MOST"); } if (DEBUG && heightMode != MeasureSpec.AT_MOST) { Log.w( TAG, "onMeasure: heightSpec " + MeasureSpec.toString(heightSpec) + " should be AT_MOST"); } final int widthSize = MeasureSpec.getSize(widthSpec); final int heightSize = MeasureSpec.getSize(heightSpec); int maxWidth = widthSize; int maxHeight = heightSize; final int count = getChildCount(); for (int i = 0; i < count; i++) { final View child = getChildAt(i); final LayoutParams lp = (LayoutParams) child.getLayoutParams(); if (lp.maxWidth > 0 && lp.maxWidth < maxWidth) { maxWidth = lp.maxWidth; } if (lp.maxHeight > 0 && lp.maxHeight < maxHeight) { maxHeight = lp.maxHeight; } } final int wPadding = getPaddingLeft() + getPaddingRight(); final int hPadding = getPaddingTop() + getPaddingBottom(); maxWidth = Math.max(0, maxWidth - wPadding); maxHeight = Math.max(0, maxHeight - hPadding); int width = widthMode == MeasureSpec.EXACTLY ? widthSize : 0; int height = heightMode == MeasureSpec.EXACTLY ? heightSize : 0; for (int i = 0; i < count; i++) { final View child = getChildAt(i); final LayoutParams lp = (LayoutParams) child.getLayoutParams(); final int childWidthSpec = makeChildMeasureSpec(maxWidth, lp.width); final int childHeightSpec = makeChildMeasureSpec(maxHeight, lp.height); child.measure(childWidthSpec, childHeightSpec); width = Math.max(width, Math.min(child.getMeasuredWidth(), widthSize - wPadding)); height = Math.max(height, Math.min(child.getMeasuredHeight(), heightSize - hPadding)); } setMeasuredDimension(width + wPadding, height + hPadding); }
@Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { Log.d(TAG, "Width spec: " + MeasureSpec.toString(widthMeasureSpec)); Log.d(TAG, "Height spec: " + MeasureSpec.toString(heightMeasureSpec)); int widthMode = MeasureSpec.getMode(widthMeasureSpec); int widthSize = MeasureSpec.getSize(widthMeasureSpec); int heightMode = MeasureSpec.getMode(heightMeasureSpec); int heightSize = MeasureSpec.getSize(heightMeasureSpec); int chosenWidth = chooseDimension(widthMode, widthSize); int chosenHeight = chooseDimension(heightMode, heightSize); int chosenDimension = Math.min(chosenWidth, chosenHeight); setMeasuredDimension(chosenDimension, chosenDimension); }
@Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { if (Constants.LOG_V) Log.v( TAG, "onMeasure(" + MeasureSpec.toString(widthMeasureSpec) + ", " + MeasureSpec.toString(heightMeasureSpec) + ")"); int wMode = MeasureSpec.getMode(widthMeasureSpec); int wSize = MeasureSpec.getSize(widthMeasureSpec); int hMode = MeasureSpec.getMode(heightMeasureSpec); int hSize = MeasureSpec.getSize(heightMeasureSpec); if (wMode == MeasureSpec.EXACTLY) { if (hMode == MeasureSpec.EXACTLY) { setSize(wSize, hSize); } else if (hMode == MeasureSpec.AT_MOST) { setSize(wSize, Math.min(wSize, hSize)); } else { setSize(wSize, wSize); } } else if (wMode == MeasureSpec.AT_MOST) { if (hMode == MeasureSpec.EXACTLY) { // f*ck exact height for vertical LinearLayout to work as desired // setSize(Math.min(wSize, hSize), hSize); setSize(Math.min(wSize, hSize), Math.min(wSize, hSize)); } else if (hMode == MeasureSpec.AT_MOST) { setSize(Math.min(wSize, hSize), Math.min(wSize, hSize)); } else { setSize(wSize, wSize); } } else { if (hMode == MeasureSpec.EXACTLY) { setSize(hSize, hSize); } else if (hMode == MeasureSpec.AT_MOST) { setSize(hSize, hSize); } else { setSize(PREF_SIZE, PREF_SIZE); } } }
@Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { int heightSize = View.MeasureSpec.getSize(heightMeasureSpec); int heightMode = View.MeasureSpec.getMode(heightMeasureSpec); int tmpHeightMeasureSpec = MeasureSpec.makeMeasureSpec(heightSize, heightMode); int widthSize = View.MeasureSpec.getSize(widthMeasureSpec); int widthMode = View.MeasureSpec.getMode(widthMeasureSpec); int tmpWidthMeasureSpec = MeasureSpec.makeMeasureSpec(widthSize, widthMode); Log.d(TAG, MeasureSpec.toString(heightMeasureSpec)); // fixed scrollview height problems if (heightMode == MeasureSpec.UNSPECIFIED && getParent() != null && (getParent() instanceof ScrollView)) { int baseHeight = 0; Context context = getContext(); if (context instanceof Activity) { Activity act = (Activity) context; int measuredHeight = act.findViewById(android.R.id.content).getMeasuredHeight(); baseHeight = measuredHeight; Log.d(TAG, "measuredHeight = " + measuredHeight); } else { baseHeight = getScreenHeight(); Log.d(TAG, "scHeight = " + baseHeight); } tmpHeightMeasureSpec = MeasureSpec.makeMeasureSpec(baseHeight, heightMode); } mPercentLayoutHelper.adjustChildren(tmpWidthMeasureSpec, tmpHeightMeasureSpec); super.onMeasure(widthMeasureSpec, heightMeasureSpec); if (mPercentLayoutHelper.handleMeasuredStateTooSmall()) { super.onMeasure(widthMeasureSpec, heightMeasureSpec); } }
/** {@inheritDoc} */ @Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { int level = -1; if (DEBUG_LAYOUT) { level = getCurrentLevel(); Xlog.d( DEBUG_LOG_TAG, "[FrameLayout][onMeasure] +" + level + ", " + "width = " + MeasureSpec.toString(widthMeasureSpec) + ", " + "height = " + MeasureSpec.toString(heightMeasureSpec) + ", this = " + this); } int count = getChildCount(); final boolean measureMatchParentChildren = MeasureSpec.getMode(widthMeasureSpec) != MeasureSpec.EXACTLY || MeasureSpec.getMode(heightMeasureSpec) != MeasureSpec.EXACTLY; mMatchParentChildren.clear(); int maxHeight = 0; int maxWidth = 0; int childState = 0; if (DEBUG_LAYOUT) { Log.d(DEBUG_LOG_TAG, "[FrameLayout] +" + level + ", pass.1, this = " + this); } for (int i = 0; i < count; i++) { final View child = getChildAt(i); if (mMeasureAllChildren || child.getVisibility() != GONE) { if (DEBUG_LAYOUT) { Xlog.d( DEBUG_LOG_TAG, "[FrameLayout] +" + level + ", child 1: " + i + ", " + "child = " + child + ", this = " + this); } measureChildWithMargins(child, widthMeasureSpec, 0, heightMeasureSpec, 0); final LayoutParams lp = (LayoutParams) child.getLayoutParams(); maxWidth = Math.max(maxWidth, child.getMeasuredWidth() + lp.leftMargin + lp.rightMargin); maxHeight = Math.max(maxHeight, child.getMeasuredHeight() + lp.topMargin + lp.bottomMargin); childState = combineMeasuredStates(childState, child.getMeasuredState()); if (measureMatchParentChildren) { if (lp.width == LayoutParams.MATCH_PARENT || lp.height == LayoutParams.MATCH_PARENT) { mMatchParentChildren.add(child); } } if (DEBUG_LAYOUT) { Log.d( DEBUG_LOG_TAG, "[FrameLayout] -" + level + ", child 1: " + i + ", " + "measure result = (" + child.getMeasuredWidth() + "," + child.getMeasuredHeight() + "), " + "child = " + child + "this = " + this); } } } if (DEBUG_LAYOUT) { Log.d( DEBUG_LOG_TAG, "[FrameLayout] -" + level + ", pass.1, maxWidth = " + maxWidth + ", maxHeight = " + maxHeight + ", this = " + this); } // Account for padding too maxWidth += getPaddingLeftWithForeground() + getPaddingRightWithForeground(); maxHeight += getPaddingTopWithForeground() + getPaddingBottomWithForeground(); // Check against our minimum height and width maxHeight = Math.max(maxHeight, getSuggestedMinimumHeight()); maxWidth = Math.max(maxWidth, getSuggestedMinimumWidth()); // Check against our foreground's minimum height and width final Drawable drawable = getForeground(); if (drawable != null) { maxHeight = Math.max(maxHeight, drawable.getMinimumHeight()); maxWidth = Math.max(maxWidth, drawable.getMinimumWidth()); } setMeasuredDimension( resolveSizeAndState(maxWidth, widthMeasureSpec, childState), resolveSizeAndState( maxHeight, heightMeasureSpec, childState << MEASURED_HEIGHT_STATE_SHIFT)); count = mMatchParentChildren.size(); if (count > 1) { if (DEBUG_LAYOUT) { Log.d(DEBUG_LOG_TAG, "[FrameLayout] +" + level + ", pass.2, this = " + this); } for (int i = 0; i < count; i++) { final View child = mMatchParentChildren.get(i); if (DEBUG_LAYOUT) { Xlog.d( DEBUG_LOG_TAG, "[FrameLayout] +" + level + ", child 2: " + i + ", " + "child = " + child + ", this = " + this); } final MarginLayoutParams lp = (MarginLayoutParams) child.getLayoutParams(); int childWidthMeasureSpec; int childHeightMeasureSpec; if (lp.width == LayoutParams.MATCH_PARENT) { childWidthMeasureSpec = MeasureSpec.makeMeasureSpec( getMeasuredWidth() - getPaddingLeftWithForeground() - getPaddingRightWithForeground() - lp.leftMargin - lp.rightMargin, MeasureSpec.EXACTLY); } else { childWidthMeasureSpec = getChildMeasureSpec( widthMeasureSpec, getPaddingLeftWithForeground() + getPaddingRightWithForeground() + lp.leftMargin + lp.rightMargin, lp.width); } if (lp.height == LayoutParams.MATCH_PARENT) { childHeightMeasureSpec = MeasureSpec.makeMeasureSpec( getMeasuredHeight() - getPaddingTopWithForeground() - getPaddingBottomWithForeground() - lp.topMargin - lp.bottomMargin, MeasureSpec.EXACTLY); } else { childHeightMeasureSpec = getChildMeasureSpec( heightMeasureSpec, getPaddingTopWithForeground() + getPaddingBottomWithForeground() + lp.topMargin + lp.bottomMargin, lp.height); } child.measure(childWidthMeasureSpec, childHeightMeasureSpec); if (DEBUG_LAYOUT) { Log.d( DEBUG_LOG_TAG, "[FrameLayout] -" + level + ", child 2: " + i + ", " + "measure result = (" + child.getMeasuredWidth() + "," + child.getMeasuredHeight() + "), " + "child = " + child + ", this = " + this); } } if (DEBUG_LAYOUT) { Log.d(DEBUG_LOG_TAG, "[FrameLayout] -" + level + ", pass.2, this = " + this); } } if (DEBUG_LAYOUT) { Xlog.d( DEBUG_LOG_TAG, "[FrameLayout][onMeasure] -" + level + ", " + "measure result = (" + getMeasuredWidth() + "," + getMeasuredHeight() + "), " + "min dimension = (" + getSuggestedMinimumWidth() + "," + getSuggestedMinimumHeight() + "), " + "this =" + this); } }