自定义ViewGroup(1)
生活随笔
收集整理的這篇文章主要介紹了
自定义ViewGroup(1)
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
源碼來自?http://blog.csdn.net/lmj623565791/article/details/38339817
升級代碼?http://blog.csdn.net/lmj623565791/article/details/38352503 ?實現(xiàn)FlowLayout
主要是?onMeasure 和?onLayout 的運用
package com.weidingqiang.testviewb;import android.content.Context; import android.util.AttributeSet; import android.view.View; import android.view.ViewGroup;/*** Created by weidingqiang on 15/11/19.*/ public class BaViewGroup extends ViewGroup {public BaViewGroup(Context context) {this(context,null);}public BaViewGroup(Context context, AttributeSet attrs) {super(context, attrs);}@Overrideprotected 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是設(shè)置的寬和高*/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;/*** 根據(jù)childView計算的出的寬和高,以及設(shè)置的margin計算容器的寬和高,主要用于容器是warp_content時*/for (int i = 0; i < cCount; i++){View childView = getChildAt(i);cWidth = childView.getMeasuredWidth();cHeight = childView.getMeasuredHeight();cParams = (MarginLayoutParams) childView.getLayoutParams();// 上面兩個childViewif (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è)置為我們計算的值* 否則:直接設(shè)置為父容器計算的值*/setMeasuredDimension((widthMode == MeasureSpec.EXACTLY) ? sizeWidth: width, (heightMode == MeasureSpec.EXACTLY) ? sizeHeight: height);}@Overrideprotected 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進行布局*/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);}}@Overrideprotected void onSizeChanged(int w, int h, int oldw, int oldh) {super.onSizeChanged(w, h, oldw, oldh);}@Overridepublic ViewGroup.LayoutParams generateLayoutParams(AttributeSet attrs){return new MarginLayoutParams(getContext(), attrs);} }布局文件
<com.weidingqiang.testviewb.BaViewGroupandroid:layout_width="300dp"android:layout_height="300dp"android:background="#ff0"><TextViewandroid: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" /><TextViewandroid: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" /><TextViewandroid: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" /><TextViewandroid: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.weidingqiang.testviewb.BaViewGroup>
轉(zhuǎn)載于:https://www.cnblogs.com/weidingqiang/p/4979887.html
總結(jié)
以上是生活随笔為你收集整理的自定义ViewGroup(1)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: KVO与KVC
- 下一篇: 进击的UI---------------