天天看點

安卓 View 設定寬高 wrap_content 屬性無效的原因

為了清楚描述問題,我們拿 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">dip</item>
        <item name="android:minWidth">dip</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 大同小異。