网友通过本文主要向大家介绍了android viewgroup,viewgroup,自定义viewgroup,viewgroup是什么,viewgroup用法等相关知识,希望对您有所帮助,也希望大家支持linkedu.com www.linkedu.com
Android--根据子控件的大小自动换行的ViewGroup,android--viewgroup
1、自定义ViewGroup
1 /** 2 * Created by Administrator on 2016/2/26. 3 * 4 * --------自动换行的ViewGroup----------- 5 */ 6 public class LineWrapLayout extends ViewGroup { 7 private static final boolean DEBUG = true; 8 private static final String TAG = "AutoLineFeedLayout"; 9 10 /** 11 * 左间距 12 */ 13 private int paddingLeft = 10; 14 /** 15 * 右间距 16 */ 17 private int paddingRight = 10; 18 /** 19 * 20 */ 21 private int paddingTop = 10; 22 /** 23 * 24 */ 25 private int paddingBottom = 10; 26 27 /** 28 * 水平方向间距 29 */ 30 private int horizontalSpace = 10; 31 /** 32 * 行间距 33 */ 34 private int verticalSpace = 5; 35 36 37 private List<Integer> listX; 38 private List<Integer> listY; 39 40 public LineWrapLayout(Context context) { 41 super(context); 42 43 } 44 public LineWrapLayout(Context context, AttributeSet attrs) { 45 super(context, attrs); 46 init(attrs); 47 } 48 49 public LineWrapLayout(Context context, AttributeSet attrs, int defStyle) { 50 super(context, attrs, defStyle); 51 init(attrs); 52 } 53 54 55 @Override 56 protected void onLayout(boolean changed, int l, int t, int r, int b) { 57 if(DEBUG) Log.d(TAG, "--- onLayout changed :" + changed + " l :" + l + ",t :" + t + ",r :" + r + ",b :" + b); 58 int count = getChildCount(); 59 int width = getWidth(); 60 Log.i(TAG, "宽度 :"+width); 61 62 63 int startOffsetX = paddingLeft;// 横坐标开始 64 int startOffsety = 0;//纵坐标开始 65 int rowCount = 1; 66 67 int preEndOffsetX = startOffsetX; 68 69 for (int i = 0; i < count; i++) { 70 final View childView = getChildAt(i); 71 72 int w = childView.getMeasuredWidth(); 73 int h = childView.getMeasuredHeight(); 74 75 int x = listX.get(i); 76 int y = listY.get(i); 77 78 // 布局子控件 79 childView.layout(x, y, x + w, y + h); 80 } 81 } 82 @Override 83 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { 84 if(DEBUG) Log.v(TAG, "--- onMeasure()"); 85 86 int count = getChildCount(); 87 int width = measureWidth(widthMeasureSpec); 88 Log.i(TAG, "宽度 :"+width); 89 90 91 int startOffsetX = paddingLeft;// 横坐标开始 92 int startOffsety = 0+paddingTop;//纵坐标开始 93 int rowCount = 1; 94 95 int preEndOffsetX = startOffsetX; 96 97 listX.clear(); 98 listY.clear(); 99 for (int i = 0; i < count; i++) { 100 Log.v(TAG, "----"); 101 final View childView = getChildAt(i); 102 // 设置子空间Child的宽高 103 childView.measure(0,0); 104 /* 获取子控件Child的宽高 */ 105 int childWidth = childView.getMeasuredWidth(); 106 int childHeight = childView.getMeasuredHeight(); 107 Log.v(TAG, "childWidth :"+childWidth+" childHeight :"+childHeight); 108 preEndOffsetX = startOffsetX + childWidth /*+ CHILD_MARGIN*/; 109 //TODO [yaojian]margin属性? 110 if (preEndOffsetX > width - paddingRight ) { 111 if (startOffsetX > paddingLeft) { 112 /* 换行 */ 113 startOffsetX = paddingLeft; 114 startOffsety += childHeight+verticalSpace; 115 rowCount++; 116 } 117 } 118 Log.d(TAG, "measure child :"+startOffsetX+", "+startOffsety+", "+preEndOffsetX+", "+(startOffsety+childHeight)); 119 listX.add(startOffsetX); 120 listY.