轉載請標明出處:http://blog.csdn.net/lmj623565791/article/details/38339817 , 本文出自:【張鴻洋的博客】java
最近因爲工做的變更,致使的博客的更新計劃有點被打亂,但願能夠儘快脈動回來~android
今天給你們帶來一篇自定義ViewGroup的教程,說白了,就是教你們如何自定義ViewGroup,若是你對自定義ViewGroup還不是很瞭解,或者正想學習如何自定義,那麼你能夠好好看看這篇博客。ide
在寫代碼以前,我必須得問幾個問題:佈局
ViewGroup至關於一個放置View的容器,而且咱們在寫佈局xml的時候,會告訴容器(凡是以layout爲開頭的屬性,都是爲用於告訴容器的),咱們的寬度(layout_width)、高度(layout_height)、對齊方式(layout_gravity)等;固然還有margin等;因而乎,ViewGroup的職能爲:給childView計算出建議的寬和高和測量模式 ;決定childView的位置;爲何只是建議的寬和高,而不是直接肯定呢,別忘了childView寬和高能夠設置爲wrap_content,這樣只有childView才能計算出本身的寬和高。學習
View的職責,根據測量模式和ViewGroup給出的建議的寬和高,計算出本身的寬和高;同時還有個更重要的職責是:在ViewGroup爲其指定的區域內繪製本身的形態。測試
你們能夠回憶一下,當在LinearLayout中寫childView的時候,能夠寫layout_gravity,layout_weight屬性;在RelativeLayout中的childView有layout_centerInParent屬性,卻沒有layout_gravity,layout_weight,這是爲何呢?這是由於每一個ViewGroup須要指定一個LayoutParams,用於肯定支持childView支持哪些屬性,好比LinearLayout指定LinearLayout.LayoutParams等。若是你們去看LinearLayout的源碼,會發現其內部定義了LinearLayout.LayoutParams,在此類中,你能夠發現weight和gravity的身影。spa
上面提到了ViewGroup會爲childView指定測量模式,下面簡單介紹下三種測量模式:.net
EXACTLY:表示設置了精確的值,通常當childView設置其寬、高爲精確值、match_parent時,ViewGroup會將其設置爲EXACTLY;code
AT_MOST:表示子佈局被限制在一個最大值內,通常當childView設置其寬、高爲wrap_content時,ViewGroup會將其設置爲AT_MOST;xml
UNSPECIFIED:表示子佈局想要多大就多大,通常出如今AadapterView的item的heightMode中、ScrollView的childView的heightMode中;此種模式比較少見。
注:上面的每一行都有一個通常,意思上述不是絕對的,對於childView的mode的設置還會和ViewGroup的測量mode有必定的關係;固然了,這是第一篇自定義ViewGroup,並且絕大部分狀況都是上面的規則,因此爲了通俗易懂,暫不深刻討論其餘內容。
上面敘述了ViewGroup和View的職責,下面從API角度進行淺析。
View的根據ViewGroup傳人的測量值和模式,對本身寬高進行肯定(onMeasure中完成),而後在onDraw中完成對本身的繪製。
ViewGroup須要給View傳入view的測量值和模式(onMeasure中完成),並且對於此ViewGroup的父佈局,本身也須要在onMeasure中完成對本身寬和高的肯定。此外,須要在onLayout中完成對其childView的位置的指定。
需求:咱們定義一個ViewGroup,內部能夠傳入0到4個childView,分別依次顯示在左上角,右上角,左下角,右下角。
對於咱們這個例子,咱們只須要ViewGroup可以支持margin便可,那麼咱們直接使用系統的MarginLayoutParams
@Override public ViewGroup.LayoutParams generateLayoutParams(AttributeSet attrs) { return new MarginLayoutParams(getContext(), attrs); }
重寫父類的該方法,返回MarginLayoutParams的實例,這樣就爲咱們的ViewGroup指定了其LayoutParams爲MarginLayoutParams。
在onMeasure中計算childView的測量值以及模式,以及設置本身的寬和高:
/** * 計算全部ChildView的寬度和高度 而後根據ChildView的計算結果,設置本身的寬和高 */ @Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { /** * 得到此ViewGroup上級容器爲其推薦的寬和高,以及計算模式 */ int widthMode = MeasureSpec.getMode(widthMeasureSpec); int heightMode = MeasureSpec.getMode(heightMeasureSpec); int sizeWidth = MeasureSpec.getSize(widthMeasureSpec); int sizeHeight = MeasureSpec.getSize(heightMeasureSpec); // 計算出全部的childView的寬和高 measureChildren(widthMeasureSpec, heightMeasureSpec); /** * 記錄若是是wrap_content是設置的寬和高 */ int width = 0; int height = 0; int cCount = getChildCount(); int cWidth = 0; int cHeight = 0; MarginLayoutParams cParams = null; // 用於計算左邊兩個childView的高度 int lHeight = 0; // 用於計算右邊兩個childView的高度,最終高度取兩者之間大值 int rHeight = 0; // 用於計算上邊兩個childView的寬度 int tWidth = 0; // 用於計算下面兩個childiew的寬度,最終寬度取兩者之間大值 int bWidth = 0; /** * 根據childView計算的出的寬和高,以及設置的margin計算容器的寬和高,主要用於容器是warp_content時 */ for (int i = 0; i < cCount; i++) { View childView = getChildAt(i); cWidth = childView.getMeasuredWidth(); cHeight = childView.getMeasuredHeight(); cParams = (MarginLayoutParams) childView.getLayoutParams(); // 上面兩個childView if (i == 0 || i == 1) { tWidth += cWidth + cParams.leftMargin + cParams.rightMargin; } if (i == 2 || i == 3) { bWidth += cWidth + cParams.leftMargin + cParams.rightMargin; } if (i == 0 || i == 2) { lHeight += cHeight + cParams.topMargin + cParams.bottomMargin; } if (i == 1 || i == 3) { rHeight += cHeight + cParams.topMargin + cParams.bottomMargin; } } width = Math.max(tWidth, bWidth); height = Math.max(lHeight, rHeight); /** * 若是是wrap_content設置爲咱們計算的值 * 不然:直接設置爲父容器計算的值 */ setMeasuredDimension((widthMode == MeasureSpec.EXACTLY) ? sizeWidth : width, (heightMode == MeasureSpec.EXACTLY) ? sizeHeight : height); }
10-14行,獲取該ViewGroup父容器爲其設置的計算模式和尺寸,大多狀況下,只要不是wrap_content,父容器都能正確的計算其尺寸。因此咱們本身須要計算若是設置爲wrap_content時的寬和高,如何計算呢?那就是經過其childView的寬和高來進行計算。
17行,經過ViewGroup的measureChildren方法爲其全部的孩子設置寬和高,此行執行完成後,childView的寬和高都已經正確的計算過了
43-71行,根據childView的寬和高,以及margin,計算ViewGroup在wrap_content時的寬和高。
80-82行,若是寬高屬性值爲wrap_content,則設置爲43-71行中計算的值,不然爲其父容器傳入的寬和高。
// abstract method in viewgroup @Override protected void onLayout(boolean changed, int l, int t, int r, int b) { int cCount = getChildCount(); int cWidth = 0; int cHeight = 0; MarginLayoutParams cParams = null; /** * 遍歷全部childView根據其寬和高,以及margin進行佈局 */ for (int i = 0; i < cCount; i++) { View childView = getChildAt(i); cWidth = childView.getMeasuredWidth(); cHeight = childView.getMeasuredHeight(); cParams = (MarginLayoutParams) childView.getLayoutParams(); int cl = 0, ct = 0, cr = 0, cb = 0; switch (i) { case 0: cl = cParams.leftMargin; ct = cParams.topMargin; break; case 1: cl = getWidth() - cWidth - cParams.leftMargin - cParams.rightMargin; ct = cParams.topMargin; break; case 2: cl = cParams.leftMargin; ct = getHeight() - cHeight - cParams.bottomMargin; break; case 3: cl = getWidth() - cWidth - cParams.leftMargin - cParams.rightMargin; ct = getHeight() - cHeight - cParams.bottomMargin; break; } cr = cl + cWidth; cb = cHeight + ct; childView.layout(cl, ct, cr, cb); } }
代碼比較容易懂:遍歷全部的childView,根據childView的寬和高以及margin,而後分別將0,1,2,3位置的childView依次設置到左上、右上、左下、右下的位置。
若是是第一個View(index=0) :則childView.layout(cl, ct, cr, cb); cl爲childView的leftMargin , ct 爲topMargin , cr 爲cl+ cWidth , cb爲 ct + cHeight
若是是第二個View(index=1) :則childView.layout(cl, ct, cr, cb);
cl爲getWidth() - cWidth - cParams.leftMargin- cParams.rightMargin;
ct 爲topMargin , cr 爲cl+ cWidth , cb爲 ct + cHeight
剩下兩個相似~
這樣就完成了,咱們的ViewGroup代碼的編寫,下面咱們進行測試,分別設置寬高爲固定值,wrap_content,match_parent
<com.example.zhy_custom_viewgroup.CustomImgContainer xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="200dp" android:layout_height="200dp" android:background="#AA333333" > <TextView android:layout_width="50dp" android:layout_height="50dp" android:background="#FF4444" android:gravity="center" android:text="0" android:textColor="#FFFFFF" android:textSize="22sp" android:textStyle="bold" /> <TextView android:layout_width="50dp" android:layout_height="50dp" android:background="#00ff00" android:gravity="center" android:text="1" android:textColor="#FFFFFF" android:textSize="22sp" android:textStyle="bold" /> <TextView android:layout_width="50dp" android:layout_height="50dp" android:background="#ff0000" android:gravity="center" android:text="2" android:textColor="#FFFFFF" android:textSize="22sp" android:textStyle="bold" /> <TextView android:layout_width="50dp" android:layout_height="50dp" android:background="#0000ff" android:gravity="center" android:text="3" android:textColor="#FFFFFF" android:textSize="22sp" android:textStyle="bold" /> </com.example.zhy_custom_viewgroup.CustomImgContainer>
ViewGroup寬和高設置爲固定值
效果圖:
<com.example.zhy_custom_viewgroup.CustomImgContainer xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="#AA333333" > <TextView android:layout_width="150dp" android:layout_height="150dp" android:background="#E5ED05" android:gravity="center" android:text="0" android:textColor="#FFFFFF" android:textSize="22sp" android:textStyle="bold" /> <TextView android:layout_width="50dp" android:layout_height="50dp" android:background="#00ff00" android:gravity="center" android:text="1" android:textColor="#FFFFFF" android:textSize="22sp" android:textStyle="bold" /> <TextView android:layout_width="50dp" android:layout_height="50dp" android:background="#ff0000" android:gravity="center" android:text="2" android:textColor="#FFFFFF" android:textSize="22sp" android:textStyle="bold" /> <TextView android:layout_width="50dp" android:layout_height="50dp" android:background="#0000ff" android:gravity="center" android:text="3" android:textColor="#FFFFFF" android:textSize="22sp" android:textStyle="bold" /> </com.example.zhy_custom_viewgroup.CustomImgContainer>
ViewGroup的寬和高設置爲wrap_content
效果圖:
<com.example.zhy_custom_viewgroup.CustomImgContainer 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="#AA333333" > <TextView android:layout_width="150dp" android:layout_height="150dp" android:background="#E5ED05" android:gravity="center" android:text="0" android:textColor="#FFFFFF" android:textSize="22sp" android:textStyle="bold" /> <TextView android:layout_width="50dp" android:layout_height="50dp" android:background="#00ff00" android:gravity="center" android:text="1" android:textColor="#FFFFFF" android:textSize="22sp" android:textStyle="bold" /> <TextView android:layout_width="50dp" android:layout_height="50dp" android:background="#ff0000" android:gravity="center" android:text="2" android:textColor="#FFFFFF" android:textSize="22sp" android:textStyle="bold" /> <TextView android:layout_width="150dp" android:layout_height="150dp" android:background="#0000ff" android:gravity="center" android:text="3" android:textColor="#FFFFFF" android:textSize="22sp" android:textStyle="bold" /> </com.example.zhy_custom_viewgroup.CustomImgContainer>
ViewGroup的寬和高設置爲match_parent
能夠看到不管ViewGroup的寬和高的值如何定義,咱們的需求都實現了預期的效果~~
好了,經過這篇教程,但願你們已經可以初步掌握自定義ViewGroup的步驟,你們能夠嘗試自定義ViewGroup去實現LinearLayout的效果~~
最後說明下,此爲第一篇ViewGroup的教程,之後還會進一步的探討如何更好的自定義ViewGroup~~~
若是你以爲這篇文章對你有用,那麼贊一個或者留個言吧~