Skip to content

Commit b39c830

Browse files
committed
fix bug: can not setting parent attrs
1 parent cb90c5b commit b39c830

File tree

5 files changed

+96
-47
lines changed

5 files changed

+96
-47
lines changed

Library/src/main/java/cn/gavinliu/android/lib/scale/ScaleFrameLayout.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@ public ScaleFrameLayout.LayoutParams generateLayoutParams(AttributeSet attrs) {
3535
@Override
3636
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
3737
this.mHelper.adjustChildren(widthMeasureSpec, heightMeasureSpec);
38+
this.mHelper.adjustSelf(widthMeasureSpec, heightMeasureSpec);
39+
3840
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
3941
if (this.mHelper.handleMeasuredStateTooSmall()) {
4042
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
@@ -72,6 +74,7 @@ public ScaleLayoutHelper.ScaleLayoutInfo getScaleLayoutInfo() {
7274
return this.mPercentLayoutInfo;
7375
}
7476

77+
@Override
7578
protected void setBaseAttributes(TypedArray a, int widthAttr, int heightAttr) {
7679
ScaleLayoutHelper.fetchWidthAndHeight(this, a, widthAttr, heightAttr);
7780
}

Library/src/main/java/cn/gavinliu/android/lib/scale/ScaleRelativeLayout.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@ public ScaleRelativeLayout.LayoutParams generateLayoutParams(AttributeSet attrs)
3535
@Override
3636
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
3737
this.mHelper.adjustChildren(widthMeasureSpec, heightMeasureSpec);
38+
this.mHelper.adjustSelf(widthMeasureSpec, heightMeasureSpec);
39+
3840
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
3941
if (this.mHelper.handleMeasuredStateTooSmall()) {
4042
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
@@ -72,6 +74,7 @@ public ScaleLayoutHelper.ScaleLayoutInfo getScaleLayoutInfo() {
7274
return this.mPercentLayoutInfo;
7375
}
7476

77+
@Override
7578
protected void setBaseAttributes(TypedArray a, int widthAttr, int heightAttr) {
7679
ScaleLayoutHelper.fetchWidthAndHeight(this, a, widthAttr, heightAttr);
7780
}

Library/src/main/java/cn/gavinliu/android/lib/scale/helper/ScaleLayoutHelper.java

Lines changed: 61 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -19,39 +19,19 @@ public class ScaleLayoutHelper {
1919
private static final String TAG = "ScaleLayoutHelper";
2020
private final ViewGroup mHost;
2121

22-
private float designWidth;
23-
private float designHeight;
24-
private float designDensity;
22+
private ScaleLayoutInfo mHostLayoutInfo;
2523

2624
public interface ScaleLayoutParams {
2725
ScaleLayoutInfo getScaleLayoutInfo();
2826
}
2927

30-
private ScaleLayoutHelper(ViewGroup mHost, float designWidth, float designHeight, float designDensity) {
31-
this.mHost = mHost;
32-
this.designWidth = designWidth;
33-
this.designHeight = designHeight;
34-
this.designDensity = designDensity;
28+
public ScaleLayoutHelper(ViewGroup host, ScaleLayoutInfo layoutInfo) {
29+
this.mHost = host;
30+
this.mHostLayoutInfo = layoutInfo;
3531
}
3632

3733
public static ScaleLayoutHelper create(ViewGroup view, AttributeSet attrs) {
38-
TypedArray array = view.getContext().obtainStyledAttributes(attrs, R.styleable.ScaleLayout);
39-
int width = 0, height = 0, density = 0;
40-
41-
int value = array.getDimensionPixelSize(R.styleable.ScaleLayout_layout_design_width, 0);
42-
if (value != 0) {
43-
width = value;
44-
}
45-
value = array.getDimensionPixelSize(R.styleable.ScaleLayout_layout_design_height, 0);
46-
if (value != 0) {
47-
height = value;
48-
}
49-
value = array.getInteger(R.styleable.ScaleLayout_layout_design_density, 0);
50-
if (value != 0) {
51-
density = value;
52-
}
53-
array.recycle();
54-
return new ScaleLayoutHelper(view, width, height, density);
34+
return new ScaleLayoutHelper(view, getScaleLayoutInfo(view.getContext(), attrs));
5535
}
5636

5737
public void adjustChildren(int widthMeasureSpec, int heightMeasureSpec) {
@@ -66,8 +46,8 @@ public void adjustChildren(int widthMeasureSpec, int heightMeasureSpec) {
6646
if (params instanceof ScaleLayoutParams) {
6747
ScaleLayoutInfo info = ((ScaleLayoutParams) params).getScaleLayoutInfo();
6848
Log.d(TAG, "using " + info);
69-
if (info != null) {
70-
info.setDesignSize(designWidth, designHeight, designDensity);
49+
if (info != null && mHostLayoutInfo != null) {
50+
info.setDesignSize(mHostLayoutInfo.designWidth, mHostLayoutInfo.designHeight, mHostLayoutInfo.designDensity);
7151
if (params instanceof ViewGroup.MarginLayoutParams) {
7252
info.fillMarginLayoutParams((ViewGroup.MarginLayoutParams) params);
7353
} else {
@@ -79,6 +59,18 @@ public void adjustChildren(int widthMeasureSpec, int heightMeasureSpec) {
7959
}
8060
}
8161

62+
public void adjustSelf(int widthMeasureSpec, int heightMeasureSpec) {
63+
if (mHostLayoutInfo != null) {
64+
ViewGroup.LayoutParams params = mHost.getLayoutParams();
65+
if (params instanceof ViewGroup.MarginLayoutParams) {
66+
mHostLayoutInfo.fillMarginLayoutParams((ViewGroup.MarginLayoutParams) params);
67+
} else {
68+
mHostLayoutInfo.fillLayoutParams(params);
69+
}
70+
mHost.setPadding(mHostLayoutInfo.leftPadding, mHostLayoutInfo.topPadding, mHostLayoutInfo.rightPadding, mHostLayoutInfo.bottomPadding);
71+
}
72+
}
73+
8274
public void restoreOriginalParams() {
8375
int i = 0;
8476

@@ -199,6 +191,24 @@ public static ScaleLayoutInfo getScaleLayoutInfo(Context context, AttributeSet a
199191
info.bottomPadding = value;
200192
}
201193

194+
value = array.getDimensionPixelSize(R.styleable.ScaleLayout_layout_design_width, 0);
195+
if (value != 0) {
196+
info = info != null ? info : new ScaleLayoutInfo(context);
197+
info.designWidth = value;
198+
}
199+
200+
value = array.getDimensionPixelSize(R.styleable.ScaleLayout_layout_design_height, 0);
201+
if (value != 0) {
202+
info = info != null ? info : new ScaleLayoutInfo(context);
203+
info.designHeight = value;
204+
}
205+
206+
value = array.getInteger(R.styleable.ScaleLayout_layout_design_density, 0);
207+
if (value != 0) {
208+
info = info != null ? info : new ScaleLayoutInfo(context);
209+
info.designDensity = value;
210+
}
211+
202212
array.recycle();
203213

204214
Log.d(TAG, "constructed: " + info);
@@ -285,13 +295,29 @@ public void fillLayoutParams(ViewGroup.LayoutParams params) {
285295
this.mPreservedParams.width = params.width;
286296
this.mPreservedParams.height = params.height;
287297

288-
if (this.width > 0) {
298+
if (this.width > 0.0F) {
289299
params.width = getRealPixelSize(this.width);
290300
}
291301

292-
if (this.height > 0) {
302+
if (this.height > 0.0F) {
293303
params.height = getRealPixelSize(this.height);
294304
}
305+
306+
if (this.leftPadding > 0.0F) {
307+
this.leftPadding = getRealPixelSize(this.leftPadding);
308+
}
309+
310+
if (this.topPadding > 0.0F) {
311+
this.topPadding = getRealPixelSize(this.topPadding);
312+
}
313+
314+
if (this.rightPadding > 0.0F) {
315+
this.rightPadding = getRealPixelSize(this.rightPadding);
316+
}
317+
318+
if (this.bottomPadding > 0.0F) {
319+
this.bottomPadding = getRealPixelSize(this.bottomPadding);
320+
}
295321
}
296322

297323
public void fillMarginLayoutParams(ViewGroup.MarginLayoutParams params) {
@@ -303,27 +329,27 @@ public void fillMarginLayoutParams(ViewGroup.MarginLayoutParams params) {
303329
MarginLayoutParamsCompat.setMarginStart(this.mPreservedParams, MarginLayoutParamsCompat.getMarginStart(params));
304330
MarginLayoutParamsCompat.setMarginEnd(this.mPreservedParams, MarginLayoutParamsCompat.getMarginEnd(params));
305331

306-
if (this.leftMargin >= 0.0F) {
332+
if (this.leftMargin > 0.0F) {
307333
params.leftMargin = getRealPixelSize(this.leftMargin);
308334
}
309335

310-
if (this.topMargin >= 0.0F) {
336+
if (this.topMargin > 0.0F) {
311337
params.topMargin = getRealPixelSize(this.topMargin);
312338
}
313339

314-
if (this.rightMargin >= 0.0F) {
340+
if (this.rightMargin > 0.0F) {
315341
params.rightMargin = getRealPixelSize(this.rightMargin);
316342
}
317343

318-
if (this.bottomMargin >= 0.0F) {
344+
if (this.bottomMargin > 0.0F) {
319345
params.bottomMargin = getRealPixelSize(this.bottomMargin);
320346
}
321347

322-
if (this.startMargin >= 0.0F) {
348+
if (this.startMargin > 0.0F) {
323349
MarginLayoutParamsCompat.setMarginStart(params, getRealPixelSize(this.startMargin));
324350
}
325351

326-
if (this.endMargin >= 0.0F) {
352+
if (this.endMargin > 0.0F) {
327353
MarginLayoutParamsCompat.setMarginEnd(params, getRealPixelSize(this.endMargin));
328354
}
329355

README.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# Android-ScaleLayout
2+
3+
A MultiScreen-Support-Library for Android
4+
5+
* Simple
6+
7+

Sample/src/main/res/layout/activity_main.xml

Lines changed: 22 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -11,25 +11,35 @@
1111
android:layout_width="wrap_content"
1212
android:layout_height="wrap_content"
1313
android:background="#46b34b"
14-
android:text="@string/hello_world"
1514
app:layout_height="120dp"
1615
app:layout_marginLeft="12dp"
1716
app:layout_marginTop="8dp"
1817
app:layout_scale_by="width"
1918
app:layout_width="166dp" />
2019

21-
<TextView
20+
<cn.gavinliu.android.lib.scale.ScaleRelativeLayout
2221
android:layout_width="wrap_content"
2322
android:layout_height="wrap_content"
24-
android:layout_toRightOf="@id/view"
25-
android:background="#46b34b"
26-
android:text="@string/hello_world"
27-
app:layout_height="120dp"
28-
app:layout_marginLeft="4dp"
29-
app:layout_marginTop="8dp"
30-
app:layout_paddingLeft="12dp"
31-
app:layout_paddingTop="12dp"
32-
app:layout_scale_by="width"
33-
app:layout_width="166dp" />
23+
android:layout_below="@id/view"
24+
android:background="#46b"
25+
app:layout_design_density="@integer/app_design_density"
26+
app:layout_design_height="@dimen/app_design_height"
27+
app:layout_design_width="@dimen/app_design_width"
28+
app:layout_height="300dp"
29+
app:layout_marginTop="5dp"
30+
app:layout_width="300dp">
31+
32+
<TextView
33+
android:layout_width="wrap_content"
34+
android:layout_height="wrap_content"
35+
android:background="#46b34b"
36+
app:layout_height="120dp"
37+
app:layout_marginLeft="12dp"
38+
app:layout_marginTop="8dp"
39+
app:layout_scale_by="width"
40+
app:layout_width="166dp" />
41+
42+
</cn.gavinliu.android.lib.scale.ScaleRelativeLayout>
43+
3444

3545
</cn.gavinliu.android.lib.scale.ScaleRelativeLayout>

0 commit comments

Comments
 (0)