From c0d47c8c3f7b66207aa67672204431e2a72effaf Mon Sep 17 00:00:00 2001 From: hshristov Date: Mon, 3 Aug 2015 10:59:04 +0300 Subject: [PATCH] StackLayout now reduce available height by its padding TextView is now remeasured so that it centers its text. This is needed because when its given more size in onLayout it won't center correctly. --- .../widgets/CommonLayoutParams.java | 25 +++++++++++++++++++ src/org/nativescript/widgets/StackLayout.java | 2 +- 2 files changed, 26 insertions(+), 1 deletion(-) diff --git a/src/org/nativescript/widgets/CommonLayoutParams.java b/src/org/nativescript/widgets/CommonLayoutParams.java index 16f9f53b9..c1744c1b8 100644 --- a/src/org/nativescript/widgets/CommonLayoutParams.java +++ b/src/org/nativescript/widgets/CommonLayoutParams.java @@ -124,6 +124,31 @@ public class CommonLayoutParams extends FrameLayout.LayoutParams { childLeft = Math.round(childLeft); childTop = Math.round(childTop); + // Re-measure TextView because it is not centered if layout width is larger than measure width. + if (child instanceof android.widget.TextView) { + int measuredWidth = child.getMeasuredWidth(); + int measuredHeight = child.getMeasuredHeight(); + + int width = right - left; + int height = bottom - top; + if (Math.abs(measuredWidth - width) > 1 || Math.abs(measuredHeight - height) > 1) { + int widthMeasureSpec = MeasureSpec.makeMeasureSpec(width, MeasureSpec.EXACTLY); + int heightMeasureSpec = MeasureSpec.makeMeasureSpec(height, MeasureSpec.EXACTLY); + if (debuggable > 0) { + sb.setLength(0); + sb.append("remeasure "); + sb.append(child); + sb.append(" with "); + sb.append(MeasureSpec.toString(widthMeasureSpec)); + sb.append(", "); + sb.append(MeasureSpec.toString(heightMeasureSpec)); + log(tag, sb.toString()); + } + + child.measure(widthMeasureSpec, heightMeasureSpec); + } + } + if (debuggable > 0) { sb.setLength(0); sb.append(child.getParent().toString()); diff --git a/src/org/nativescript/widgets/StackLayout.java b/src/org/nativescript/widgets/StackLayout.java index 43e943434..afac0f977 100644 --- a/src/org/nativescript/widgets/StackLayout.java +++ b/src/org/nativescript/widgets/StackLayout.java @@ -87,7 +87,7 @@ public class StackLayout extends LayoutBase { remainingLength = Math.max(0, remainingLength - childMeasuredHeight); } else { - CommonLayoutParams.measureChild(child, MeasureSpec.makeMeasureSpec(remainingLength, measureSpecMode), heightMeasureSpec); + CommonLayoutParams.measureChild(child, MeasureSpec.makeMeasureSpec(remainingLength, measureSpecMode), childMeasureSpec); final int childMeasuredWidth = CommonLayoutParams.getDesiredWidth(child); final int childMeasuredHeight = CommonLayoutParams.getDesiredHeight(child);