經過重寫ViewGroup學習onMeasure()和onLayout()方法

在繼承ViewGroup類時,須要重寫兩個方法,分別是onMeasure和onLayout。

1,在方法onMeasure中調用setMeasuredDimension方法

void android.view.View.setMeasuredDimension(int measuredWidth, int measuredHeight)

在onMeasure(int, int)中,必須調用setMeasuredDimension(int width, int height)來存儲測量獲得的寬度和高度值,若是沒有這麼去作會觸發異常IllegalStateException。

2,在方法onMeasure中調用孩子的measure方法

void android.view.View.measure(int widthMeasureSpec, int heightMeasureSpec)

這個方法用來測量出view的大小。父view使用width參數和height參數來提供constraint信息。實際上,view的測量工做在onMeasure(int, int)方法中完成。所以,只有onMeasure(int, int)方法能夠且必須被重寫。參數widthMeasureSpec提供view的水平空間的規格說明,參數heightMeasureSpec提供view的垂直空間的規格說明。

3,解析onMeasure(int, int)方法

void android.view.View.onMeasure(int widthMeasureSpec, int heightMeasureSpec)

測量view及其內容來肯定view的寬度和高度。這個方法在measure(int, int)中被調用,必須被重寫來精確和有效的測量view的內容。

在重寫這個方法時,必須調用setMeasuredDimension(int, int)來存儲測量獲得的寬度和高度值。執行失敗會觸發一個IllegalStateException異常。調用父view的onMeasure(int, int)是合法有效的用法。

view的基本測量數據默認取其背景尺寸,除非容許更大的尺寸。子view必須重寫onMeasure(int, int)來提供其內容更加準確的測量數值。若是被重寫,子類確保測量的height和width至少是view的最小高度和寬度(經過getSuggestedMinimumHeight()和getSuggestedMinimumWidth()獲取)。

4,解析onLayout(boolean, int, int, int, int)方法

void android.view.ViewGroup.onLayout(boolean changed, int l, int t, int r, int b)

調用場景:在view給其孩子設置尺寸和位置時被調用。子view,包括孩子在內,必須重寫onLayout(boolean, int, int, int, int)方法,而且調用各自的layout(int, int, int, int)方法。

參數說明:參數changed表示view有新的尺寸或位置;參數l表示相對於父view的Left位置;參數t表示相對於父view的Top位置;參數r表示相對於父view的Right位置;參數b表示相對於父view的Bottom位置。.

5,解析View.MeasureSpec類

android.view.View.MeasureSpec

MeasureSpec對象,封裝了layout規格說明,而且從父view傳遞給子view。每一個MeasureSpec對象表明了width或height的規格。

MeasureSpec對象包含一個size和一個mode,其中mode能夠取如下三個數值之一:

    UNSPECIFIED,1073741824 [0x40000000],未加規定的,表示沒有給子view添加任何規定。
    EXACTLY,0 [0x0],精確的,表示父view爲子view肯定精確的尺寸。html

    AT_MOST,-2147483648 [0x80000000],子view能夠在指定的尺寸內儘可能大。java

在這裏給你們舉一個例子demo:android

第一步:自定義一個View實現ViewGroup接口,即自定義ViewGroup:app

 

[java] view plain copyide

 在CODE上查看代碼片派生到個人代碼片

  1. package net.loonggg.viewgroup;  
  2.   
  3. import android.content.Context;  
  4. import android.util.AttributeSet;  
  5. import android.view.View;  
  6. import android.view.ViewGroup;  
  7.   
  8. public class MyViewGroup extends ViewGroup {  
  9.   
  10.     public MyViewGroup(Context context) {  
  11.         super(context);  
  12.     }  
  13.   
  14.     public MyViewGroup(Context context, AttributeSet attrs) {  
  15.         super(context, attrs);  
  16.     }  
  17.   
  18.     public MyViewGroup(Context context, AttributeSet attrs, int defStyle) {  
  19.         super(context, attrs, defStyle);  
  20.     }  
  21.   
  22.     /** 
  23.      * 計算控件的大小 
  24.      */  
  25.     @Override  
  26.     protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {  
  27.         super.onMeasure(widthMeasureSpec, heightMeasureSpec);  
  28.         int measureWidth = measureWidth(widthMeasureSpec);  
  29.         int measureHeight = measureHeight(heightMeasureSpec);  
  30.         // 計算自定義的ViewGroup中全部子控件的大小  
  31.         measureChildren(widthMeasureSpec, heightMeasureSpec);  
  32.         // 設置自定義的控件MyViewGroup的大小  
  33.         setMeasuredDimension(measureWidth, measureHeight);  
  34.     }  
  35.   
  36.     private int measureWidth(int pWidthMeasureSpec) {  
  37.         int result = 0;  
  38.         int widthMode = MeasureSpec.getMode(pWidthMeasureSpec);// 獲得模式  
  39.         int widthSize = MeasureSpec.getSize(pWidthMeasureSpec);// 獲得尺寸  
  40.   
  41.         switch (widthMode) {  
  42.         /** 
  43.          * mode共有三種狀況,取值分別爲MeasureSpec.UNSPECIFIED, MeasureSpec.EXACTLY, 
  44.          * MeasureSpec.AT_MOST。 
  45.          *  
  46.          *  
  47.          * MeasureSpec.EXACTLY是精確尺寸, 
  48.          * 當咱們將控件的layout_width或layout_height指定爲具體數值時如andorid 
  49.          * :layout_width="50dip",或者爲FILL_PARENT是,都是控件大小已經肯定的狀況,都是精確尺寸。 
  50.          *  
  51.          *  
  52.          * MeasureSpec.AT_MOST是最大尺寸, 
  53.          * 當控件的layout_width或layout_height指定爲WRAP_CONTENT時 
  54.          * ,控件大小通常隨着控件的子空間或內容進行變化,此時控件尺寸只要不超過父控件容許的最大尺寸便可 
  55.          * 。所以,此時的mode是AT_MOST,size給出了父控件容許的最大尺寸。 
  56.          *  
  57.          *  
  58.          * MeasureSpec.UNSPECIFIED是未指定尺寸,這種狀況很少,通常都是父控件是AdapterView, 
  59.          * 經過measure方法傳入的模式。 
  60.          */  
  61.         case MeasureSpec.AT_MOST:  
  62.         case MeasureSpec.EXACTLY:  
  63.             result = widthSize;  
  64.             break;  
  65.         }  
  66.         return result;  
  67.     }  
  68.   
  69.     private int measureHeight(int pHeightMeasureSpec) {  
  70.         int result = 0;  
  71.   
  72.         int heightMode = MeasureSpec.getMode(pHeightMeasureSpec);  
  73.         int heightSize = MeasureSpec.getSize(pHeightMeasureSpec);  
  74.   
  75.         switch (heightMode) {  
  76.         case MeasureSpec.AT_MOST:  
  77.         case MeasureSpec.EXACTLY:  
  78.             result = heightSize;  
  79.             break;  
  80.         }  
  81.         return result;  
  82.     }  
  83.   
  84.     /** 
  85.      * 覆寫onLayout,其目的是爲了指定視圖的顯示位置,方法執行的先後順序是在onMeasure以後,由於視圖確定是只有知道大小的狀況下, 
  86.      * 才能肯定怎麼擺放 
  87.      */  
  88.     @Override  
  89.     protected void onLayout(boolean changed, int l, int t, int r, int b) {  
  90.         // 記錄總高度  
  91.         int mTotalHeight = 0;  
  92.         // 遍歷全部子視圖  
  93.         int childCount = getChildCount();  
  94.         for (int i = 0; i < childCount; i++) {  
  95.             View childView = getChildAt(i);  
  96.   
  97.             // 獲取在onMeasure中計算的視圖尺寸  
  98.             int measureHeight = childView.getMeasuredHeight();  
  99.             int measuredWidth = childView.getMeasuredWidth();  
  100.   
  101.             childView.layout(l, mTotalHeight, measuredWidth, mTotalHeight  
  102.                     + measureHeight);  
  103.   
  104.             mTotalHeight += measureHeight;  
  105.   
  106.         }  
  107.     }  
  108.   
  109. }  

第二步,佈局文件:佈局

 

 

[html] view plain copyspa

 在CODE上查看代碼片派生到個人代碼片

  1. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  2.     xmlns:tools="http://schemas.android.com/tools"  
  3.     android:layout_width="match_parent"  
  4.     android:layout_height="match_parent"  
  5.     android:background="#00f0f0"  
  6.     tools:context=".MainActivity" >  
  7.   
  8.     <net.loonggg.viewgroup.MyViewGroup  
  9.         android:id="@+id/myViewGroup"  
  10.         android:layout_width="480dp"  
  11.         android:layout_height="300dp"  
  12.         android:background="#0f0f0f" >  
  13.   
  14.         <TextView  
  15.             android:layout_width="200dp"  
  16.             android:layout_height="100dp"  
  17.             android:background="#000000"  
  18.             android:gravity="center"  
  19.             android:text="第一個TextView" />  
  20.   
  21.         <TextView  
  22.             android:layout_width="100dp"  
  23.             android:layout_height="200dp"  
  24.             android:background="#ffffff"  
  25.             android:gravity="center"  
  26.             android:text="第二個TextView" />  
  27.     </net.loonggg.viewgroup.MyViewGroup>  
  28.   
  29. </RelativeLayout>  


第三步,MainActivity.java:.net

 

 

[java] view plain copycode

 在CODE上查看代碼片派生到個人代碼片

  1. package net.loonggg.viewgroup;  
  2.   
  3. import android.os.Bundle;  
  4. import android.app.Activity;  
  5.   
  6. public class MainActivity extends Activity {  
  7.   
  8.     @Override  
  9.     protected void onCreate(Bundle savedInstanceState) {  
  10.         super.onCreate(savedInstanceState);  
  11.         setContentView(R.layout.activity_main);  
  12.     }  
  13.   
  14. }  
相關文章
相關標籤/搜索