为了清楚描述问题,我们拿 button 来举例
如果 Button 宽高设置 wrap_content 并设置了 background 为一个图片的时候,会出现宽高默认为图片的大小 wrap_content 无效的情况,针对这个问题我们查看 Button 的 测量过程,因为 Button 继承自 TextView ,所以我们看 TextView 的 onMeasure 方法,只贴出关键代码:
// Check against our minimum width
width = Math.max(width, getSuggestedMinimumWidth());
if (widthMode == MeasureSpec.AT_MOST) {
width = Math.min(widthSize, width);
}
如果是 widthMode 不是 MeasureSpec.EXACTLY 情况下 ,width 是测量自身后的值,然后和 getSuggestedMinimumWidth() 取最大值
getSuggestedMinimumWidth() 是 View 的方法:
/**
* Returns the suggested minimum width that the view should use. This
* returns the maximum of the view's minimum width
* and the background's minimum width
* ({@link android.graphics.drawable.Drawable#getMinimumWidth()}).
* <p>
* When being used in {@link #onMeasure(int, int)}, the caller should still
* ensure the returned width is within the requirements of the parent.
*
* @return The suggested minimum width of the view.
*/
protected int getSuggestedMinimumWidth() {
return (mBackground == null) ? mMinWidth : max(mMinWidth, mBackground.getMinimumWidth());
}
也就是说没有设置背景的情况下,返回自身宽度最小值;有背景的情况下,返回背景的最小宽度和自身最小宽度的最大值,背景的最小宽度的概念是 getIntrinsicWidth()
,也就是说图片的话是图片自身的宽度,如果背景是 ColorDrawable 之类的没有原始宽度的时候返回 -1。
然后到 Button 的默认 style “Base.Widget.AppCompat.Button” 看一下
<style name="Base.Widget.AppCompat.Button" parent="android:Widget">
<item name="android:background">@drawable/abc_btn_default_mtrl_shape</item>
<item name="android:textAppearance">?android:attr/textAppearanceButton</item>
<item name="android:minHeight">48dip</item>
<item name="android:minWidth">88dip</item>
<item name="android:focusable">true</item>
<item name="android:clickable">true</item>
<item name="android:gravity">center_vertical|center_horizontal</item>
</style>
可见默认值 android:minHeight = 48dip , android:minWidth = 88dip。
最后回到上面的问题,可以得出 Button 的高度取决于自身宽高的最小值和背景的宽高。
如果背景有原始高度的话,比如图片 BitmapDrawable等,最终的测量值取已测量的值、背景、默认最小宽高的三者最大值。
如果背景没有原始高度的话,比如 ColorDrawable 等,最终的宽高为已测量的值、背景二者最大值。
其他继承 View 的控件跟 button 大同小异。