1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169
| public class AutoLayoutHelper { private static final String TAG = "AutoLayoutHelper";
private final ViewGroup mHost;
private static final int[] LL = new int[]{ android.R.attr.textSize, android.R.attr.padding, android.R.attr.paddingLeft, android.R.attr.paddingTop, android.R.attr.paddingRight, android.R.attr.paddingBottom, android.R.attr.layout_width, android.R.attr.layout_height, android.R.attr.layout_margin, android.R.attr.layout_marginLeft, android.R.attr.layout_marginTop, android.R.attr.layout_marginRight, android.R.attr.layout_marginBottom, android.R.attr.maxWidth, android.R.attr.maxHeight, android.R.attr.minWidth, android.R.attr.minHeight, android.R.attr.layout_marginStart, android.R.attr.layout_marginEnd, android.R.attr.paddingStart, android.R.attr.paddingEnd, };
private static final int INDEX_TEXT_SIZE = 0; private static final int INDEX_PADDING = 1; private static final int INDEX_PADDING_LEFT = 2; private static final int INDEX_PADDING_TOP = 3; private static final int INDEX_PADDING_RIGHT = 4; private static final int INDEX_PADDING_BOTTOM = 5; private static final int INDEX_WIDTH = 6; private static final int INDEX_HEIGHT = 7; private static final int INDEX_MARGIN = 8; private static final int INDEX_MARGIN_LEFT = 9; private static final int INDEX_MARGIN_TOP = 10; private static final int INDEX_MARGIN_RIGHT = 11; private static final int INDEX_MARGIN_BOTTOM = 12; private static final int INDEX_MAX_WIDTH = 13; private static final int INDEX_MAX_HEIGHT = 14; private static final int INDEX_MIN_WIDTH = 15; private static final int INDEX_MIN_HEIGHT = 16; private static final int INDEX_MARGIN_START = 17; private static final int INDEX_MARGIN_END = 18; private static final int INDEX_PADDING_START = 19; private static final int INDEX_PADDING_END = 20;
public AutoLayoutHelper(ViewGroup host) { mHost = host;
initAutoLayoutConfig(host); }
private void initAutoLayoutConfig(ViewGroup host) { if (host != null) { AutoLayoutConifg autoLayoutConifg = AutoLayoutConifg.getInstance(); autoLayoutConifg.init(host.getContext()); } else { Log.e(TAG, "host == null"); } }
public void adjustChildren() { AutoLayoutConifg.getInstance().checkParams(); for (int i = 0, n = mHost.getChildCount(); i < n; i++) { View view = mHost.getChildAt(i); ViewGroup.LayoutParams params = view.getLayoutParams(); if (params instanceof AutoLayoutParams) { AutoLayoutInfo info = ((AutoLayoutParams) params).getAutoLayoutInfo(); if (info != null) { info.fillAttrs(view); } } } }
public static AutoLayoutInfo getAutoLayoutInfo(Context context, AttributeSet attrs) { TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.AutoLayout_Layout); int baseWidth = a.getInt(R.styleable.AutoLayout_Layout_layout_auto_basewidth, 0); int baseHeight = a.getInt(R.styleable.AutoLayout_Layout_layout_auto_baseheight, 0); a.recycle();
AutoLayoutInfo info = new AutoLayoutInfo(); TypedArray array = context.obtainStyledAttributes(attrs, LL); for (int i = 0, n = array.getIndexCount(); i < n; i++) { int index = array.getIndex(i);
if (!DimenUtils.isPxVal(array.peekValue(index))) { continue; }
int pxVal; try { pxVal = array.getDimensionPixelOffset(index, 0); } catch (Exception e) { e.printStackTrace(); continue; } switch (index) { case INDEX_TEXT_SIZE: info.addAttr(new TextSizeAttr(pxVal, baseWidth, baseHeight)); break; case INDEX_PADDING: info.addAttr(new PaddingAttr(pxVal, baseWidth, baseHeight)); break; case INDEX_PADDING_LEFT: case INDEX_PADDING_START: info.addAttr(new PaddingLeftAttr(pxVal, baseWidth, baseHeight)); break; case INDEX_PADDING_TOP: info.addAttr(new PaddingTopAttr(pxVal, baseWidth, baseHeight)); break; case INDEX_PADDING_RIGHT: case INDEX_PADDING_END: info.addAttr(new PaddingRightAttr(pxVal, baseWidth, baseHeight)); break; case INDEX_PADDING_BOTTOM: info.addAttr(new PaddingBottomAttr(pxVal, baseWidth, baseHeight)); break; case INDEX_WIDTH: info.addAttr(new WidthAttr(pxVal, baseWidth, baseHeight)); break; case INDEX_HEIGHT: info.addAttr(new HeightAttr(pxVal, baseWidth, baseHeight)); break; case INDEX_MARGIN: info.addAttr(new MarginAttr(pxVal, baseWidth, baseHeight)); break; case INDEX_MARGIN_LEFT: case INDEX_MARGIN_START: info.addAttr(new MarginLeftAttr(pxVal, baseWidth, baseHeight)); break; case INDEX_MARGIN_TOP: info.addAttr(new MarginTopAttr(pxVal, baseWidth, baseHeight)); break; case INDEX_MARGIN_RIGHT: case INDEX_MARGIN_END: info.addAttr(new MarginRightAttr(pxVal, baseWidth, baseHeight)); break; case INDEX_MARGIN_BOTTOM: info.addAttr(new MarginBottomAttr(pxVal, baseWidth, baseHeight)); break; case INDEX_MAX_WIDTH: info.addAttr(new MaxWidthAttr(pxVal, baseWidth, baseHeight)); break; case INDEX_MAX_HEIGHT: info.addAttr(new MaxHeightAttr(pxVal, baseWidth, baseHeight)); break; case INDEX_MIN_WIDTH: info.addAttr(new MinWidthAttr(pxVal, baseWidth, baseHeight)); break; case INDEX_MIN_HEIGHT: info.addAttr(new MinHeightAttr(pxVal, baseWidth, baseHeight)); break; } } array.recycle(); L.e("getAutoLayoutInfo " + info.toString()); return info; }
public interface AutoLayoutParams { AutoLayoutInfo getAutoLayoutInfo(); } }
|