Last active
May 9, 2016 08:42
-
-
Save SeniorZhai/661dfba5f933d25f5c8732ca810a5e0f to your computer and use it in GitHub Desktop.
Toolbar
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Override | |
protected void onLayout(boolean changed, int l, int t, int r, int b) { | |
final boolean isRtl = ViewCompat.getLayoutDirection(this) == ViewCompat.LAYOUT_DIRECTION_RTL; | |
final int width = getWidth(); | |
final int height = getHeight(); | |
final int paddingLeft = getPaddingLeft(); | |
final int paddingRight = getPaddingRight(); | |
final int paddingTop = getPaddingTop(); | |
final int paddingBottom = getPaddingBottom(); | |
int left = paddingLeft; | |
int right = width - paddingRight; | |
final int[] collapsingMargins = mTempMargins; | |
collapsingMargins[0] = collapsingMargins[1] = 0; | |
// Align views within the minimum toolbar height, if set. | |
final int alignmentHeight = ViewCompat.getMinimumHeight(this); | |
if (shouldLayout(mNavButtonView)) { | |
if (isRtl) { | |
right = layoutChildRight(mNavButtonView, right, collapsingMargins, | |
alignmentHeight); | |
} else { | |
left = layoutChildLeft(mNavButtonView, left, collapsingMargins, | |
alignmentHeight); | |
} | |
} | |
if (shouldLayout(mCollapseButtonView)) { | |
if (isRtl) { | |
right = layoutChildRight(mCollapseButtonView, right, collapsingMargins, | |
alignmentHeight); | |
} else { | |
left = layoutChildLeft(mCollapseButtonView, left, collapsingMargins, | |
alignmentHeight); | |
} | |
} | |
if (shouldLayout(mMenuView)) { | |
if (isRtl) { | |
left = layoutChildLeft(mMenuView, left, collapsingMargins, | |
alignmentHeight); | |
} else { | |
right = layoutChildRight(mMenuView, right, collapsingMargins, | |
alignmentHeight); | |
} | |
} | |
collapsingMargins[0] = Math.max(0, getContentInsetLeft() - left); | |
collapsingMargins[1] = Math.max(0, getContentInsetRight() - (width - paddingRight - right)); | |
left = Math.max(left, getContentInsetLeft()); | |
right = Math.min(right, width - paddingRight - getContentInsetRight()); | |
if (shouldLayout(mExpandedActionView)) { | |
if (isRtl) { | |
right = layoutChildRight(mExpandedActionView, right, collapsingMargins, | |
alignmentHeight); | |
} else { | |
left = layoutChildLeft(mExpandedActionView, left, collapsingMargins, | |
alignmentHeight); | |
} | |
} | |
if (shouldLayout(mLogoView)) { | |
if (isRtl) { | |
right = layoutChildRight(mLogoView, right, collapsingMargins, | |
alignmentHeight); | |
} else { | |
left = layoutChildLeft(mLogoView, left, collapsingMargins, | |
alignmentHeight); | |
} | |
} | |
final boolean layoutTitle = shouldLayout(mTitleTextView); | |
final boolean layoutSubtitle = shouldLayout(mSubtitleTextView); | |
int titleHeight = 0; | |
if (layoutTitle) { | |
final LayoutParams lp = (LayoutParams) mTitleTextView.getLayoutParams(); | |
titleHeight += lp.topMargin + mTitleTextView.getMeasuredHeight() + lp.bottomMargin; | |
} | |
if (layoutSubtitle) { | |
final LayoutParams lp = (LayoutParams) mSubtitleTextView.getLayoutParams(); | |
titleHeight += lp.topMargin + mSubtitleTextView.getMeasuredHeight() + lp.bottomMargin; | |
} | |
if (layoutTitle || layoutSubtitle) { | |
int titleTop; | |
final View topChild = layoutTitle ? mTitleTextView : mSubtitleTextView; | |
final View bottomChild = layoutSubtitle ? mSubtitleTextView : mTitleTextView; | |
final LayoutParams toplp = (LayoutParams) topChild.getLayoutParams(); | |
final LayoutParams bottomlp = (LayoutParams) bottomChild.getLayoutParams(); | |
final boolean titleHasWidth = layoutTitle && mTitleTextView.getMeasuredWidth() > 0 | |
|| layoutSubtitle && mSubtitleTextView.getMeasuredWidth() > 0; | |
switch (mGravity & Gravity.VERTICAL_GRAVITY_MASK) { | |
case Gravity.TOP: | |
titleTop = getPaddingTop() + toplp.topMargin + mTitleMarginTop; | |
break; | |
default: | |
case Gravity.CENTER_VERTICAL: | |
final int space = height - paddingTop - paddingBottom; | |
int spaceAbove = (space - titleHeight) / 2; | |
if (spaceAbove < toplp.topMargin + mTitleMarginTop) { | |
spaceAbove = toplp.topMargin + mTitleMarginTop; | |
} else { | |
final int spaceBelow = height - paddingBottom - titleHeight - | |
spaceAbove - paddingTop; | |
if (spaceBelow < toplp.bottomMargin + mTitleMarginBottom) { | |
spaceAbove = Math.max(0, spaceAbove - | |
(bottomlp.bottomMargin + mTitleMarginBottom - spaceBelow)); | |
} | |
} | |
titleTop = paddingTop + spaceAbove; | |
break; | |
case Gravity.BOTTOM: | |
titleTop = height - paddingBottom - bottomlp.bottomMargin - mTitleMarginBottom - | |
titleHeight; | |
break; | |
} | |
if (isRtl) { | |
final int rd = (titleHasWidth ? mTitleMarginStart : 0) - collapsingMargins[1]; | |
right -= Math.max(0, rd); | |
collapsingMargins[1] = Math.max(0, -rd); | |
int titleRight = right; | |
int subtitleRight = right; | |
if (layoutTitle) { | |
final LayoutParams lp = (LayoutParams) mTitleTextView.getLayoutParams(); | |
final int titleLeft = titleRight - mTitleTextView.getMeasuredWidth(); | |
final int titleBottom = titleTop + mTitleTextView.getMeasuredHeight(); | |
mTitleTextView.layout(titleLeft, titleTop, titleRight, titleBottom); | |
titleRight = titleLeft - mTitleMarginEnd; | |
titleTop = titleBottom + lp.bottomMargin; | |
} | |
if (layoutSubtitle) { | |
final LayoutParams lp = (LayoutParams) mSubtitleTextView.getLayoutParams(); | |
titleTop += lp.topMargin; | |
final int subtitleLeft = subtitleRight - mSubtitleTextView.getMeasuredWidth(); | |
final int subtitleBottom = titleTop + mSubtitleTextView.getMeasuredHeight(); | |
mSubtitleTextView.layout(subtitleLeft, titleTop, subtitleRight, subtitleBottom); | |
subtitleRight = subtitleRight - mTitleMarginEnd; | |
titleTop = subtitleBottom + lp.bottomMargin; | |
} | |
if (titleHasWidth) { | |
right = Math.min(titleRight, subtitleRight); | |
} | |
} else { | |
final int ld = (titleHasWidth ? mTitleMarginStart : 0) - collapsingMargins[0]; | |
left += Math.max(0, ld); | |
collapsingMargins[0] = Math.max(0, -ld); | |
int titleLeft = left; | |
int subtitleLeft = left; | |
if (layoutTitle) { | |
final LayoutParams lp = (LayoutParams) mTitleTextView.getLayoutParams(); | |
final int titleRight = titleLeft + mTitleTextView.getMeasuredWidth(); | |
final int titleBottom = titleTop + mTitleTextView.getMeasuredHeight(); | |
mTitleTextView.layout(titleLeft, titleTop, titleRight, titleBottom); | |
titleLeft = titleRight + mTitleMarginEnd; | |
titleTop = titleBottom + lp.bottomMargin; | |
} | |
if (layoutSubtitle) { | |
final LayoutParams lp = (LayoutParams) mSubtitleTextView.getLayoutParams(); | |
titleTop += lp.topMargin; | |
final int subtitleRight = subtitleLeft + mSubtitleTextView.getMeasuredWidth(); | |
final int subtitleBottom = titleTop + mSubtitleTextView.getMeasuredHeight(); | |
mSubtitleTextView.layout(subtitleLeft, titleTop, subtitleRight, subtitleBottom); | |
subtitleLeft = subtitleRight + mTitleMarginEnd; | |
titleTop = subtitleBottom + lp.bottomMargin; | |
} | |
if (titleHasWidth) { | |
left = Math.max(titleLeft, subtitleLeft); | |
} | |
} | |
} | |
// Get all remaining children sorted for layout. This is all prepared | |
// such that absolute layout direction can be used below. | |
addCustomViewsWithGravity(mTempViews, Gravity.LEFT); | |
final int leftViewsCount = mTempViews.size(); | |
for (int i = 0; i < leftViewsCount; i++) { | |
left = layoutChildLeft(mTempViews.get(i), left, collapsingMargins, | |
alignmentHeight); | |
} | |
addCustomViewsWithGravity(mTempViews, Gravity.RIGHT); | |
final int rightViewsCount = mTempViews.size(); | |
for (int i = 0; i < rightViewsCount; i++) { | |
right = layoutChildRight(mTempViews.get(i), right, collapsingMargins, | |
alignmentHeight); | |
} | |
// Centered views try to center with respect to the whole bar, but views pinned | |
// to the left or right can push the mass of centered views to one side or the other. | |
addCustomViewsWithGravity(mTempViews, Gravity.CENTER_HORIZONTAL); | |
final int centerViewsWidth = getViewListMeasuredWidth(mTempViews, collapsingMargins); | |
final int parentCenter = paddingLeft + (width - paddingLeft - paddingRight) / 2; | |
final int halfCenterViewsWidth = centerViewsWidth / 2; | |
int centerLeft = parentCenter - halfCenterViewsWidth; | |
final int centerRight = centerLeft + centerViewsWidth; | |
if (centerLeft < left) { | |
centerLeft = left; | |
} else if (centerRight > right) { | |
centerLeft -= centerRight - right; | |
} | |
final int centerViewsCount = mTempViews.size(); | |
for (int i = 0; i < centerViewsCount; i++) { | |
centerLeft = layoutChildLeft(mTempViews.get(i), centerLeft, collapsingMargins, | |
alignmentHeight); | |
} | |
mTempViews.clear(); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Override | |
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { | |
int width = 0; | |
int height = 0; | |
int childState = 0; | |
final int[] collapsingMargins = mTempMargins; | |
final int marginStartIndex; | |
final int marginEndIndex; | |
if (ViewUtils.isLayoutRtl(this)) { | |
marginStartIndex = 1; | |
marginEndIndex = 0; | |
} else { | |
marginStartIndex = 0; | |
marginEndIndex = 1; | |
} | |
int navWidth = 0; | |
if (shouldLayout(mNavButtonView)) { | |
measureChildConstrained(mNavButtonView, widthMeasureSpec, width, heightMeasureSpec, 0, | |
mMaxButtonHeight); | |
navWidth = mNavButtonView.getMeasuredWidth() + getHorizontalMargins(mNavButtonView); | |
height = Math.max(height, mNavButtonView.getMeasuredHeight() + | |
getVerticalMargins(mNavButtonView)); | |
childState = ViewUtils.combineMeasuredStates(childState, | |
ViewCompat.getMeasuredState(mNavButtonView)); | |
} | |
if (shouldLayout(mCollapseButtonView)) { | |
measureChildConstrained(mCollapseButtonView, widthMeasureSpec, width, | |
heightMeasureSpec, 0, mMaxButtonHeight); | |
navWidth = mCollapseButtonView.getMeasuredWidth() + | |
getHorizontalMargins(mCollapseButtonView); | |
height = Math.max(height, mCollapseButtonView.getMeasuredHeight() + | |
getVerticalMargins(mCollapseButtonView)); | |
childState = ViewUtils.combineMeasuredStates(childState, | |
ViewCompat.getMeasuredState(mCollapseButtonView)); | |
} | |
final int contentInsetStart = getContentInsetStart(); | |
width += Math.max(contentInsetStart, navWidth); | |
collapsingMargins[marginStartIndex] = Math.max(0, contentInsetStart - navWidth); | |
int menuWidth = 0; | |
if (shouldLayout(mMenuView)) { | |
measureChildConstrained(mMenuView, widthMeasureSpec, width, heightMeasureSpec, 0, | |
mMaxButtonHeight); | |
menuWidth = mMenuView.getMeasuredWidth() + getHorizontalMargins(mMenuView); | |
height = Math.max(height, mMenuView.getMeasuredHeight() + | |
getVerticalMargins(mMenuView)); | |
childState = ViewUtils.combineMeasuredStates(childState, | |
ViewCompat.getMeasuredState(mMenuView)); | |
} | |
final int contentInsetEnd = getContentInsetEnd(); | |
width += Math.max(contentInsetEnd, menuWidth); | |
collapsingMargins[marginEndIndex] = Math.max(0, contentInsetEnd - menuWidth); | |
if (shouldLayout(mExpandedActionView)) { | |
width += measureChildCollapseMargins(mExpandedActionView, widthMeasureSpec, width, | |
heightMeasureSpec, 0, collapsingMargins); | |
height = Math.max(height, mExpandedActionView.getMeasuredHeight() + | |
getVerticalMargins(mExpandedActionView)); | |
childState = ViewUtils.combineMeasuredStates(childState, | |
ViewCompat.getMeasuredState(mExpandedActionView)); | |
} | |
if (shouldLayout(mLogoView)) { | |
width += measureChildCollapseMargins(mLogoView, widthMeasureSpec, width, | |
heightMeasureSpec, 0, collapsingMargins); | |
height = Math.max(height, mLogoView.getMeasuredHeight() + | |
getVerticalMargins(mLogoView)); | |
childState = ViewUtils.combineMeasuredStates(childState, | |
ViewCompat.getMeasuredState(mLogoView)); | |
} | |
final int childCount = getChildCount(); | |
for (int i = 0; i < childCount; i++) { | |
final View child = getChildAt(i); | |
final LayoutParams lp = (LayoutParams) child.getLayoutParams(); | |
if (lp.mViewType != LayoutParams.CUSTOM || !shouldLayout(child)) { | |
// We already got all system views above. Skip them and GONE views. | |
continue; | |
} | |
width += measureChildCollapseMargins(child, widthMeasureSpec, width, | |
heightMeasureSpec, 0, collapsingMargins); | |
height = Math.max(height, child.getMeasuredHeight() + getVerticalMargins(child)); | |
childState = ViewUtils.combineMeasuredStates(childState, | |
ViewCompat.getMeasuredState(child)); | |
} | |
int titleWidth = 0; | |
int titleHeight = 0; | |
final int titleVertMargins = mTitleMarginTop + mTitleMarginBottom; | |
final int titleHorizMargins = mTitleMarginStart + mTitleMarginEnd; | |
if (shouldLayout(mTitleTextView)) { | |
titleWidth = measureChildCollapseMargins(mTitleTextView, widthMeasureSpec, | |
width + titleHorizMargins, heightMeasureSpec, titleVertMargins, | |
collapsingMargins); | |
titleWidth = mTitleTextView.getMeasuredWidth() + getHorizontalMargins(mTitleTextView); | |
titleHeight = mTitleTextView.getMeasuredHeight() + getVerticalMargins(mTitleTextView); | |
childState = ViewUtils.combineMeasuredStates(childState, | |
ViewCompat.getMeasuredState(mTitleTextView)); | |
} | |
if (shouldLayout(mSubtitleTextView)) { | |
titleWidth = Math.max(titleWidth, measureChildCollapseMargins(mSubtitleTextView, | |
widthMeasureSpec, width + titleHorizMargins, | |
heightMeasureSpec, titleHeight + titleVertMargins, | |
collapsingMargins)); | |
titleHeight += mSubtitleTextView.getMeasuredHeight() + | |
getVerticalMargins(mSubtitleTextView); | |
childState = ViewUtils.combineMeasuredStates(childState, | |
ViewCompat.getMeasuredState(mSubtitleTextView)); | |
} | |
width += titleWidth; | |
height = Math.max(height, titleHeight); | |
width += getPaddingLeft() + getPaddingRight(); | |
height += getPaddingTop() + getPaddingBottom(); | |
final int measuredWidth = ViewCompat.resolveSizeAndState( | |
Math.max(width, getSuggestedMinimumWidth()), | |
widthMeasureSpec, childState & ViewCompat.MEASURED_STATE_MASK); | |
final int measuredHeight = ViewCompat.resolveSizeAndState( | |
Math.max(height, getSuggestedMinimumHeight()), | |
heightMeasureSpec, childState << ViewCompat.MEASURED_HEIGHT_STATE_SHIFT); | |
setMeasuredDimension(measuredWidth, shouldCollapse() ? 0 : measuredHeight); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment