1. No matter how you set the size of custom components, the custom components always seem to be match_parent. We need to override the onMeasure method and correctly set the component size in it. [Java] @ Override protected void onMeasure (int widthMeasureSpec, int heightMeasureSpec) {// Note: to be as concise as possible, this article only discusses EXACTLY and AT_MOST modes int mode = MeasureSpec. getMode (widthMeasureSpec); if (mode = MeasureSpec. AT_MOST | mode = MeasureSpec. EXACTLY) {this. widthMeasureSpec = widthMeasureSpec; this. heightMeasureSpec = heightMeasureSpec; int width = MeasureSpec. getSize (widthMeasureSpec); int height = MeasureSpec. g EtSize (heightMeasureSpec); setMeasuredDimension (width, height);} else if (mode = MeasureSpec. UNSPECIFIED) {Log. d ("WOGU", "mode = UNSPECIFIED"); super. onMeasure (widthMeasureSpec, heightMeasureSpec);} 2. Set the size of the component in the Custom component to overwrite onMeasure and onLayout, and set the size of the child element in the component. [Java] public class MyViewGroup extends ViewGroup {protected int widthMeasureSpec, heightMeasureSpec; @ Override protected void onLayout (boolean changed, int left, int top, int right, int bottom) {if (changed) {View view = getChildAt (0); // measure triggers onMeasure of the View. // WidthMeasureSpec includes size and mode // int width = MeasureSpec. getSize (widthMeasureSpec); // int height = MeasureSpec. getSize (heightMeasureSpec); // int mode = MeasureSpec. getMode (widthMeasureSpec); // the possible value of mode: MeasureSpec. AT_MOST, MeasureSpec. EXACTLY, MeasureSpec. UNSPECIFIED // here, we simply record widthMeasureSpec and heightMeasureSpec in onMeasure // In fact, MeasureSpec provides a method to synthesize widthMeasureSpec and heightMeasureSpec according to the sizet and pattern. makeMeasureSpec (getWidth (), MeasureSpec. EXACTLY); // int heightMeasureSpec = MeasureSpec. makeMeasureSpec (getHeight (), MeasureSpec. EXACTLY); view. measure (widthMeasureSpec, heightMeasureSpec); view. layout (0, 0, getWidth (), getHeight () ;}@ Override protected void onMeasure (int widthMeasureSpec, int heightMeasureSpec) {this. widthMeasureSpec = widthMeasureSpec; this. heightMeasureSpec = heightMeasureSpec ;...}}