Solution wrap_content Invalid in custom View
Children's shoes with custom view will find that a custom control that inherits the view directly needs to override the Onmeasure () method and set its own size when wrap_content, and if the custom control is set to a size in the layout file, Wrap_content is equivalent to Match_parent.
In fact, the Onmeasure () method has been rewritten in the control of Android, and the wrap_content and so on have been specially handled, and the default width and height are given when wrap_content. So for the treatment of this problem we also have a certain idea, in Onmeasure () for the wrap_content situation to give the appropriate width, height can, code as follows:
@Overrideprotected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { super.onMeasure(widthMeasureSpec, heightMeasureSpec); int widthSpecMode = MeasureSpec.getMode(widthMeasureSpec); int widthSpecSize = MeasureSpec.getSize(widthMeasureSpec); int heightSpecMode = MeasureSpec.getMode(heightMeasureSpec); int heithtSpecSize = MeasureSpec.getSize(heightMeasureSpec); if (widthSpecMode == MeasureSpec.AT_MOST && heightSpecMode == MeasureSpec.AT_MOST) { setMeasuredDimension(200, 200); } else if (widthSpecMode == MeasureSpec.AT_MOST) { setMeasuredDimension(200, heithtSpecSize); } else if (heightSpecMode == MeasureSpec.AT_MOST) { setMeasuredDimension(widthSpecSize, 200); }}
Android Check Gaps (view)-Wrap_content Invalid solution in Custom view