自定義View時wrap_content不起作用?

view顯示到屏幕上,大概來講,需要走如下的流程:從調用方法的角度來看,即需要走三個方法:measure(),layout(),draw(),這三個方法是view的方法,走完這三個方法view就會顯示到屏幕上

而且這三個方法是final修飾的,系統不允許我們重寫這些方法,要求我們遵循這個框架流程,但是又不能不讓我們自定義自己的測量,佈局和繪畫過程,所以給出了onMeasure(),onLayout(),onDraw()可重寫的方法,它們分別對應於measure(),layout(),draw()方法,分別在measure(),layout(),draw()內部調用,(這裏用到了模板方法模式)我們可以通過重寫on開頭的這些方法達到自定義的效果。

下面,看一下view的onMeasure()方法的默認實現,讀代碼發現默認實現wrap_content和match_parent獲取的測量值是一樣的,即都是通過int specSize = MeasureSpec.getSize(measureSpec);獲取的,那麼這裏的measureSpec(即onMeasure(int widthMeasureSpec, int heightMeasureSpec)方法傳過來的的兩個參數,widthMeasureSpec 和 heightMeasureSpec從哪裏來的呢?發現,是通過如下流程傳過來的,從父容器開始,在父容器中的measure()方法中調用了measureChildren()方法,在measureChildren()方法中遍歷了所有的子view,調用了measureChild()方法,在measureChild()方法中getChildMeasureSpec()方法獲取了子View的大小和測量模式,然後再通過調用子view的measure()傳給了子view的onMeasure()方法。簡述如下

父measure() --> 父measureChildren()--> 父measureChild() -- >  父 getChildMeasureSpec()-- >   子measure() --> 子onMeasure()

上面的方法的調用在具體的控件中可能並不是準確的(具體到某個方法),但是整個流程是正確的,當把某個自定義view放到某個系統已經實現好的現有的父容器中時(比如Linear Layout,FrameLayout),由於父容器已經重寫了ViewGroup的onMeasure()方法,加上了測量子view的邏輯(ViewGroup默認是沒有對子View的操作的,只是提供了一些操作的方法,如measureChildren(),measureChild(),measureChildWithMargins()等),所以會調用getChildMeasureSpec()方法獲取子view的大小和測量模式。

protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        //獲取測量後的大小(getDefaultSize)
        //並進行設置(setMeasuredDimension)
        setMeasuredDimension(getDefaultSize(getSuggestedMinimumWidth(), widthMeasureSpec),
                getDefaultSize(getSuggestedMinimumHeight(), heightMeasureSpec));
    }


public static int getDefaultSize(int size, int measureSpec) {
        int result = size;
        int specMode = MeasureSpec.getMode(measureSpec);
        int specSize = MeasureSpec.getSize(measureSpec);

        switch (specMode) {
        case MeasureSpec.UNSPECIFIED:
            result = size;
            break;
        case MeasureSpec.AT_MOST://對應wrap_content
        case MeasureSpec.EXACTLY://對應match_parent和確定的高寬值
            result = specSize;
            break;
        }
        return result;
    }
另:protected int getSuggestedMinimumWidth() {
        return (mBackground == null) ? mMinWidth : max(mMinWidth, mBackground.getMinimumWidth());
    }
protected void measureChild(View child, int parentWidthMeasureSpec,
            int parentHeightMeasureSpec) {
        final LayoutParams lp = child.getLayoutParams();
        //getChildMeasureSpec獲取子View的測量規格
        final int childWidthMeasureSpec = getChildMeasureSpec(parentWidthMeasureSpec,
                mPaddingLeft + mPaddingRight, lp.width);
        final int childHeightMeasureSpec = getChildMeasureSpec(parentHeightMeasureSpec,
                mPaddingTop + mPaddingBottom, lp.height);

        child.measure(childWidthMeasureSpec, childHeightMeasureSpec);
    }

//注:在LinearLayout中測量子view的方法調用的應該是下面這個方法(待驗證)
protected void measureChildWithMargins(View child,
            int parentWidthMeasureSpec, int widthUsed,
            int parentHeightMeasureSpec, int heightUsed) {
        final MarginLayoutParams lp = (MarginLayoutParams) child.getLayoutParams();

        final int childWidthMeasureSpec = getChildMeasureSpec(parentWidthMeasureSpec,
                mPaddingLeft + mPaddingRight + lp.leftMargin + lp.rightMargin
                        + widthUsed, lp.width);
        final int childHeightMeasureSpec = getChildMeasureSpec(parentHeightMeasureSpec,
                mPaddingTop + mPaddingBottom + lp.topMargin + lp.bottomMargin
                        + heightUsed, lp.height);

        child.measure(childWidthMeasureSpec, childHeightMeasureSpec);
    }

經過上面的分析結合 measureChild()方法發現getDefaultSize()方法參數中的 measureSpec而是通過getChildMeasureSpec()方法獲取的,下面看一下getChildMeasureSpec()方法,可以看出,父容器是EXACTLY和AT_MOST時(UNSPECIFIED一般是系統使用,暫不考慮,無論子view是match_parent還是wrap_content,其獲得的size(即方法中你的resultSize)都是父容器的size減去父容器自身的padding : int size = Math.max(0, specSize - padding);

public static int getChildMeasureSpec(int spec, int padding, int childDimension) {
        //父容器的測量模式
        int specMode = MeasureSpec.getMode(spec);
        //父容器的尺寸
        int specSize = MeasureSpec.getSize(spec);

        int size = Math.max(0, specSize - padding);

        int resultSize = 0;
        int resultMode = 0;
        //對不同的父容器測量模式分別進行處理
        switch (specMode) {
        // Parent has imposed an exact size on us

        //父容器是EXACTLY模式
        case MeasureSpec.EXACTLY:

            //子View 尺寸 >= 0,即指定了確切的寬高值。
            if (childDimension >= 0) {
                resultSize = childDimension;
                resultMode = MeasureSpec.EXACTLY;

            //子View是MATCH_PARENT
            } else if (childDimension == LayoutParams.MATCH_PARENT) {
                // Child wants to be our size. So be it.
                resultSize = size;
                resultMode = MeasureSpec.EXACTLY;

            //子View是WRAP_CONTENT
            } else if (childDimension == LayoutParams.WRAP_CONTENT) {
                // Child wants to determine its own size. It can't be
                // bigger than us.
                resultSize = size;
                resultMode = MeasureSpec.AT_MOST;
            }
            break;

        // Parent has imposed a maximum size on us

        //父容器是AT_MOST模式
        case MeasureSpec.AT_MOST:

            //子View 尺寸 >= 0,即指定了確切的寬高值。
            if (childDimension >= 0) {
                // Child wants a specific size... so be it
                resultSize = childDimension;
                resultMode = MeasureSpec.EXACTLY;

            //子View是MATCH_PARENT
            } else if (childDimension == LayoutParams.MATCH_PARENT) {
                // Child wants to be our size, but our size is not fixed.
                // Constrain child to not be bigger than us.
                resultSize = size;
                resultMode = MeasureSpec.AT_MOST;

            //子View是WRAP_CONTENT
            } else if (childDimension == LayoutParams.WRAP_CONTENT) {
                // Child wants to determine its own size. It can't be
                // bigger than us.
                resultSize = size;
                resultMode = MeasureSpec.AT_MOST;
            }
            break;

        // Parent asked to see how big we want to be

        //父容器是UNSPECIFIED模式
        case MeasureSpec.UNSPECIFIED:

            //子View 尺寸 >= 0,即指定了確切的寬高值。
            if (childDimension >= 0) {
                // Child wants a specific size... let him have it
                resultSize = childDimension;
                resultMode = MeasureSpec.EXACTLY;

            //子View是MATCH_PARENT
            } else if (childDimension == LayoutParams.MATCH_PARENT) {
                // Child wants to be our size... find out how big it should
                // be
                resultSize = View.sUseZeroUnspecifiedMeasureSpec ? 0 : size;
                resultMode = MeasureSpec.UNSPECIFIED;

            //子View是WRAP_CONTENT
            } else if (childDimension == LayoutParams.WRAP_CONTENT) {
                // Child wants to determine its own size.... find out how
                // big it should be
                resultSize = View.sUseZeroUnspecifiedMeasureSpec ? 0 : size;
                resultMode = MeasureSpec.UNSPECIFIED;
            }
            break;
        }
        //noinspection ResourceType
        return MeasureSpec.makeMeasureSpec(resultSize, resultMode);
    }

解決辦法:如下,即設置一個默認的寬高

 @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);

        // 獲取寬的大小
        int widthSize = MeasureSpec.getSize(widthMeasureSpec);

        // 獲取高的大小
        int heightSize = MeasureSpec.getSize(heightMeasureSpec);

        //默認寬高大小
        int defaultWidth = 300;
        int defaultHeight = 300;

        if (getLayoutParams().width == ViewGroup.LayoutParams.WRAP_CONTENT) {
            widthSize = defaultWidth;
        }

        if (getLayoutParams().height == ViewGroup.LayoutParams.WRAP_CONTENT) {
            heightSize = defaultHeight;
        }
        //重新設置大小
        setMeasuredDimension(widthSize, heightSize);
    }

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章