mirror of
https://github.com/NativeScript/NativeScript.git
synced 2025-11-05 13:26:48 +08:00
Add support for percent base parameters - width, height & margins
This commit is contained in:
@@ -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);
|
||||
|
||||
@@ -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,25 +197,50 @@ 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);
|
||||
}
|
||||
|
||||
static void log(String tag, String message) {
|
||||
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);
|
||||
}
|
||||
|
||||
|
||||
@@ -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);
|
||||
|
||||
@@ -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);
|
||||
|
||||
@@ -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);
|
||||
|
||||
@@ -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();
|
||||
|
||||
|
||||
@@ -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);
|
||||
|
||||
@@ -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();
|
||||
|
||||
|
||||
@@ -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);
|
||||
|
||||
Reference in New Issue
Block a user