Merge pull request #4 from NativeScript/hhristov/fix-verbose-logs

Fix for GridLayout desired size when not stretched
This commit is contained in:
Hristo Hristov
2015-10-09 16:42:42 +03:00
8 changed files with 68 additions and 47 deletions

View File

@@ -20,7 +20,7 @@ import android.widget.FrameLayout;
*/ */
public class CommonLayoutParams extends FrameLayout.LayoutParams { public class CommonLayoutParams extends FrameLayout.LayoutParams {
static final String tag = "NSLayout"; static final String TAG = "NSLayout";
static int debuggable = -1; static int debuggable = -1;
private static final StringBuilder sb = new StringBuilder(); private static final StringBuilder sb = new StringBuilder();
@@ -154,7 +154,7 @@ public class CommonLayoutParams extends FrameLayout.LayoutParams {
sb.append(MeasureSpec.toString(widthMeasureSpec)); sb.append(MeasureSpec.toString(widthMeasureSpec));
sb.append(", "); sb.append(", ");
sb.append(MeasureSpec.toString(heightMeasureSpec)); sb.append(MeasureSpec.toString(heightMeasureSpec));
log(tag, sb.toString()); log(TAG, sb.toString());
} }
child.measure(widthMeasureSpec, heightMeasureSpec); child.measure(widthMeasureSpec, heightMeasureSpec);
@@ -174,7 +174,7 @@ public class CommonLayoutParams extends FrameLayout.LayoutParams {
sb.append(childRight); sb.append(childRight);
sb.append(", "); sb.append(", ");
sb.append(childBottom); sb.append(childBottom);
log(tag, sb.toString()); log(TAG, sb.toString());
} }
child.layout(childLeft, childTop, childRight, childBottom); child.layout(childLeft, childTop, childRight, childBottom);
@@ -189,11 +189,16 @@ public class CommonLayoutParams extends FrameLayout.LayoutParams {
if(debuggable < 0) { if(debuggable < 0) {
try { try {
Context context = child.getContext(); Context context = child.getContext();
int flags = context.getPackageManager().getPackageInfo(context.getPackageName(), 0).applicationInfo.flags; ApplicationInfo ai = context.getPackageManager().getApplicationInfo(context.getPackageName(), android.content.pm.PackageManager.GET_META_DATA);
debuggable = (flags & ApplicationInfo.FLAG_DEBUGGABLE) != 0 ? 1 : 0; android.os.Bundle bundle = ai.metaData;
} Boolean debugLayouts = bundle != null ? bundle.getBoolean("debugLayouts", false) : false;
catch (NameNotFoundException e) { debuggable = debugLayouts ? 1 : 0;
} catch (NameNotFoundException e) {
debuggable = 0; debuggable = 0;
Log.e(TAG, "Failed to load meta-data, NameNotFound: " + e.getMessage());
} catch (NullPointerException e) {
debuggable = 0;
Log.e(TAG, "Failed to load meta-data, NullPointer: " + e.getMessage());
} }
} }
@@ -209,7 +214,7 @@ public class CommonLayoutParams extends FrameLayout.LayoutParams {
sb.append(MeasureSpec.toString(childWidthMeasureSpec)); sb.append(MeasureSpec.toString(childWidthMeasureSpec));
sb.append(", "); sb.append(", ");
sb.append(MeasureSpec.toString(childHeightMeasureSpec)); sb.append(MeasureSpec.toString(childHeightMeasureSpec));
log(tag, sb.toString()); log(TAG, sb.toString());
} }
child.measure(childWidthMeasureSpec, childHeightMeasureSpec); child.measure(childWidthMeasureSpec, childHeightMeasureSpec);
@@ -218,27 +223,40 @@ public class CommonLayoutParams extends FrameLayout.LayoutParams {
public static void updateChildLayoutParams(View child, int widthMeasureSpec, int heightMeasureSpec) { public static void updateChildLayoutParams(View child, int widthMeasureSpec, int heightMeasureSpec) {
int availableWidth = MeasureSpec.getSize(widthMeasureSpec); int availableWidth = MeasureSpec.getSize(widthMeasureSpec);
int widthSpec = MeasureSpec.getMode(widthMeasureSpec);
int availableHeight = MeasureSpec.getSize(heightMeasureSpec); int availableHeight = MeasureSpec.getSize(heightMeasureSpec);
int heightSpec = MeasureSpec.getMode(heightMeasureSpec);
CommonLayoutParams lp = (CommonLayoutParams)child.getLayoutParams(); CommonLayoutParams lp = (CommonLayoutParams)child.getLayoutParams();
if (widthSpec != MeasureSpec.UNSPECIFIED) {
if (lp.widthPercent > 0) { if (lp.widthPercent > 0) {
lp.width = (int)(availableWidth * lp.widthPercent); lp.width = (int)(availableWidth * lp.widthPercent);
} }
if (lp.heightPercent > 0) {
lp.height = (int)(availableHeight * lp.heightPercent);
}
if (lp.leftMarginPercent > 0) { if (lp.leftMarginPercent > 0) {
lp.leftMargin = (int)(availableWidth * lp.leftMarginPercent); lp.leftMargin = (int)(availableWidth * lp.leftMarginPercent);
} }
if (lp.rightMarginPercent > 0) { if (lp.rightMarginPercent > 0) {
lp.rightMargin = (int)(availableWidth * lp.rightMarginPercent); lp.rightMargin = (int)(availableWidth * lp.rightMarginPercent);
} }
}
if (heightSpec != MeasureSpec.UNSPECIFIED) {
if (lp.heightPercent > 0) {
lp.height = (int)(availableHeight * lp.heightPercent);
}
if (lp.topMarginPercent > 0) { if (lp.topMarginPercent > 0) {
lp.topMargin = (int)(availableHeight * lp.topMarginPercent); lp.topMargin = (int)(availableHeight * lp.topMarginPercent);
} }
if (lp.bottomMarginPercent > 0) { if (lp.bottomMarginPercent > 0) {
lp.bottomMargin = (int)(availableHeight * lp.bottomMarginPercent); lp.bottomMargin = (int)(availableHeight * lp.bottomMarginPercent);
} }
} }
}
static void log(String tag, String message) { static void log(String tag, String message) {
Log.d(tag, message); Log.d(tag, message);

View File

@@ -700,13 +700,15 @@ class MeasureHelper {
this.columnStarValue = columnStarCount > 0 ? (this.width - currentColumnWidth) / columnStarCount : 0; this.columnStarValue = columnStarCount > 0 ? (this.width - currentColumnWidth) / columnStarCount : 0;
for(int i = 0; i < columnCount; i++) { if (this.stretchedHorizontally) {
for (int i = 0; i < columnCount; i++) {
ItemGroup item = this.columns.get(i); ItemGroup item = this.columns.get(i);
if (item.getIsStar()) { if (item.getIsStar()) {
item.length = item.rowOrColumn.getValue() * this.columnStarValue; item.length = item.rowOrColumn.getValue() * this.columnStarValue;
} }
} }
} }
}
private void fixRows() { private void fixRows() {
float currentRowHeight = 0; float currentRowHeight = 0;
@@ -725,13 +727,15 @@ class MeasureHelper {
this.rowStarValue = rowStarCount > 0 ? (this.height - currentRowHeight) / rowStarCount : 0; this.rowStarValue = rowStarCount > 0 ? (this.height - currentRowHeight) / rowStarCount : 0;
for(int i = 0; i < rowCount; i++) { if(this.stretchedVertically) {
for (int i = 0; i < rowCount; i++) {
ItemGroup item = this.rows.get(i); ItemGroup item = this.rows.get(i);
if (item.getIsStar()) { if (item.getIsStar()) {
item.length = item.rowOrColumn.getValue() * this.rowStarValue; item.length = item.rowOrColumn.getValue() * this.rowStarValue;
} }
} }
} }
}
private void fakeMeasure() { private void fakeMeasure() {
// Fake measure - measure all elements that have star rows and auto columns - with infinity Width and infinity Height // Fake measure - measure all elements that have star rows and auto columns - with infinity Width and infinity Height

View File

@@ -99,7 +99,7 @@ public class ImageView extends android.widget.ImageView {
sb.append(", measureHeight: "); sb.append(", measureHeight: ");
sb.append(measureHeight); sb.append(measureHeight);
CommonLayoutParams.log(CommonLayoutParams.tag, sb.toString()); CommonLayoutParams.log(CommonLayoutParams.TAG, sb.toString());
} }
int widthSizeAndState = resolveSizeAndState(measureWidth, widthMeasureSpec, 0); int widthSizeAndState = resolveSizeAndState(measureWidth, widthMeasureSpec, 0);

View File

@@ -20,7 +20,6 @@ public abstract class LayoutBase extends ViewGroup {
super(context); super(context);
} }
@Override @Override
protected LayoutParams generateDefaultLayoutParams() { protected LayoutParams generateDefaultLayoutParams() {
return new CommonLayoutParams(); return new CommonLayoutParams();