</pre>首先在理解我为什么这么写之前,请阅读一篇前人的文章:<a target=_blank target="_blank" href="/go.html?url=https://blog.csdn.net/a396901990/article/details/36475213">https://blog.csdn.net/a396901990/article/details/36475213</a><p></p><p>这篇文章详细的分析了一下onMeasure函数的源码和用法。通过阅读这篇文章我才得到了启示。再次感谢一下这个博主。</p><p></p><p>进入正题~</p><p></p><p>随着开发的深入越来越多的控件需要自己去写去画,也就是我们常说的自定义控件。但是相信很多人都碰到过自定义控件在布局上或者是显示大小上经常碰到一些奇奇怪怪的问题。这篇文章所描述和解决的问题只是其中之一。</p><p><span style="font-size:24px">问题:自定义的控件,如果在layout里面写好了固定宽高的话,我在代码里想要通过setMinimumHeight或者setMinimumWidth方法进行修改时无效,没有任何变化。</span></p><p><span style="font-size:24px">解决办法:</span></p><p></p><pre code_snippet_id="1639245" snippet_file_name="blog_20160408_2_3718092" name="code" class="java">@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
int height = MeasureSpec.getSize(heightMeasureSpec);
heightMeasureSpec = MeasureSpec.makeMeasureSpec(height,MeasureSpec.UNSPECIFIED);
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
}</pre>首先在理解我为什么这么写之前,请阅读一篇前人的文章:<a target