7">
117
|
|
if (info.padding != 0)
|
|
118
|
|
{
|
|
119
|
|
int vertical = (int) (info.padding * 1.0f / mDesignHeight * mAvailaleHegiht);
|
|
120
|
|
int horizon = (int) (info.padding * 1.0f / mDesignWidth * mAvailableWidth);
|
|
121
|
|
left = right = horizon;
|
|
122
|
|
top = bottom = vertical;
|
|
123
|
|
}
|
|
124
|
|
if (info.paddingLeft != 0)
|
|
125
|
|
{
|
|
126
|
|
left = (int) (info.paddingLeft * 1.0f / mDesignWidth * mAvailableWidth);
|
|
127
|
|
}
|
|
128
|
|
|
|
129
|
|
if (info.paddingTop != 0)
|
|
130
|
|
{
|
|
131
|
|
top = (int) (info.paddingTop * 1.0f / mDesignHeight * mAvailaleHegiht);
|
|
132
|
|
}
|
|
133
|
|
|
|
134
|
|
if (info.paddingRight != 0)
|
|
135
|
|
{
|
|
136
|
|
right = (int) (info.paddingRight * 1.0f / mDesignWidth * mAvailableWidth);
|
|
137
|
|
}
|
|
138
|
|
|
|
139
|
|
if (info.paddingBottom != 0)
|
|
140
|
|
{
|
|
141
|
|
bottom = (int) (info.paddingBottom * 1.0f / mDesignHeight * mAvailaleHegiht);
|
|
142
|
|
}
|
|
143
|
|
|
|
144
|
|
view.setPadding(left, top, right, bottom);
|
|
145
|
|
}
|
|
146
|
|
|
|
147
|
|
private int getAvailableWidth()
|
|
148
|
|
{
|
|
149
|
|
return AutoLayout.getInstance().getAvailableWidth();
|
|
150
|
|
}
|
|
151
|
|
|
|
152
|
|
private int getAvailaleHegiht()
|
|
153
|
|
{
|
|
154
|
|
return AutoLayout.getInstance().getAvailaleHeight();
|
|
155
|
|
|
|
156
|
|
}
|
|
157
|
|
|
|
158
|
|
private int getDesignWidth()
|
|
159
|
|
{
|
|
160
|
|
return AutoLayout.getInstance().getDesignWidth();
|
|
161
|
|
}
|
|
162
|
|
|
|
163
|
|
private int getDesignHeight()
|
|
164
|
|
{
|
|
165
|
|
return AutoLayout.getInstance().getDesignHeight();
|
|
166
|
|
}
|
|
167
|
|
|
|
168
|
|
|
|
169
|
|
private void supportTextSize(View view, AutoLayoutInfo info)
|
|
170
|
|
{
|
|
171
|
|
if (!(view instanceof TextView)) return;
|
|
172
|
|
if (info.textSize == 0) return;
|
|
173
|
|
|
|
174
|
|
boolean textSizeBaseWidth = info.textSizeBaseWidth;
|
|
175
|
|
float textSize;
|
|
176
|
|
if (textSizeBaseWidth)
|
|
177
|
|
{
|
|
178
|
|
textSize = info.textSize * 1.0f / getDesignWidth() * getAvailableWidth();
|
|
179
|
|
} else
|
|
180
|
|
{
|
|
181
|
|
textSize = info.textSize * 1.0f / getDesignHeight() * getAvailaleHegiht();
|
|
182
|
|
}
|
|
183
|
|
//textSize = textSize / 1.34f;
|
|
184
|
|
|
|
185
|
|
((TextView) view).setIncludeFontPadding(false);
|
|
186
|
|
((TextView) view).setTextSize(TypedValue.COMPLEX_UNIT_PX, textSize);
|
|
187
|
|
}
|
|
188
|
|
|
|
189
|
|
|
|
190
|
|
public static AutoLayoutInfo getAutoLayoutInfo(Context context,
|
|
191
|
|
AttributeSet attrs)
|
|
192
|
|
{
|
|
193
|
|
AutoLayoutInfo info = new AutoLayoutInfo();
|
|
194
|
|
|
|
195
|
|
boolean isTextSizeBaseWidth = isTextSizeBaseWidth(context, attrs);
|
|
196
|
|
info.textSizeBaseWidth = isTextSizeBaseWidth;
|
|
197
|
|
|
|
198
|
|
TypedArray array = context.obtainStyledAttributes(attrs, LL);
|
|
199
|
|
|
|
200
|
|
int n = array.getIndexCount();
|
|
201
|
|
|
|
202
|
|
|
|
203
|
|
for (int i = 0; i < n; i++)
|
|
204
|
|
{
|
|
205
|
|
int index = array.getIndex(i);
|
|
206
|
|
String val = array.getString(index);
|
|
207
|
|
L.e(val);
|
|
208
|
|
if (val.equals(VAL_WRAP_CONTENT) || val.equals(VAL_MATCH_PARENT))
|
|
209
|
|
{
|
|
210
|
|
continue;
|
|
211
|
|
}
|
|
212
|
|
if (!val.endsWith("px"))
|
|
213
|
|
{
|
|
214
|
|
continue;
|
|
215
|
|
}
|
|
216
|
|
switch (index)
|
|
217
|
|
{
|
|
218
|
|
case INDEX_TEXT_SIZE:
|
|
219
|
|
info.textSize = array.getDimensionPixelOffset(index, 0);
|
|
220
|
|
break;
|
|
221
|
|
case INDEX_PADDING:
|
|
222
|
|
info.padding = array.getDimensionPixelOffset(index, 0);
|
|
223
|
|
break;
|
|
224
|
|
case INDEX_PADDING_LEFT:
|
|
225
|
|
info.paddingLeft = array.getDimensionPixelOffset(index, 0);
|
|
226
|
|
break;
|
|
227
|
|
case INDEX_PADDING_TOP:
|
|
228
|
|
info.paddingTop = array.getDimensionPixelOffset(index, 0);
|
|
229
|
|
break;
|
|
230
|
|
case INDEX_PADDING_RIGHT:
|
|
231
|
|
info.paddingRight = array.getDimensionPixelOffset(index, 0);
|
|
232
|
|
break;
|
|
233
|
|
case INDEX_PADDING_BOTTOM:
|
|
234
|
|
info.paddingBottom = array.getDimensionPixelOffset(index, 0);
|
|
235
|
|
break;
|
|
236
|
|
case INDEX_WIDTH:
|
|
237
|
|
info.widthPx = array.getDimensionPixelSize(index, 0);
|
|
238
|
|
break;
|
|
239
|
|
case INDEX_HEIGHT:
|
|
240
|
|
info.heightPx = array.getDimensionPixelSize(index, 0);
|
|
241
|
|
break;
|
|
242
|
|
case INDEX_MARGIN:
|
|
243
|
|
info.margin = array.getDimensionPixelOffset(index, 0);
|
|
244
|
|
break;
|
|
245
|
|
case INDEX_MARGIN_LEFT:
|
|
246
|
|
info.marginLeft = array.getDimensionPixelOffset(index, 0);
|
|
247
|
|
break;
|
|
248
|
|
case INDEX_MARGIN_TOP:
|
|
249
|
|
info.marginTop = array.getDimensionPixelOffset(index, 0);
|
|
250
|
|
break;
|
|
251
|
|
case INDEX_MARGIN_RIGHT:
|
|
252
|
|
info.marginRight = array.getDimensionPixelOffset(index, 0);
|
|
253
|
|
break;
|
|
254
|
|
case INDEX_MARGIN_BOTTOM:
|
|
255
|
|
info.marginBottom = array.getDimensionPixelOffset(index, 0);
|
|
256
|
|
break;
|
|
257
|
|
}
|
|
258
|
|
}
|
|
259
|
|
array.recycle();
|
|
260
|
|
L.e(info.toString());
|
|
261
|
|
return info;
|
|
262
|
|
}
|
|
263
|
|
|
|
264
|
|
private static boolean isTextSizeBaseWidth(Context context, AttributeSet attrs)
|
|
265
|
|
{
|
|
266
|
|
TypedArray array = context.obtainStyledAttributes(attrs, R.styleable.AutoLayout_Layout);
|
|
267
|
|
boolean res = array.getBoolean(R.styleable.AutoLayout_Layout_layout_auto_textSizeBaseWidth, false);
|
|
268
|
|
array.recycle();
|
|
269
|
|
return res;
|
|
270
|
|
}
|
|
271
|
|
|
|
272
|
|
|
|
273
|
|
public static class AutoLayoutInfo
|
|
274
|
|
{
|
|
275
|
|
public int widthPx;
|
|
276
|
|
public int heightPx;
|
|
277
|
|
|
|
278
|
|
private int margin;
|
|
279
|
|
private int marginLeft;
|
|
280
|
|
private int marginRight;
|
|
281
|
|
private int marginTop;
|
|
282
|
|
private int marginBottom;
|
|
283
|
|
|
|
284
|
|
private int textSize;
|
|
285
|
|
|
|
286
|
|
private int padding;
|
|
287
|
|
private int paddingLeft;
|
|
288
|
|
private int paddingRight;
|
|
289
|
|
private int paddingTop;
|
|
290
|
|
private int paddingBottom;
|
|
291
|
|
|
|
292
|
|
private boolean textSizeBaseWidth;
|
|
293
|
|
|
|
294
|
|
|
|
295
|
|
public void fillLayoutParams(ViewGroup.LayoutParams params, int avaWidth,
|
|
296
|
|
int avaHeight, int designWidth, int designHeight)
|
|
297
|
|
{
|
|
298
|
|
|
|
299
|
|
|
|
300
|
|
if (widthPx != 0)
|
|
301
|
|
{
|
|
302
|
|
params.width = (int) (widthPx * 1.0f / designWidth * avaWidth);
|
|
303
|
|
}
|
|
304
|
|
if (heightPx != 0)
|
|
305
|
|
{
|
|
306
|
|
params.height = (int) (heightPx * 1.0f / designHeight * avaHeight);
|
|
307
|
|
}
|
|
308
|
|
}
|
|
309
|
|
|
|
310
|
|
@Override
|
|
311
|
|
public String toString()
|
|
312
|
|
{
|
|
313
|
|
return "AutoLayoutInfo{" +
|
|
314
|
|
"widthPx=" + widthPx +
|
|
315
|
|
", heightPx=" + heightPx +
|
|
316
|
|
", margin=" + margin +
|
|
317
|
|
", marginLeft=" + marginLeft +
|
|
318
|
|
", marginRight=" + marginRight +
|
|
319
|
|
", marginTop=" + marginTop +
|
|
320
|
|
", marginBottom=" + marginBottom +
|
|
321
|
|
", textSize=" + textSize +
|
|
322
|
|
", padding=" + padding +
|
|
323
|
|
", paddingLeft=" + paddingLeft +
|
|
324
|
|
", paddingRight=" + paddingRight +
|
|
325
|
|
", paddingTop=" + paddingTop +
|
|
326
|
|
", paddingBottom=" + paddingBottom +
|
|
327
|
|
'}';
|
|
328
|
|
}
|
|
329
|
|
|
|
330
|
|
public void fillMarginLayoutParams(ViewGroup.MarginLayoutParams params, int avaWidth,
|
|
331
|
|
int avaHeight, int designWidth, int designHeight)
|
|
332
|
|
{
|
|
333
|
|
|
|
334
|
|
if (margin != 0)
|
|
335
|
|
{
|
|
336
|
|
int marginSize = (int) (margin * 1.0f / designHeight * avaHeight);
|
|
337
|
|
params.leftMargin = params.topMargin = params.rightMargin = params.bottomMargin = marginSize;
|
|
338
|
|
}
|
|
339
|
|
if (marginLeft != 0)
|
|
340
|
|
{
|
|
341
|
|
params.leftMargin = (int) (marginLeft * 1.0f / designWidth * avaWidth);
|
|
342
|
|
}
|
|
343
|
|
if (marginTop != 0)
|
|
344
|
|
{
|
|
345
|
|
params.topMargin = (int) (marginTop * 1.0f / designHeight * avaHeight);
|
|
346
|
|
}
|
|
347
|
|
if (marginRight != 0)
|
|
348
|
|
{
|
|
349
|
|
params.rightMargin = (int) (marginRight * 1.0f / designWidth * avaWidth);
|
|
350
|
|
}
|
|
351
|
|
if (marginBottom != 0)
|
|
352
|
|
{
|
|
353
|
|
params.bottomMargin = (int) (marginBottom * 1.0f / designHeight * avaHeight);
|
|
354
|
|
}
|
|
355
|
|
fillLayoutParams(params, avaWidth, avaHeight, designWidth, designHeight);
|
|
356
|
|
}
|
|
357
|
|
|
|
358
|
|
|
|
359
|
|
}
|
|
360
|
|
|
|
361
|
|
public interface AutoLayoutParams
|
|
362
|
|
{
|
|
363
|
|
AutoLayoutInfo getPercentLayoutInfo();
|
|
364
|
|
}
|
|
365
|
|
}
|
|
|
@ -1,82 +0,0 @@
|
|
1
|
|
package com.zhy.autolayout;
|
|
2
|
|
|
|
3
|
|
import android.content.Context;
|
|
4
|
|
import android.util.AttributeSet;
|
|
5
|
|
import android.view.ViewGroup;
|
|
6
|
|
import android.widget.LinearLayout;
|
|
7
|
|
|
|
8
|
|
/**
|
|
9
|
|
* Created by zhy on 15/6/30.
|
|
10
|
|
*/
|
|
11
|
|
public class AutoLinearLayout extends LinearLayout
|
|
12
|
|
{
|
|
13
|
|
|
|
14
|
|
private AutoLayoutHelper mPercentLayoutHelper;
|
|
15
|
|
|
|
16
|
|
public AutoLinearLayout(Context context, AttributeSet attrs)
|
|
17
|
|
{
|
|
18
|
|
super(context, attrs);
|
|
19
|
|
mPercentLayoutHelper = new AutoLayoutHelper(this);
|
|
20
|
|
}
|
|
21
|
|
|
|
22
|
|
|
|
23
|
|
@Override
|
|
24
|
|
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec)
|
|
25
|
|
{
|
|
26
|
|
if (!isInEditMode())
|
|
27
|
|
mPercentLayoutHelper.adjustChildren();
|
|
28
|
|
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
|
|
29
|
|
}
|
|
30
|
|
|
|
31
|
|
|
|
32
|
|
@Override
|
|
33
|
|
protected void onLayout(boolean changed, int l, int t, int r, int b)
|
|
34
|
|
{
|
|
35
|
|
super.onLayout(changed, l, t, r, b);
|
|
36
|
|
}
|
|
37
|
|
|
|
38
|
|
|
|
39
|
|
@Override
|
|
40
|
|
public LayoutParams generateLayoutParams(AttributeSet attrs)
|
|
41
|
|
{
|
|
42
|
|
return new AutoLinearLayout.LayoutParams(getContext(), attrs);
|
|
43
|
|
}
|
|
44
|
|
|
|
45
|
|
|
|
46
|
|
public static class LayoutParams extends LinearLayout.LayoutParams
|
|
47
|
|
implements AutoLayoutHelper.AutoLayoutParams
|
|
48
|
|
{
|
|
49
|
|
private AutoLayoutHelper.AutoLayoutInfo mAutoLayoutInfo;
|
|
50
|
|
|
|
51
|
|
public LayoutParams(Context c, AttributeSet attrs)
|
|
52
|
|
{
|
|
53
|
|
super(c, attrs);
|
|
54
|
|
mAutoLayoutInfo = AutoLayoutHelper.getAutoLayoutInfo(c, attrs);
|
|
55
|
|
}
|
|
56
|
|
|
|
57
|
|
@Override
|
|
58
|
|
public AutoLayoutHelper.AutoLayoutInfo getPercentLayoutInfo()
|
|
59
|
|
{
|
|
60
|
|
return mAutoLayoutInfo;
|
|
61
|
|
}
|
|
62
|
|
|
|
63
|
|
|
|
64
|
|
public LayoutParams(int width, int height)
|
|
65
|
|
{
|
|
66
|
|
super(width, height);
|
|
67
|
|
}
|
|
68
|
|
|
|
69
|
|
|
|
70
|
|
public LayoutParams(ViewGroup.LayoutParams source)
|
|
71
|
|
{
|
|
72
|
|
super(source);
|
|
73
|
|
}
|
|
74
|
|
|
|
75
|
|
public LayoutParams(MarginLayoutParams source)
|
|
76
|
|
{
|
|
77
|
|
super(source);
|
|
78
|
|
}
|
|
79
|
|
|
|
80
|
|
}
|
|
81
|
|
|
|
82
|
|
}
|
|
|
@ -1,100 +0,0 @@
|
|
1
|
|
/*
|
|
2
|
|
* Copyright (C) 2015 The Android Open Source Project
|
|
3
|
|
*
|
|
4
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
5
|
|
* you may not use this file except in compliance with the License.
|
|
6
|
|
* You may obtain a copy of the License at
|
|
7
|
|
*
|
|
8
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
9
|
|
*
|
|
10
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
11
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
12
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
13
|
|
* See the License for the specific language governing permissions and
|
|
14
|
|
* limitations under the License.
|
|
15
|
|
*/
|
|
16
|
|
|
|
17
|
|
package com.zhy.autolayout;
|
|
18
|
|
|
|
19
|
|
import android.content.Context;
|
|
20
|
|
import android.content.res.TypedArray;
|
|
21
|
|
import android.util.AttributeSet;
|
|
22
|
|
import android.view.ViewGroup;
|
|
23
|
|
import android.widget.RelativeLayout;
|
|
24
|
|
|
|
25
|
|
public class AutoRelativeLayout extends RelativeLayout
|
|
26
|
|
{
|
|
27
|
|
private final AutoLayoutHelper mHelper = new AutoLayoutHelper(this);
|
|
28
|
|
|
|
29
|
|
public AutoRelativeLayout(Context context)
|
|
30
|
|
{
|
|
31
|
|
super(context);
|
|
32
|
|
}
|
|
33
|
|
|
|
34
|
|
public AutoRelativeLayout(Context context, AttributeSet attrs)
|
|
35
|
|
{
|
|
36
|
|
super(context, attrs);
|
|
37
|
|
}
|
|
38
|
|
|
|
39
|
|
public AutoRelativeLayout(Context context, AttributeSet attrs, int defStyle)
|
|
40
|
|
{
|
|
41
|
|
super(context, attrs, defStyle);
|
|
42
|
|
}
|
|
43
|
|
|
|
44
|
|
@Override
|
|
45
|
|
public LayoutParams generateLayoutParams(AttributeSet attrs)
|
|
46
|
|
{
|
|
47
|
|
return new LayoutParams(getContext(), attrs);
|
|
48
|
|
}
|
|
49
|
|
|
|
50
|
|
|
|
51
|
|
|
|
52
|
|
@Override
|
|
53
|
|
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec)
|
|
54
|
|
{
|
|
55
|
|
if (!isInEditMode())
|
|
56
|
|
mHelper.adjustChildren();
|
|
57
|
|
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
|
|
58
|
|
}
|
|
59
|
|
|
|
60
|
|
@Override
|
|
61
|
|
protected void onLayout(boolean changed, int left, int top, int right, int bottom)
|
|
62
|
|
{
|
|
63
|
|
super.onLayout(changed, left, top, right, bottom);
|
|
64
|
|
}
|
|
65
|
|
|
|
66
|
|
public static class LayoutParams extends RelativeLayout.LayoutParams
|
|
67
|
|
implements AutoLayoutHelper.AutoLayoutParams
|
|
68
|
|
{
|
|
69
|
|
private AutoLayoutHelper.AutoLayoutInfo mAutoLayoutInfo;
|
|
70
|
|
|
|
71
|
|
public LayoutParams(Context c, AttributeSet attrs)
|
|
72
|
|
{
|
|
73
|
|
super(c, attrs);
|
|
74
|
|
mAutoLayoutInfo = AutoLayoutHelper.getAutoLayoutInfo(c, attrs);
|
|
75
|
|
}
|
|
76
|
|
|
|
77
|
|
public LayoutParams(int width, int height)
|
|
78
|
|
{
|
|
79
|
|
super(width, height);
|
|
80
|
|
}
|
|
81
|
|
|
|
82
|
|
public LayoutParams(ViewGroup.LayoutParams source)
|
|
83
|
|
{
|
|
84
|
|
super(source);
|
|
85
|
|
}
|
|
86
|
|
|
|
87
|
|
public LayoutParams(MarginLayoutParams source)
|
|
88
|
|
{
|
|
89
|
|
super(source);
|
|
90
|
|
}
|
|
91
|
|
|
|
92
|
|
@Override
|
|
93
|
|
public AutoLayoutHelper.AutoLayoutInfo getPercentLayoutInfo()
|
|
94
|
|
{
|
|
95
|
|
return mAutoLayoutInfo;
|
|
96
|
|
}
|
|
97
|
|
|
|
98
|
|
|
|
99
|
|
}
|
|
100
|
|
}
|
|
|
@ -1,22 +0,0 @@
|
|
1
|
|
package com.zhy.autolayout;
|
|
2
|
|
|
|
3
|
|
import android.util.Log;
|
|
4
|
|
|
|
5
|
|
/**
|
|
6
|
|
* Created by zhy on 15/11/18.
|
|
7
|
|
*/
|
|
8
|
|
public class L
|
|
9
|
|
{
|
|
10
|
|
public static boolean debug = false;
|
|
11
|
|
private static final String TAG = "AUTO_LAYOUT";
|
|
12
|
|
|
|
13
|
|
public static void e(String msg)
|
|
14
|
|
{
|
|
15
|
|
if (debug)
|
|
16
|
|
{
|
|
17
|
|
Log.e(TAG, msg);
|
|
18
|
|
}
|
|
19
|
|
}
|
|
20
|
|
|
|
21
|
|
|
|
22
|
|
}
|
|
|
@ -1,6 +0,0 @@
|
|
1
|
|
<?xml version="1.0" encoding="utf-8"?>
|
|
2
|
|
<resources>
|
|
3
|
|
<declare-styleable name="AutoLayout_Layout">
|
|
4
|
|
<attr name="layout_auto_textSizeBaseWidth" format="boolean"/>
|
|
5
|
|
</declare-styleable>
|
|
6
|
|
</resources>
|
|
|
@ -1,3 +0,0 @@
|
|
1
|
|
<resources>
|
|
2
|
|
<string name="app_name">autolayout</string>
|
|
3
|
|
</resources>
|