日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 运维知识 > Android >内容正文

Android

Android 手把手教您自定义ViewGroup

發(fā)布時間:2025/6/15 Android 18 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Android 手把手教您自定义ViewGroup 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

最近由于工作的變動,導(dǎo)致的博客的更新計(jì)劃有點(diǎn)被打亂,希望可以盡快脈動回來~

今天給大家?guī)硪黄远xViewGroup的教程,說白了,就是教大家如何自定義ViewGroup,如果你對自定義ViewGroup還不是很了解,或者正想學(xué)習(xí)如何自定義,那么你可以好好看看這篇博客。

1、概述

在寫代碼之前,我必須得問幾個問題:

1、ViewGroup的職責(zé)是啥?

ViewGroup相當(dāng)于一個放置View的容器,并且我們在寫布局xml的時候,會告訴容器(凡是以layout為開頭的屬性,都是為用于告訴容器的),我們的寬度(layout_width)、高度(layout_height)、對齊方式(layout_gravity)等;當(dāng)然還有margin等;于是乎,ViewGroup的職能為:給childView計(jì)算出建議的寬和高和測量模式 ;決定childView的位置;為什么只是建議的寬和高,而不是直接確定呢,別忘了childView寬和高可以設(shè)置為wrap_content,這樣只有childView才能計(jì)算出自己的寬和高。

2、View的職責(zé)是啥?

View的職責(zé),根據(jù)測量模式和ViewGroup給出的建議的寬和高,計(jì)算出自己的寬和高;同時還有個更重要的職責(zé)是:在ViewGroup為其指定的區(qū)域內(nèi)繪制自己的形態(tài)。

3、ViewGroup和LayoutParams之間的關(guān)系?

大家可以回憶一下,當(dāng)在LinearLayout中寫childView的時候,可以寫layout_gravity,layout_weight屬性;在RelativeLayout中的childView有l(wèi)ayout_centerInParent屬性,卻沒有l(wèi)ayout_gravity,layout_weight,這是為什么呢?這是因?yàn)槊總€ViewGroup需要指定一個LayoutParams,用于確定支持childView支持哪些屬性,比如LinearLayout指定LinearLayout.LayoutParams等。如果大家去看LinearLayout的源碼,會發(fā)現(xiàn)其內(nèi)部定義了LinearLayout.LayoutParams,在此類中,你可以發(fā)現(xiàn)weight和gravity的身影。

2、View的3種測量模式

上面提到了ViewGroup會為childView指定測量模式,下面簡單介紹下三種測量模式:

EXACTLY:表示設(shè)置了精確的值,一般當(dāng)childView設(shè)置其寬、高為精確值、match_parent時,ViewGroup會將其設(shè)置為EXACTLY;

AT_MOST:表示子布局被限制在一個最大值內(nèi),一般當(dāng)childView設(shè)置其寬、高為wrap_content時,ViewGroup會將其設(shè)置為AT_MOST;

UNSPECIFIED:表示子布局想要多大就多大,一般出現(xiàn)在AadapterView的item的heightMode中、ScrollView的childView的heightMode中;此種模式比較少見。

注:上面的每一行都有一個一般,意思上述不是絕對的,對于childView的mode的設(shè)置還會和ViewGroup的測量mode有一定的關(guān)系;當(dāng)然了,這是第一篇自定義ViewGroup,而且絕大部分情況都是上面的規(guī)則,所以為了通俗易懂,暫不深入討論其他內(nèi)容。

3、從API角度進(jìn)行淺析

上面敘述了ViewGroup和View的職責(zé),下面從API角度進(jìn)行淺析。

View的根據(jù)ViewGroup傳人的測量值和模式,對自己寬高進(jìn)行確定(onMeasure中完成),然后在onDraw中完成對自己的繪制。

ViewGroup需要給View傳入view的測量值和模式(onMeasure中完成),而且對于此ViewGroup的父布局,自己也需要在onMeasure中完成對自己寬和高的確定。此外,需要在onLayout中完成對其childView的位置的指定。

4、完整的例子

需求:我們定義一個ViewGroup,內(nèi)部可以傳入0到4個childView,分別依次顯示在左上角,右上角,左下角,右下角。

1、決定該ViewGroup的LayoutParams

對于我們這個例子,我們只需要ViewGroup能夠支持margin即可,那么我們直接使用系統(tǒng)的MarginLayoutParams

[java]?view plaincopy
  • @Override??
  • ????public?ViewGroup.LayoutParams?generateLayoutParams(AttributeSet?attrs)??
  • ????{??
  • ????????return?new?MarginLayoutParams(getContext(),?attrs);??
  • ????}??

  • 重寫父類的該方法,返回MarginLayoutParams的實(shí)例,這樣就為我們的ViewGroup指定了其LayoutParams為MarginLayoutParams。

    2、onMeasure

    在onMeasure中計(jì)算childView的測量值以及模式,以及設(shè)置自己的寬和高:

    [java]?view plaincopy
  • /**?
  • ?????*?計(jì)算所有ChildView的寬度和高度?然后根據(jù)ChildView的計(jì)算結(jié)果,設(shè)置自己的寬和高?
  • ?????*/??
  • ????@Override??
  • ????protected?void?onMeasure(int?widthMeasureSpec,?int?heightMeasureSpec)??
  • ????{??
  • ????????/**?
  • ?????????*?獲得此ViewGroup上級容器為其推薦的寬和高,以及計(jì)算模式?
  • ?????????*/??
  • ????????int?widthMode?=?MeasureSpec.getMode(widthMeasureSpec);??
  • ????????int?heightMode?=?MeasureSpec.getMode(heightMeasureSpec);??
  • ????????int?sizeWidth?=?MeasureSpec.getSize(widthMeasureSpec);??
  • ????????int?sizeHeight?=?MeasureSpec.getSize(heightMeasureSpec);??
  • ??
  • ??
  • ????????//?計(jì)算出所有的childView的寬和高??
  • ????????measureChildren(widthMeasureSpec,?heightMeasureSpec);??
  • ????????/**?
  • ?????????*?記錄如果是wrap_content是設(shè)置的寬和高?
  • ?????????*/??
  • ????????int?width?=?0;??
  • ????????int?height?=?0;??
  • ??
  • ????????int?cCount?=?getChildCount();??
  • ??
  • ????????int?cWidth?=?0;??
  • ????????int?cHeight?=?0;??
  • ????????MarginLayoutParams?cParams?=?null;??
  • ??
  • ????????//?用于計(jì)算左邊兩個childView的高度??
  • ????????int?lHeight?=?0;??
  • ????????//?用于計(jì)算右邊兩個childView的高度,最終高度取二者之間大值??
  • ????????int?rHeight?=?0;??
  • ??
  • ????????//?用于計(jì)算上邊兩個childView的寬度??
  • ????????int?tWidth?=?0;??
  • ????????//?用于計(jì)算下面兩個childiew的寬度,最終寬度取二者之間大值??
  • ????????int?bWidth?=?0;??
  • ??
  • ????????/**?
  • ?????????*?根據(jù)childView計(jì)算的出的寬和高,以及設(shè)置的margin計(jì)算容器的寬和高,主要用于容器是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設(shè)置為我們計(jì)算的值?
  • ?????????*?否則:直接設(shè)置為父容器計(jì)算的值?
  • ?????????*/??
  • ????????setMeasuredDimension((widthMode?==?MeasureSpec.EXACTLY)???sizeWidth??
  • ????????????????:?width,?(heightMode?==?MeasureSpec.EXACTLY)???sizeHeight??
  • ????????????????:?height);??
  • ????}??

  • 10-14行,獲取該ViewGroup父容器為其設(shè)置的計(jì)算模式和尺寸,大多情況下,只要不是wrap_content,父容器都能正確的計(jì)算其尺寸。所以我們自己需要計(jì)算如果設(shè)置為wrap_content時的寬和高,如何計(jì)算呢?那就是通過其childView的寬和高來進(jìn)行計(jì)算。

    17行,通過ViewGroup的measureChildren方法為其所有的孩子設(shè)置寬和高,此行執(zhí)行完成后,childView的寬和高都已經(jīng)正確的計(jì)算過了

    43-71行,根據(jù)childView的寬和高,以及margin,計(jì)算ViewGroup在wrap_content時的寬和高。

    80-82行,如果寬高屬性值為wrap_content,則設(shè)置為43-71行中計(jì)算的值,否則為其父容器傳入的寬和高。

    3、onLayout對其所有childView進(jìn)行定位(設(shè)置childView的繪制區(qū)域)

    [java]?view plaincopy
  • //?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根據(jù)其寬和高,以及margin進(jìn)行布局?
  • ?????????*/??
  • ????????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,根據(jù)childView的寬和高以及margin,然后分別將0,1,2,3位置的childView依次設(shè)置到左上、右上、左下、右下的位置。

    如果是第一個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代碼的編寫,下面我們進(jìn)行測試,分別設(shè)置寬高為固定值,wrap_content,match_parent

    5、測試結(jié)果

    布局1:

    [html]?view plaincopy
  • <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寬和高設(shè)置為固定值

    效果圖:



    布局2:

    [html]?view plaincopy
  • <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的寬和高設(shè)置為wrap_content
    效果圖:



    布局3:

    [html]?view plaincopy
  • <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的寬和高設(shè)置為match_parent



    可以看到無論ViewGroup的寬和高的值如何定義,我們的需求都實(shí)現(xiàn)了預(yù)期的效果~~

    好了,通過這篇教程,希望大家已經(jīng)能夠初步掌握自定義ViewGroup的步驟,大家可以嘗試自定義ViewGroup去實(shí)現(xiàn)LinearLayout的效果~~

    最后說明下,此為第一篇ViewGroup的教程,以后還會進(jìn)一步的探討如何更好的自定義ViewGroup~~~


    如果你覺得這篇文章對你有用,那么贊一個或者留個言吧~

    總結(jié)

    以上是生活随笔為你收集整理的Android 手把手教您自定义ViewGroup的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。