From c79ff2a4cc11af843bc50d3b5ed9de849c567665 Mon Sep 17 00:00:00 2001 From: hshristov Date: Fri, 11 Sep 2015 15:04:54 +0300 Subject: [PATCH] Add support for percent base parameters - width, height & margins --- .../nativescript/widgets/AbsoluteLayout.java | 1 + .../widgets/CommonLayoutParams.java | 47 ++++++++++++++++--- .../nativescript/widgets/ContentLayout.java | 1 + src/org/nativescript/widgets/DockLayout.java | 1 + src/org/nativescript/widgets/GridLayout.java | 1 + .../widgets/HorizontalScrollView.java | 2 + src/org/nativescript/widgets/StackLayout.java | 1 + .../widgets/VerticalScrollView.java | 2 + src/org/nativescript/widgets/WrapLayout.java | 1 + 9 files changed, 50 insertions(+), 7 deletions(-) diff --git a/src/org/nativescript/widgets/AbsoluteLayout.java b/src/org/nativescript/widgets/AbsoluteLayout.java index bb5ca5206..aaee2f6a8 100644 --- a/src/org/nativescript/widgets/AbsoluteLayout.java +++ b/src/org/nativescript/widgets/AbsoluteLayout.java @@ -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); diff --git a/src/org/nativescript/widgets/CommonLayoutParams.java b/src/org/nativescript/widgets/CommonLayoutParams.java index 56d8cc893..79028628c 100644 --- a/src/org/nativescript/widgets/CommonLayoutParams.java +++ b/src/org/nativescript/widgets/CommonLayoutParams.java @@ -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); } diff --git a/src/org/nativescript/widgets/ContentLayout.java b/src/org/nativescript/widgets/ContentLayout.java index aadb72f40..e2459027a 100644 --- a/src/org/nativescript/widgets/ContentLayout.java +++ b/src/org/nativescript/widgets/ContentLayout.java @@ -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); diff --git a/src/org/nativescript/widgets/DockLayout.java b/src/org/nativescript/widgets/DockLayout.java index a36355e7a..5d27737c0 100644 --- a/src/org/nativescript/widgets/DockLayout.java +++ b/src/org/nativescript/widgets/DockLayout.java @@ -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); diff --git a/src/org/nativescript/widgets/GridLayout.java b/src/org/nativescript/widgets/GridLayout.java index cf1ec81dd..0c242ec10 100644 --- a/src/org/nativescript/widgets/GridLayout.java +++ b/src/org/nativescript/widgets/GridLayout.java @@ -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); diff --git a/src/org/nativescript/widgets/HorizontalScrollView.java b/src/org/nativescript/widgets/HorizontalScrollView.java index 734101d59..18f597446 100644 --- a/src/org/nativescript/widgets/HorizontalScrollView.java +++ b/src/org/nativescript/widgets/HorizontalScrollView.java @@ -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(); diff --git a/src/org/nativescript/widgets/StackLayout.java b/src/org/nativescript/widgets/StackLayout.java index afac0f977..73d83b061 100644 --- a/src/org/nativescript/widgets/StackLayout.java +++ b/src/org/nativescript/widgets/StackLayout.java @@ -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); diff --git a/src/org/nativescript/widgets/VerticalScrollView.java b/src/org/nativescript/widgets/VerticalScrollView.java index d758b1174..e03a1f9e7 100644 --- a/src/org/nativescript/widgets/VerticalScrollView.java +++ b/src/org/nativescript/widgets/VerticalScrollView.java @@ -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(); diff --git a/src/org/nativescript/widgets/WrapLayout.java b/src/org/nativescript/widgets/WrapLayout.java index c3c842e5e..a660aeeee 100644 --- a/src/org/nativescript/widgets/WrapLayout.java +++ b/src/org/nativescript/widgets/WrapLayout.java @@ -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);