@Override public void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { if (mOnLayout == null) { mOnLayout = makeLayout(mTextOn); } if (mOffLayout == null) { mOffLayout = makeLayout(mTextOff); } mTrackDrawable.getPadding(mTempRect); final int maxTextWidth = Math.max(mOnLayout.getWidth(), mOffLayout.getWidth()); final int switchWidth = Math.max( mSwitchMinWidth, maxTextWidth * 2 + mThumbTextPadding * 4 + mTempRect.left + mTempRect.right); final int switchHeight = mTrackDrawable.getIntrinsicHeight(); mThumbWidth = maxTextWidth + mThumbTextPadding * 2; mSwitchWidth = switchWidth; mSwitchHeight = switchHeight; super.onMeasure(widthMeasureSpec, heightMeasureSpec); final int measuredHeight = getMeasuredHeight(); if (measuredHeight < switchHeight) { setMeasuredDimension(getMeasuredWidth(), switchHeight); } }
@Override public void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { final Rect padding = mTempRect; int thumbWidth; int thumbHeight; if (mThumbDrawable != null) { mThumbDrawable.getPadding(padding); thumbWidth = mThumbDrawable.getIntrinsicWidth() - padding.left - padding.right; thumbHeight = mThumbDrawable.getIntrinsicHeight(); } else { thumbWidth = 0; thumbHeight = 0; } mThumbWidth = thumbWidth; int trackHeight; if (mTrackDrawable != null) { mTrackDrawable.getPadding(padding); trackHeight = mTrackDrawable.getIntrinsicHeight(); } else { padding.setEmpty(); trackHeight = 0; } int paddingLeft = padding.left; int paddingRight = padding.right; if (mThumbDrawable != null) { final Insets inset = Insets.NONE; paddingLeft = Math.max(paddingLeft, inset.left); paddingRight = Math.max(paddingRight, inset.right); } final int switchWidth = Math.max(mSwitchMinWidth, 2 * mThumbWidth + paddingLeft + paddingRight); final int switchHeight = Math.max(trackHeight, thumbHeight); mSwitchWidth = switchWidth; mSwitchHeight = switchHeight; super.onMeasure(widthMeasureSpec, heightMeasureSpec); final int measuredHeight = getMeasuredHeight(); if (measuredHeight < switchHeight) { setMeasuredDimension(switchWidth, switchHeight); } }