Android自定義ViewGroup之第一次接觸ViewGroup
整理總結(jié)自鴻洋的博客:http://blog.csdn.net/lmj623565791/article/details/38339817/
一、com.cctvjiatao.customviewgroup.act.MainActivity.Java
需求:我們定義一個(gè)ViewGroup,內(nèi)部可以傳入0到4個(gè)childView,分別依次顯示在左上角,右上角,左下角,右下角
public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); // setContentView(R.layout.activity_main2); // setContentView(R.layout.activity_main3); } }
二、com.cctvjiatao.customviewgroup.view.CustomViewGroup.java
一)、ViewGroup是是什么?作用呢?
1、它相當(dāng)于放置View的容器。xml布局文件中,凡是以“l(fā)ayout”開頭的屬性都是和ViewGroup(即容器)相關(guān)的,比如高度(layout_height)、寬度(layout_width)、對齊方式(layout_gravity)等;
2、它給childView計(jì)算出建議的寬、高和測量模式,決定childView的位置;
為什么是“建議的寬、高”而不是直接確定呢?因?yàn)楫?dāng)childView的寬、高設(shè)置為wrap_content時(shí),只有childView自己才能計(jì)算出自己的寬和高。
二)、View的作用是什么?
1、它根據(jù)測量模式和ViewGroup給出的建議的寬、高,計(jì)算出自己的寬、高;
2、在ViewGroup為其指定的區(qū)域內(nèi)繪制自己的形態(tài);
三)、View的三種測量模式
1、EXACTLY:表示設(shè)置了精確的值,一般當(dāng)childView設(shè)置其寬高為精確值、match_parent時(shí),ViewGroup會將其設(shè)置為EXACTLY;
2、AT_MOST:表示子布局被限制在一個(gè)最大值內(nèi),一般當(dāng)childView設(shè)置其寬、高為wrap_content時(shí),ViewGroup會將其設(shè)置為AT_MOST;
3、UNSPECIFIED:表示子布局想要多大就多大,一般出現(xiàn)在AadapterView的item的heightMode中、ScrollView的childView的heightMode中;此種模式比較少見。
四)、ViewGroup 和 LayoutParams的關(guān)系
當(dāng)在LinearLayout中寫childView的時(shí)候,可以寫layout_gravity,layout_weight屬性;
而在RelativeLayout中的childView有l(wèi)ayout_centerInParent屬性,卻沒有l(wèi)ayout_gravity,layout_weight,這是為什么呢?
這是因?yàn)槊總€(gè)ViewGroup需要指定一個(gè)LayoutParams,用于確定支持childView支持哪些屬性,比如LinearLayout指定LinearLayout.LayoutParams等
如果去看LinearLayout的源碼,會發(fā)現(xiàn)其內(nèi)部定義了LinearLayout.LayoutParams,在此類中,你可以發(fā)現(xiàn)weight和gravity的身影。
五)、從API角度分析ViewGroup和View的作用
View 根據(jù) ViewGroup 傳入的測量值和測量模式,確定自己的寬、高(在onMeasure中完成),然后在onDraw中完成對自己的繪制;
ViewGroup需要給View傳入View的測量值和測量模式(在onMeasure中完成),而且對于此ViewGroup的父布局,ViewGroup也要在onMeasure中完成對自己寬、高的確定;
ViewGroup需要再onLayout中完成對其childView的位置的指定。
public class CustomViewGroup extends ViewGroup { public CustomViewGroup(Context context) { super(context); } public CustomViewGroup(Context context, AttributeSet attrs) { super(context, attrs); } public CustomViewGroup(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); } /** * 一、重寫generateLayoutParams,確定該ViewGroup的LayoutParams * 返回MarginLayoutParams的實(shí)例,這樣就為我們的ViewGroup指定了其LayoutParams為MarginLayoutParams */ @Override public LayoutParams generateLayoutParams(AttributeSet attrs) { return new MarginLayoutParams(getContext(), attrs); } /** * 二、計(jì)算所有ChildView的寬度和高度 然后根據(jù)ChildView的計(jì)算結(jié)果,設(shè)置自己的寬和高 */ @Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { //1、獲得此ViewGroup上級容器為其推薦的寬和高,以及計(jì)算模式 int widthMode = MeasureSpec.getMode(widthMeasureSpec); int heightMode = MeasureSpec.getMode(heightMeasureSpec); int sizeWidth = MeasureSpec.getSize(widthMeasureSpec); int sizeHeight = MeasureSpec.getSize(heightMeasureSpec); // 2、計(jì)算出所有的childView的寬和高 measureChildren(widthMeasureSpec, heightMeasureSpec); // 3、如果ViewGroup布局是wrap_content時(shí),根據(jù)childView的尺寸,計(jì)算容器的寬和高 int width = 0;//ViewGroup的寬度 int height = 0;//ViewGroup的高度 int cCount = getChildCount();//childView的數(shù)量 int cWidth = 0;//childView的總寬度 int cHeight = 0;//childView的總高度 MarginLayoutParams cParams = null;//View的測量模式 int lHeight = 0;// 用于計(jì)算左邊兩個(gè)childView的高度 int rHeight = 0;// 用于計(jì)算右邊兩個(gè)childView的高度,最終高度取二者之間大值 int tWidth = 0;// 用于計(jì)算上邊兩個(gè)childView的寬度 int bWidth = 0;// 用于計(jì)算下面兩個(gè)childiew的寬度,最終寬度取二者之間大值 for (int i = 0; i < cCount; i++) { View childView = getChildAt(i); cWidth = childView.getMeasuredWidth(); cHeight = childView.getMeasuredHeight(); cParams = (MarginLayoutParams) childView.getLayoutParams(); if (i == 0 || i == 1) {// 上面兩個(gè)childView tWidth += cWidth + cParams.leftMargin + cParams.rightMargin; } if (i == 2 || i == 3) {// 下面兩個(gè)childView bWidth += cWidth + cParams.leftMargin + cParams.rightMargin; } if (i == 0 || i == 2) {// 左面兩個(gè)childView lHeight += cHeight + cParams.topMargin + cParams.bottomMargin; } if (i == 1 || i == 3) {// 右面兩個(gè)childView rHeight += cHeight + cParams.topMargin + cParams.bottomMargin; } } width = Math.max(tWidth, bWidth);//取最大寬度 height = Math.max(lHeight, rHeight);//去最大高度 //4、如果是wrap_content設(shè)置為我們計(jì)算的值;否則直接設(shè)置為父容器計(jì)算的值 setMeasuredDimension( (widthMode == MeasureSpec.EXACTLY) ? sizeWidth : width, (heightMode == MeasureSpec.EXACTLY) ? sizeHeight : height ); } /** * 三、重寫onLayout,對其所有childView進(jìn)行定位(設(shè)置childView的繪制區(qū)域) */ @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); } } }
三、三種布局
activity_main.xml
<com.cctvjiatao.customviewgroup.view.CustomViewGroup 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.cctvjiatao.customviewgroup.view.CustomViewGroup>
activity_main2.xml
<com.cctvjiatao.customviewgroup.view.CustomViewGroup 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.cctvjiatao.customviewgroup.view.CustomViewGroup>
activity_main3.xml
<com.cctvjiatao.customviewgroup.view.CustomViewGroup 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.cctvjiatao.customviewgroup.view.CustomViewGroup>
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- Android自定義控件ViewGroup實(shí)現(xiàn)標(biāo)簽云(四)
- Android自定義ViewGroup實(shí)現(xiàn)標(biāo)簽浮動(dòng)效果
- Android自定義ViewGroup之實(shí)現(xiàn)FlowLayout流式布局
- Android自定義ViewGroup的實(shí)現(xiàn)方法
- Android應(yīng)用開發(fā)中自定義ViewGroup的究極攻略
- Android App開發(fā)中自定義View和ViewGroup的實(shí)例教程
- Android應(yīng)用開發(fā)中自定義ViewGroup視圖容器的教程
- 從源碼解析Android中View的容器ViewGroup
- Android自定義ViewGroup打造各種風(fēng)格的SlidingMenu
- Android自定義ViewGroup之CustomGridLayout(一)
相關(guān)文章
Android dip,px,pt,sp 的區(qū)別詳解
本篇文章是對Android中dip,px,pt,sp的區(qū)別進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下2013-06-06RecyclerView實(shí)現(xiàn)列表倒計(jì)時(shí)
這篇文章主要為大家詳細(xì)介紹了RecyclerView實(shí)現(xiàn)列表倒計(jì)時(shí),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-09-09Android Zxing生成二維碼經(jīng)典案例分享
這篇文章主要為大家分享了Android Zxing生成二維碼經(jīng)典案例,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-11-11解決Android MediaRecorder錄制視頻過短問題
本文主要介紹Android MediaRecorder,在使用MediaRecorder時(shí)經(jīng)常會遇到視頻錄制太短問題,這里提供解決問題的實(shí)例代碼以供大家參考2016-07-07Android AIDL中Map參數(shù)傳遞的問題詳解
這篇文章主要給大家介紹了關(guān)于Android AIDL中Map參數(shù)傳遞問題的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友下面來一起看看吧。2017-12-12