Measure icon. measureChildWithMargins(icon, widthMeasureSpec, 0, heightMeasureSpec, 0); !// Figure out how much width the icon used. MarginLayoutParams lp = (MarginLayoutParams) icon.getLayoutParams(); int widthUsed = icon.getMeasuredWidth() + lp.leftMargin + lp.rightMargin; int heightUsed = 0; !// Measure title measureChildWithMargins( titleView, !// Pass width constraints and width already used. widthMeasureSpec, widthUsed, !// Pass height constraints and height already used. heightMeasureSpec, heightUsed ); !// Measure the Subtitle. measureChildWithMargins( subtitleView, !// Pass width constraints and width already used. widthMeasureSpec, widthUsed, !// Pass height constraints and height already used. heightMeasureSpec, titleView.getMeasuredHeight()); !// Calculate this view's measured width and height. !// Figure out how much total space the icon used. int iconWidth = icon.getMeasuredWidth() + lp.leftMargin + lp.rightMargin; int iconHeight = icon.getMeasuredHeight() + lp.topMargin + lp.bottomMargin; lp = (MarginLayoutParams) titleView.getLayoutParams(); !// Figure out how much total space the title used. int titleWidth = titleView.getMeasuredWidth() + lp.leftMargin + lp.rightMargin; int titleHeight = titleView.getMeasuredHeight() + lp.topMargin + lp.bottomMargin; lp = (MarginLayoutParams) subtitleView.getLayoutParams(); !// Figure out how much total space the subtitle used. int subtitleWidth = subtitleView.getMeasuredWidth() + lp.leftMargin + lp.rightMargin; int subtitleHeight = subtitleView.getMeasuredHeight() + lp.topMargin + lp.bottomMargin; !// The width taken by the children + padding. int width = getPaddingTop() + getPaddingBottom() + iconWidth + Math.max(titleWidth, subtitleWidth); !// The height taken by the children + padding. int height = getPaddingTop() + getPaddingBottom() + Math.max(iconHeight, titleHeight + subtitleHeight); !// Reconcile the measured dimensions with the this view's constraints and !// set the final measured width and height. setMeasuredDimension( resolveSize(width, widthMeasureSpec), resolveSize(height, heightMeasureSpec) ); } @Override protected void onLayout(boolean changed, int left, int top, int right, int bottom) { MarginLayoutParams layoutParams = (MarginLayoutParams) icon.getLayoutParams(); !// Figure out the x-coordinate and y-coordinate of the icon. int x = getPaddingLeft() + layoutParams.leftMargin; int y = getPaddingTop() + layoutParams.topMargin; !// Layout the icon. icon.layout(x, y, x + icon.getMeasuredWidth(), y + icon.getMeasuredHeight()); !// Calculate the x-coordinate of the title: icon's right coordinate + !// the icon's right margin. x += icon.getMeasuredWidth() + layoutParams.rightMargin; !// Add in the title's left margin. layoutParams = (MarginLayoutParams) titleView.getLayoutParams(); x += layoutParams.leftMargin; !// Calculate the y-coordinate of the title: this ViewGroup's top padding + !// the title's top margin y = getPaddingTop() + layoutParams.topMargin; !// Layout the title. titleView.layout(x, y, x + titleView.getMeasuredWidth(), y + titleView.getMeasuredHeight()); !// The subtitle has the same x-coordinate as the title. So no more calculating there. !// Calculate the y-coordinate of the subtitle: the title's bottom coordinate + !// the title's bottom margin. y += titleView.getMeasuredHeight() + layoutParams.bottomMargin; layoutParams = (MarginLayoutParams) subtitleView.getLayoutParams(); !// Add in the subtitle's top margin. y += layoutParams.topMargin; !// Layout the subtitle. subtitleView.layout(x, y, x + subtitleView.getMeasuredWidth(), y + subtitleView.getMeasuredHeight()); }