Android 重寫ViewGroup 分析onMeasure()和onLayout()方法
Android 重寫ViewGroup 分析onMeasure()和onLayout()方法
在繼承ViewGroup類時,需要重寫兩個方法,分別是onMeasure和onLayout。
1,在方法onMeasure中調(diào)用setMeasuredDimension方法
void android.view.View.setMeasuredDimension(int measuredWidth, int measuredHeight)
在onMeasure(int, int)中,必須調(diào)用setMeasuredDimension(int width, int height)來存儲測量得到的寬度和高度值,如果沒有這么去做會觸發(fā)異常IllegalStateException。
2,在方法onMeasure中調(diào)用孩子的measure方法
void android.view.View.measure(int widthMeasureSpec, int heightMeasureSpec)
這個方法用來測量出view的大小。父view使用width參數(shù)和height參數(shù)來提供constraint信息。實際上,view的測量工作在onMeasure(int, int)方法中完成。因此,只有onMeasure(int, int)方法可以且必須被重寫。參數(shù)widthMeasureSpec提供view的水平空間的規(guī)格說明,參數(shù)heightMeasureSpec提供view的垂直空間的規(guī)格說明。
3,解析onMeasure(int, int)方法
void android.view.View.onMeasure(int widthMeasureSpec, int heightMeasureSpec)
測量view及其內(nèi)容來確定view的寬度和高度。這個方法在measure(int, int)中被調(diào)用,必須被重寫來精確和有效的測量view的內(nèi)容。
在重寫這個方法時,必須調(diào)用setMeasuredDimension(int, int)來存儲測量得到的寬度和高度值。執(zhí)行失敗會觸發(fā)一個IllegalStateException異常。調(diào)用父view的onMeasure(int, int)是合法有效的用法。
view的基本測量數(shù)據(jù)默認取其背景尺寸,除非允許更大的尺寸。子view必須重寫onMeasure(int, int)來提供其內(nèi)容更加準確的測量數(shù)值。如果被重寫,子類確保測量的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)
調(diào)用場景:在view給其孩子設(shè)置尺寸和位置時被調(diào)用。子view,包括孩子在內(nèi),必須重寫onLayout(boolean, int, int, int, int)方法,并且調(diào)用各自的layout(int, int, int, int)方法。
參數(shù)說明:參數(shù)changed表示view有新的尺寸或位置;參數(shù)l表示相對于父view的Left位置;參數(shù)t表示相對于父view的Top位置;參數(shù)r表示相對于父view的Right位置;參數(shù)b表示相對于父view的Bottom位置。.
5,解析View.MeasureSpec類
android.view.View.MeasureSpec
MeasureSpec對象,封裝了layout規(guī)格說明,并且從父view傳遞給子view。每個MeasureSpec對象代表了width或height的規(guī)格。
MeasureSpec對象包含一個size和一個mode,其中mode可以取以下三個數(shù)值之一:
- UNSPECIFIED,1073741824 [0x40000000],未加規(guī)定的,表示沒有給子view添加任何規(guī)定。
- EXACTLY,0 [0x0],精確的,表示父view為子view確定精確的尺寸。
- AT_MOST,-2147483648 [0x80000000],子view可以在指定的尺寸內(nèi)盡量大。
在這里給大家舉一個例子demo:
第一步:自定義一個View實現(xiàn)ViewGroup接口,即自定義ViewGroup:
package net.loonggg.viewgroup; import android.content.Context; import android.util.AttributeSet; import android.view.View; import android.view.ViewGroup; public class MyViewGroup extends ViewGroup { public MyViewGroup(Context context) { super(context); } public MyViewGroup(Context context, AttributeSet attrs) { super(context, attrs); } public MyViewGroup(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); } /** * 計算控件的大小 */ @Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { super.onMeasure(widthMeasureSpec, heightMeasureSpec); int measureWidth = measureWidth(widthMeasureSpec); int measureHeight = measureHeight(heightMeasureSpec); // 計算自定義的ViewGroup中所有子控件的大小 measureChildren(widthMeasureSpec, heightMeasureSpec); // 設(shè)置自定義的控件MyViewGroup的大小 setMeasuredDimension(measureWidth, measureHeight); } private int measureWidth(int pWidthMeasureSpec) { int result = 0; int widthMode = MeasureSpec.getMode(pWidthMeasureSpec);// 得到模式 int widthSize = MeasureSpec.getSize(pWidthMeasureSpec);// 得到尺寸 switch (widthMode) { /** * mode共有三種情況,取值分別為MeasureSpec.UNSPECIFIED, MeasureSpec.EXACTLY, * MeasureSpec.AT_MOST。 * * * MeasureSpec.EXACTLY是精確尺寸, * 當我們將控件的layout_width或layout_height指定為具體數(shù)值時如andorid * :layout_width="50dip",或者為FILL_PARENT是,都是控件大小已經(jīng)確定的情況,都是精確尺寸。 * * * MeasureSpec.AT_MOST是最大尺寸, * 當控件的layout_width或layout_height指定為WRAP_CONTENT時 * ,控件大小一般隨著控件的子空間或內(nèi)容進行變化,此時控件尺寸只要不超過父控件允許的最大尺寸即可 * 。因此,此時的mode是AT_MOST,size給出了父控件允許的最大尺寸。 * * * MeasureSpec.UNSPECIFIED是未指定尺寸,這種情況不多,一般都是父控件是AdapterView, * 通過measure方法傳入的模式。 */ case MeasureSpec.AT_MOST: case MeasureSpec.EXACTLY: result = widthSize; break; } return result; } private int measureHeight(int pHeightMeasureSpec) { int result = 0; int heightMode = MeasureSpec.getMode(pHeightMeasureSpec); int heightSize = MeasureSpec.getSize(pHeightMeasureSpec); switch (heightMode) { case MeasureSpec.AT_MOST: case MeasureSpec.EXACTLY: result = heightSize; break; } return result; } /** * 覆寫onLayout,其目的是為了指定視圖的顯示位置,方法執(zhí)行的前后順序是在onMeasure之后,因為視圖肯定是只有知道大小的情況下, * 才能確定怎么擺放 */ @Override protected void onLayout(boolean changed, int l, int t, int r, int b) { // 記錄總高度 int mTotalHeight = 0; // 遍歷所有子視圖 int childCount = getChildCount(); for (int i = 0; i < childCount; i++) { View childView = getChildAt(i); // 獲取在onMeasure中計算的視圖尺寸 int measureHeight = childView.getMeasuredHeight(); int measuredWidth = childView.getMeasuredWidth(); childView.layout(l, mTotalHeight, measuredWidth, mTotalHeight + measureHeight); mTotalHeight += measureHeight; } } }
第二步,布局文件:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:background="#00f0f0" tools:context=".MainActivity" > <net.loonggg.viewgroup.MyViewGroup android:id="@+id/myViewGroup" android:layout_width="480dp" android:layout_height="300dp" android:background="#0f0f0f" > <TextView android:layout_width="200dp" android:layout_height="100dp" android:background="#000000" android:gravity="center" android:text="第一個TextView" /> <TextView android:layout_width="100dp" android:layout_height="200dp" android:background="#ffffff" android:gravity="center" android:text="第二個TextView" /> </net.loonggg.viewgroup.MyViewGroup> </RelativeLayout>
第三步,MainActivity.java:
package net.loonggg.viewgroup; import android.os.Bundle; import android.app.Activity; public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } }
感謝閱讀,希望能幫助到大家,謝謝大家對本站的支持!
相關(guān)文章
Android總結(jié)之WebView與Javascript交互(互相調(diào)用)
本篇文章主要介紹了WebView與Javascript進行數(shù)據(jù)交互,詳解的講訴了WebView與Javascript進行數(shù)據(jù)交互的方法,有興趣的可以了解一下。2016-11-11Android實現(xiàn)捕獲未知異常并提交給服務(wù)器的方法
這篇文章主要介紹了Android實現(xiàn)捕獲未知異常并提交給服務(wù)器的方法,涉及Android的異常與錯誤處理機制相關(guān)操作技巧,需要的朋友可以參考下2016-08-08Android自定義View繪制貝塞爾曲線實現(xiàn)流程
貝塞爾曲線的本質(zhì)是通過數(shù)學計算的公式來繪制平滑的曲線,分為一階,二階,三階及多階。但是這里不講數(shù)學公式和驗證,那些偉大的數(shù)學家已經(jīng)證明過了,所以就只講講Android開發(fā)中的運用吧2022-11-11Android自定義View實現(xiàn)音頻播放圓形進度條
這篇文章主要為大家詳細介紹了Android自定義View實現(xiàn)音頻播放圓形進度條,具有一定的參考價值,感興趣的小伙伴們可以參考一下2018-06-06Android使用GridView實現(xiàn)橫向滾動效果
這篇文章主要為大家詳細介紹了Android使用GridView實現(xiàn)橫向滾動效果,具有一定的參考價值,感興趣的小伙伴們可以參考一下2018-07-07Android中使用TextView實現(xiàn)圖文混排的方法
向TextView或EditText中添加圖像比直接添加文本復(fù)雜一點點,需要用到<img>標簽。接下來通過本文給大家介紹Android中使用TextView實現(xiàn)圖文混排的方法,希望對大家有所幫助2016-02-02淺析Flutter AbsorbPointer 與 IgnorePointer的區(qū)別
Flutter是Google一個新的用于構(gòu)建跨平臺的手機App的SDK。這篇文章主要介紹了Flutter AbsorbPointer 與 IgnorePointer的區(qū)別,本文通過實例代碼給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-04-04