Add support for percent base parameters - width, height & margins

This commit is contained in:
hshristov
2015-09-11 15:04:54 +03:00
parent 32eb8ca3aa
commit c79ff2a4cc
9 changed files with 50 additions and 7 deletions

View File

@@ -30,6 +30,7 @@ public class AbsoluteLayout extends LayoutBase {
continue;
}
CommonLayoutParams.updateChildLayoutParams(child, widthMeasureSpec, heightMeasureSpec);
CommonLayoutParams.measureChild(child, childMeasureSpec, childMeasureSpec);
final int childMeasuredWidth = CommonLayoutParams.getDesiredWidth(child);
final int childMeasuredHeight = CommonLayoutParams.getDesiredHeight(child);

View File

@@ -28,6 +28,14 @@ public class CommonLayoutParams extends FrameLayout.LayoutParams {
super(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
}
public float widthPercent = 0;
public float heightPercent = 0;
public float topMarginPercent = 0;
public float leftMarginPercent = 0;
public float bottomMarginPercent = 0;
public float rightMarginPercent = 0;
public int left = 0;
public int top = 0;
public int row = 0;
@@ -177,7 +185,7 @@ public class CommonLayoutParams extends FrameLayout.LayoutParams {
return;
}
// Negative means we are not initialized.
// Negative means not initialized.
if(debuggable < 0) {
try {
Context context = child.getContext();
@@ -189,24 +197,49 @@ public class CommonLayoutParams extends FrameLayout.LayoutParams {
}
}
int childWidthMeasureSpec = getMeasureSpec(child, widthMeasureSpec, true);
int childHeightMeasureSpec = getMeasureSpec(child, heightMeasureSpec, false);
if (debuggable > 0) {
sb.setLength(0);
sb.append(child.getParent().toString());
sb.append(" :measureChild: ");
sb.append(child.toString());
sb.append(" ");
sb.append(MeasureSpec.toString(widthMeasureSpec));
sb.append(MeasureSpec.toString(childWidthMeasureSpec));
sb.append(", ");
sb.append(MeasureSpec.toString(heightMeasureSpec));
sb.append(MeasureSpec.toString(childHeightMeasureSpec));
log(tag, sb.toString());
}
int childWidthMeasureSpec = getMeasureSpec(child, widthMeasureSpec, true);
int childHeightMeasureSpec = getMeasureSpec(child, heightMeasureSpec, false);
child.measure(childWidthMeasureSpec, childHeightMeasureSpec);
}
public static void updateChildLayoutParams(View child, int widthMeasureSpec, int heightMeasureSpec) {
int availableWidth = MeasureSpec.getSize(widthMeasureSpec);
int availableHeight = MeasureSpec.getSize(heightMeasureSpec);
CommonLayoutParams lp = (CommonLayoutParams)child.getLayoutParams();
if (lp.widthPercent > 0) {
lp.width = (int)(availableWidth * lp.widthPercent);
}
if (lp.heightPercent > 0) {
lp.height = (int)(availableHeight * lp.heightPercent);
}
if (lp.leftMarginPercent > 0) {
lp.leftMargin = (int)(availableWidth * lp.leftMarginPercent);
}
if (lp.rightMarginPercent > 0) {
lp.rightMargin = (int)(availableWidth * lp.rightMarginPercent);
}
if (lp.topMarginPercent > 0) {
lp.topMargin = (int)(availableHeight * lp.topMarginPercent);
}
if (lp.bottomMarginPercent > 0) {
lp.bottomMargin = (int)(availableHeight * lp.bottomMarginPercent);
}
}
static void log(String tag, String message) {
Log.d(tag, message);
}

View File

@@ -29,6 +29,7 @@ public class ContentLayout extends LayoutBase {
continue;
}
CommonLayoutParams.updateChildLayoutParams(child, widthMeasureSpec, heightMeasureSpec);
CommonLayoutParams.measureChild(child, widthMeasureSpec, heightMeasureSpec);
final int childMeasuredWidth = CommonLayoutParams.getDesiredWidth(child);
final int childMeasuredHeight = CommonLayoutParams.getDesiredHeight(child);

View File

@@ -64,6 +64,7 @@ public class DockLayout extends LayoutBase {
childHeightMeasureSpec = MeasureSpec.makeMeasureSpec(remainingHeight, heightMode == MeasureSpec.EXACTLY ? MeasureSpec.AT_MOST : heightMode);
}
CommonLayoutParams.updateChildLayoutParams(child, widthMeasureSpec, heightMeasureSpec);
CommonLayoutParams.measureChild(child, childWidthMeasureSpec, childHeightMeasureSpec);
final int childMeasuredWidth = CommonLayoutParams.getDesiredWidth(child);
final int childMeasuredHeight = CommonLayoutParams.getDesiredHeight(child);

View File

@@ -272,6 +272,7 @@ public class GridLayout extends LayoutBase {
continue;
}
CommonLayoutParams.updateChildLayoutParams(child, widthMeasureSpec, heightMeasureSpec);
MeasureSpecs measureSpecs = this.map.get(child);
this.updateMeasureSpecs(child, measureSpecs);
this.helper.addMeasureSpec(measureSpecs);

View File

@@ -74,6 +74,7 @@ public class HorizontalScrollView extends android.widget.HorizontalScrollView {
this.contentMeasuredHeight = 0;
}
else {
CommonLayoutParams.updateChildLayoutParams(child, widthMeasureSpec, heightMeasureSpec);
CommonLayoutParams.measureChild(child, MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED), heightMeasureSpec);
this.contentMeasuredWidth = CommonLayoutParams.getDesiredWidth(child);
this.contentMeasuredHeight = CommonLayoutParams.getDesiredHeight(child);
@@ -84,6 +85,7 @@ public class HorizontalScrollView extends android.widget.HorizontalScrollView {
}
// Don't add in our paddings because they are already added as child margins. (we will include them twice if we add them).
// Check the previous line - this.setPadding(lp.leftMargin, lp.topMargin, lp.rightMargin, lp.bottomMargin);
// this.contentMeasuredWidth += this.getPaddingLeft() + this.getPaddingRight();
// this.contentMeasuredHeight += this.getPaddingTop() + this.getPaddingBottom();

View File

@@ -77,6 +77,7 @@ public class StackLayout extends LayoutBase {
continue;
}
CommonLayoutParams.updateChildLayoutParams(child, widthMeasureSpec, heightMeasureSpec);
if (isVertical) {
CommonLayoutParams.measureChild(child, childMeasureSpec, MeasureSpec.makeMeasureSpec(remainingLength, measureSpecMode));
final int childMeasuredWidth = CommonLayoutParams.getDesiredWidth(child);

View File

@@ -75,6 +75,7 @@ public class VerticalScrollView extends ScrollView {
this.setPadding(0, 0, 0, 0);
}
else {
CommonLayoutParams.updateChildLayoutParams(child, widthMeasureSpec, heightMeasureSpec);
CommonLayoutParams.measureChild(child, widthMeasureSpec, MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED));
this.contentMeasuredWidth = CommonLayoutParams.getDesiredWidth(child);
this.contentMeasuredHeight = CommonLayoutParams.getDesiredHeight(child);
@@ -85,6 +86,7 @@ public class VerticalScrollView extends ScrollView {
}
// Don't add in our paddings because they are already added as child margins. (we will include them twice if we add them).
// check the previous line - this.setPadding(lp.leftMargin, lp.topMargin, lp.rightMargin, lp.bottomMargin);
// this.contentMeasuredWidth += this.getPaddingLeft() + this.getPaddingRight();
// this.contentMeasuredHeight += this.getPaddingTop() + this.getPaddingBottom();

View File

@@ -92,6 +92,7 @@ public class WrapLayout extends LayoutBase {
continue;
}
CommonLayoutParams.updateChildLayoutParams(child, widthMeasureSpec, heightMeasureSpec);
CommonLayoutParams.measureChild(child, childWidthMeasureSpec, childHeightMeasureSpec);
final int childMeasuredWidth = CommonLayoutParams.getDesiredWidth(child);
final int childMeasuredHeight = CommonLayoutParams.getDesiredHeight(child);