自定義ViewGroup-通過內邊距加深理解
阿新 • • 發佈:2018-12-23
讀取 4 個方向的
padding:
activity_main:
結果展示:
padding:
public int getPaddingLeft () 離左邊的 padding
public int getPaddingRight () 離右邊的 padding
public int getPaddingTop () 離頂部的 padding
public int getPaddingRight () 離底部的 padding
CustomViewGroup3:
public class CustomViewGroup3 extends ViewGroup { public CustomViewGroup3(Context context) { super(context); } public CustomViewGroup3(Context context, AttributeSet attrs) { this(context, attrs, 0); } public CustomViewGroup3(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); } @Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { super.onMeasure(widthMeasureSpec, heightMeasureSpec); //測量所有的子View measureChildren(widthMeasureSpec, heightMeasureSpec); //測量自己的寬高 int width = measureWidth(widthMeasureSpec); int height = measureHeight(heightMeasureSpec); setMeasuredDimension(width, height); } private int measureHeight(int widthMeasureSpec) { //父控制元件建議自己測量的值 int measureMode = MeasureSpec.getMode(widthMeasureSpec); int measureSize = MeasureSpec.getSize(widthMeasureSpec); int width = 0; if (measureMode == MeasureSpec.EXACTLY) { width = measureSize; } else if (measureMode == MeasureSpec.AT_MOST) { int aWidth = 0; int bWidth = 0; int cWidth = 0; int dWidth = 0; for (int i = 0; i < getChildCount(); i++) { if (i == 0) { aWidth = getChildAt(i).getMeasuredWidth();//左邊View的寬度 } else if (i == 1) { bWidth = getChildAt(i).getMeasuredWidth(); } else if (i == 2) { cWidth = getChildAt(i).getMeasuredWidth(); } else if (i == 3) { dWidth = getChildAt(i).getMeasuredWidth(); } } //getPaddingTop:top離top的內邊距 getPaddingBottom:bottom離底的內邊距 width = Math.max(aWidth, bWidth) + Math.max(cWidth, dWidth) + getPaddingTop() + getPaddingBottom();//取到寬度的最大值 } return width; } private int measureWidth(int heightMeasureSpec) { int measureMode = MeasureSpec.getMode(heightMeasureSpec); int measureSize = MeasureSpec.getSize(heightMeasureSpec); int height = 0; if (measureMode == MeasureSpec.EXACTLY) { height = measureSize; } else if (measureMode == MeasureSpec.AT_MOST) { int aHeight = 0; int bHeight = 0; int cHeight = 0; int dHeight = 0; for (int i = 0; i < getChildCount(); i++) { if (i == 0) { aHeight = getChildAt(i).getMeasuredHeight(); } else if (i == 1) { bHeight = getChildAt(i).getMeasuredHeight(); } else if (i == 2) { cHeight = getChildAt(i).getMeasuredHeight(); } else if (i == 3) { dHeight = getChildAt(i).getMeasuredHeight(); } } height = Math.max(aHeight, bHeight) + Math.max(cHeight, dHeight) + getPaddingLeft() + getPaddingRight();//取到寬度的最大值 } return height; } @Override protected void onLayout(boolean changed, int l, int t, int r, int b) { int leftPadding = getPaddingLeft(); int rightPadding = getPaddingRight(); int topPadding = getPaddingTop(); int bottomPadding = getPaddingBottom(); for (int i = 0; i < getChildCount(); i++) { View childView = getChildAt(i);//某一個子View if (i == 0) { childView.layout(leftPadding, topPadding, childView.getMeasuredWidth() + leftPadding, childView.getMeasuredHeight() + topPadding); } else if (i == 1) { childView.layout(getMeasuredWidth() - childView.getMeasuredWidth() - rightPadding, topPadding, getMeasuredWidth() - rightPadding, childView.getMeasuredHeight() + topPadding); } else if (i == 2) { childView.layout(leftPadding, getMeasuredHeight() - childView.getMeasuredHeight() - bottomPadding, childView.getMeasuredWidth() + leftPadding, getMeasuredHeight() - bottomPadding); } else if (i == 3) { childView.layout(getMeasuredWidth() - childView.getMeasuredWidth() - rightPadding, getMeasuredHeight() - childView.getMeasuredHeight() - bottomPadding, getMeasuredWidth() - rightPadding, getMeasuredHeight() - bottomPadding); } } } }
activity_main:
<com.example.user.myapplication4.CustomViewGroup3 android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="#FFCCCCCC" android:paddingBottom="15dp" android:paddingLeft="20dp" android:paddingRight="30dp" android:paddingTop="10dp"> <TextView android:layout_width="100dp" android:layout_height="100dp" android:background="@android:color/holo_blue_bright" /> <TextView android:layout_width="100dp" android:layout_height="100dp" android:background="@android:color/holo_blue_dark" /> <TextView android:layout_width="100dp" android:layout_height="100dp" android:background="@android:color/holo_red_dark" /> <TextView android:layout_width="100dp" android:layout_height="100dp" android:background="@android:color/holo_green_light" /> </com.example.user.myapplication4.CustomViewGroup3>
結果展示: