Android開發之View重寫相關API-onLayout,onMeasure,MeasureSpec

來源:互聯網
上載者:User

標籤:android   des   style   color   使用   strong   

 1.onLayout

android.view.ViewGroup

protected void onLayout(boolean changed, int l, int t, int r, int b)

執行layout操作時調用onLayout方法。View要給它的每個Child設定size和position。擁有Children的子類需要重寫onLayout方法並且調用每個Child的layout方法。

參數changed表示view的size或position發生變化。參數l, t, r, b分別表示相對於parent的left, top, right, bottom position。

 2.onMeasure

protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec)

測量View及其Content,確定measuredWidth和measuredHeight。在方法measure(int, int)中調用。重寫onMeasure方法時,需要調用方法setMeasuredDimension(int, int),儲存View的measuredWidth和measuredHeight。若儲存失敗,方法measure(int, int)會拋出異常IllegalStateException。可以調用super.onMeasure(int, int)方法。

除非MeasureSpec准許更大的size,否則measure的預設實現是background size。子類重寫onMeasure(int, int)提供Content的更佳測量。如果onMeasure被重寫,子類必須保證measuredWidth和measuredHeight至少是view的minHeight和minWidth。minHeight/Width通過getSuggestedMinimumHight/Width()擷取。

參數width/heightMeasureSpec表示parent強加的horizontal/vertical space要求。

void android.view.ViewGroup.layout(int l, int t, int r, int b)

給view及其descendants設定size和position。它正是layout機制的第2個步,每個parent調用它的chlidren的layout操作。使用在measure階段測量得到的size和position資料完成layout操作。擁有child的子類必須重寫onLayout方法,調用每個child的layout操作。

 

void android.view.ViewGroup.measureChildren(int widthMeasureSpec, int heightMeasureSpec)

請View的所有children測量themseles, 測量依據是View的MeasureSpec和padding。忽略處於GONE狀態的children,是否GONE狀態由getChildMeasureSpec來確定。

參數width/heightMeasureSpec表示view的width/height要求。

3.MeasureSpec

android.view.View.MeasureSpec

MeasureSpec是View的內部類

public static class MeasureSpec

MeasureSpec封裝從parent傳遞給child的layout要求。每個MeasureSpec表示對width/height的要求。MeasureSpec由size和mode組成。可用的mode有3種:

1. UNSPECIFIED表示parent沒有強加給child任何constraint。

2. EXACTLY表示parent已經確定child的精確size。

3. AT_MOST表示child可以設定為specified size之內的任何值。

MeasureSpec實現為int類型,相比object類型,降低了allocation。可以將<size, mode>元組pack和unpack為int類型。

 

MeasureSpec定義的常量有:

private static final int MODE_SHIFT = 30;

private static final int MODE_MASK  = 0x3 << MODE_SHIFT;

public static final int UNSPECIFIED = 0 << MODE_SHIFT;

public static final int EXACTLY     = 1 << MODE_SHIFT;

public static final int AT_MOST     = 2 << MODE_SHIFT;

 

MeasureSpec定義的方法有:

public static String toString(int measureSpec) { 
    int mode = getMode(measureSpec); 
    int size = getSize(measureSpec);

    StringBuilder sb = new StringBuilder("MeasureSpec: ");

    if (mode == UNSPECIFIED) 
        sb.append("UNSPECIFIED "); 
    else if (mode == EXACTLY) 
        sb.append("EXACTLY "); 
    else if (mode == AT_MOST) 
        sb.append("AT_MOST "); 
    else 
        sb.append(mode).append(" ");

    sb.append(size); 
    return sb.toString(); 
}

public static int getSize(int measureSpec) { 
    return (measureSpec & ~MODE_MASK); 
}

public static int getMode(int measureSpec) { 
    return (measureSpec & MODE_MASK); 
}

public static int makeMeasureSpec(int size, int mode) { 
    return size + mode; 
}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.