attrs.xml文件
<?xml version="1.0" encoding="utf-8"?> <resources> <declare-styleable name="MyViewGroup04"> <attr name="orientation" format="integer"/> <attr name="ori" format="enum"> <enum name="horizontal" value="1"></enum> <enum name="vertical" value="0"></enum> <enum name="all" value="2"></enum> </attr> </declare-styleable> </resources>
MyViewGroup04
public class MyViewGroup04 extends ViewGroup { private int orientation = 0; private final int marginVertical = 20;//间距 private final int marginHorizontal = 20;//间距 int mTop = 20; int mLeft = 20; public MyViewGroup04(Context context) { this(context,null); } public MyViewGroup04(Context context, AttributeSet attrs) { this(context, attrs,0); } public MyViewGroup04(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); init(context, attrs, defStyleAttr); } private void init(Context context, AttributeSet attrs, int defStyleAttr) { TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.MyViewGroup04);//拿到属性文件 orientation = typedArray.getInt(R.styleable.MyViewGroup04_ori, 0);//得到里面的ID } @Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { super.onMeasure(widthMeasureSpec, heightMeasureSpec); int totalHeight = 0; int totalWidth = 0; for (int i = 0; i < getChildCount(); i++) { View childAt = getChildAt(i); measureChild(childAt,widthMeasureSpec, heightMeasureSpec); if (orientation == 0){ totalHeight += childAt.getMeasuredHeight()+marginVertical; totalWidth = childAt.getMeasuredWidth()+mLeft*2; }else if (orientation == 1){ totalWidth += childAt.getMeasuredWidth()+marginHorizontal; totalHeight = childAt.getMeasuredHeight()+mTop*2; }else if (orientation == 2){ totalHeight += childAt.getMeasuredHeight()+marginVertical; totalWidth += childAt.getMeasuredWidth()+marginHorizontal; } } int modeWidth = MeasureSpec.getMode(widthMeasureSpec); int sizeWidth = MeasureSpec.getSize(widthMeasureSpec); int modeHeight = MeasureSpec.getMode(heightMeasureSpec); int sizeHeight = MeasureSpec.getSize(heightMeasureSpec); if (orientation == 0){ //纵向 switch (modeHeight){ case MeasureSpec.AT_MOST: totalHeight += mTop; break; case MeasureSpec.EXACTLY: totalHeight = sizeHeight; break; } }else if (orientation == 1){ //横向 switch (modeWidth){ case MeasureSpec.AT_MOST: totalWidth += mLeft; break; case MeasureSpec.EXACTLY: totalWidth = sizeWidth; break; } }else if (orientation == 2){ switch (modeWidth){ case MeasureSpec.AT_MOST: totalWidth += mLeft; totalHeight += mTop * 2; break; case MeasureSpec.EXACTLY: totalWidth = sizeWidth; break; } //totalHeight += mTop; } setMeasuredDimension(totalWidth,totalHeight); } @Override protected void onLayout(boolean changed, int l, int t, int r, int b) { int childCount = getChildCount(); int mVertical = marginVertical; int top = mTop; int mhorizontal = marginHorizontal; int left = mLeft; for (int i = 0; i < childCount; i++) { View childAt = getChildAt(i); childAt.layout(left,top,childAt.getMeasuredWidth()+left,childAt.getMeasuredHeight()+top); if (orientation == 0){ top += childAt.getMeasuredHeight()+mVertical; }else if (orientation == 1){ left += childAt.getMeasuredWidth()+mhorizontal; }else if (orientation == 2){ top += childAt.getMeasuredHeight()+mVertical; left += childAt.getMeasuredWidth()+mhorizontal; } } } }