From 470164c3583956c20eba3f2c13fc62e43868d889 Mon Sep 17 00:00:00 2001 From: Hristo Hristov Date: Thu, 15 Dec 2016 17:19:08 +0200 Subject: [PATCH 1/5] fixes --- .../widgets/CommonLayoutParams.java | 2 +- .../org/nativescript/widgets/TabStrip.java | 16 +- .../org/nativescript/widgets/ViewHelper.java | 781 ++++++++++-------- 3 files changed, 465 insertions(+), 334 deletions(-) diff --git a/android/widgets/src/main/java/org/nativescript/widgets/CommonLayoutParams.java b/android/widgets/src/main/java/org/nativescript/widgets/CommonLayoutParams.java index 190e9582d..4020e9f3f 100644 --- a/android/widgets/src/main/java/org/nativescript/widgets/CommonLayoutParams.java +++ b/android/widgets/src/main/java/org/nativescript/widgets/CommonLayoutParams.java @@ -26,7 +26,7 @@ public class CommonLayoutParams extends FrameLayout.LayoutParams { private static final StringBuilder sb = new StringBuilder(); public CommonLayoutParams() { - super(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT); + super(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT, Gravity.FILL); } public float widthPercent = 0; diff --git a/android/widgets/src/main/java/org/nativescript/widgets/TabStrip.java b/android/widgets/src/main/java/org/nativescript/widgets/TabStrip.java index 4fb0ea918..48c7b2202 100644 --- a/android/widgets/src/main/java/org/nativescript/widgets/TabStrip.java +++ b/android/widgets/src/main/java/org/nativescript/widgets/TabStrip.java @@ -51,8 +51,8 @@ class TabStrip extends LinearLayout { private final SimpleTabColorizer mDefaultTabColorizer; private int mDefaultTabTextColor; - private Integer mTabTextColor; - private Integer mSelectedTabTextColor; + private int mTabTextColor = -1; + private int mSelectedTabTextColor = -1; TabStrip(Context context) { this(context, null); @@ -98,21 +98,21 @@ class TabStrip extends LinearLayout { invalidate(); } - void setTabTextColor(Integer color){ + void setTabTextColor(int color){ mTabTextColor = color; updateTabsTextColor(); } - Integer getTabTextColor(){ + int getTabTextColor(){ return mTabTextColor; } - void setSelectedTabTextColor(Integer color){ + void setSelectedTabTextColor(int color){ mSelectedTabTextColor = color; updateTabsTextColor(); } - Integer getSelectedTabTextColor(){ + int getSelectedTabTextColor(){ return mSelectedTabTextColor; } @@ -122,7 +122,7 @@ class TabStrip extends LinearLayout { LinearLayout linearLayout = (LinearLayout)getChildAt(i); TextView textView = (TextView)linearLayout.getChildAt(1); if (i == mSelectedPosition){ - if (mSelectedTabTextColor != null){ + if (mSelectedTabTextColor >= 0){ textView.setTextColor(mSelectedTabTextColor); } else { @@ -130,7 +130,7 @@ class TabStrip extends LinearLayout { } } else { - if (mTabTextColor != null){ + if (mTabTextColor >= 0){ textView.setTextColor(mTabTextColor); } else { diff --git a/android/widgets/src/main/java/org/nativescript/widgets/ViewHelper.java b/android/widgets/src/main/java/org/nativescript/widgets/ViewHelper.java index f4c8859f7..4f4946c33 100644 --- a/android/widgets/src/main/java/org/nativescript/widgets/ViewHelper.java +++ b/android/widgets/src/main/java/org/nativescript/widgets/ViewHelper.java @@ -11,383 +11,514 @@ import android.widget.FrameLayout; */ public class ViewHelper { - private ViewHelper() { + private ViewHelper() { + } + + static final int version = android.os.Build.VERSION.SDK_INT; + + public static int getMinWidth(android.view.View view) { + return view.getMinimumWidth(); + } + + public static void setMinWidth(android.view.View view, int value) { + view.setMinimumWidth(value); + } + + public static int getMinHeight(android.view.View view) { + return view.getMinimumHeight(); + } + + public static void setMinHeight(android.view.View view, int value) { + view.setMinimumHeight(value); + } + + public static int getWidth(android.view.View view) { + ViewGroup.LayoutParams params = view.getLayoutParams(); + if (params != null) { + return params.width; } - static final int version = android.os.Build.VERSION.SDK_INT; + return ViewGroup.LayoutParams.MATCH_PARENT; + } - public static int getMinWidth(android.view.View view) { - return view.getMinimumWidth(); - } - public static void setMinWidth(android.view.View view, int value) { - view.setMinimumWidth(value); + public static void setWidth(android.view.View view, int value) { + ViewGroup.LayoutParams params = view.getLayoutParams(); + if (params == null) { + params = new CommonLayoutParams(); + params.width = value; } - public static int getMinHeight(android.view.View view) { - return view.getMinimumHeight(); - } - public static void setMinHeight(android.view.View view, int value) { - view.setMinimumHeight(value); + view.setLayoutParams(params); + } + + public static void setWidthPercent(android.view.View view, int value) { + ViewGroup.LayoutParams params = view.getLayoutParams(); + if (params == null) { + CommonLayoutParams lp = new CommonLayoutParams(); } - public static int getWidth(android.view.View view) { - ViewGroup.LayoutParams params = view.getLayoutParams(); - if (params != null) { - return params.width; - } - - return ViewGroup.LayoutParams.MATCH_PARENT; + if (params instanceof CommonLayoutParams) { + CommonLayoutParams lp = (CommonLayoutParams) params; + lp.widthPercent = value; + lp.width = (lp.gravity & Gravity.HORIZONTAL_GRAVITY_MASK) == Gravity.FILL_HORIZONTAL ? ViewGroup.LayoutParams.MATCH_PARENT : ViewGroup.LayoutParams.WRAP_CONTENT; + view.setLayoutParams(params); } - public static void setWidth(android.view.View view, int value) { - ViewGroup.LayoutParams params = view.getLayoutParams(); - if (params == null) { - params = new CommonLayoutParams(); - params.width = value; - } + } + public static int getHeight(android.view.View view) { + ViewGroup.LayoutParams params = view.getLayoutParams(); + if (params != null) { + return params.height; + } + + return ViewGroup.LayoutParams.MATCH_PARENT; + } + + public static void setHeight(android.view.View view, int value) { + ViewGroup.LayoutParams params = view.getLayoutParams(); + if (params == null) { + params = new CommonLayoutParams(); + params.height = value; + } + + view.setLayoutParams(params); + } + + public static void setHeightPercent(android.view.View view, int value) { + ViewGroup.LayoutParams params = view.getLayoutParams(); + if (params == null) { + CommonLayoutParams lp = new CommonLayoutParams(); + } + + if (params instanceof CommonLayoutParams) { + CommonLayoutParams lp = (CommonLayoutParams) params; + lp.heightPercent = value; + lp.height = (lp.gravity & Gravity.VERTICAL_GRAVITY_MASK) == Gravity.FILL_VERTICAL ? ViewGroup.LayoutParams.MATCH_PARENT : ViewGroup.LayoutParams.WRAP_CONTENT; + view.setLayoutParams(params); + } + } + + public static Rect getMargin(android.view.View view) { + ViewGroup.LayoutParams params = view.getLayoutParams(); + if (params instanceof ViewGroup.MarginLayoutParams) { + ViewGroup.MarginLayoutParams lp = (ViewGroup.MarginLayoutParams) params; + return new Rect(lp.leftMargin, lp.topMargin, lp.rightMargin, lp.bottomMargin); + } + + return new Rect(); + } + + public static void setMargin(android.view.View view, int left, int top, int right, int bottom) { + ViewGroup.LayoutParams params = view.getLayoutParams(); + // Initialize if empty. + if (params == null) { + params = new CommonLayoutParams(); + } + + // Set margins only if params are of the correct type. + if (params instanceof ViewGroup.MarginLayoutParams) { + ViewGroup.MarginLayoutParams lp = (ViewGroup.MarginLayoutParams) params; + lp.leftMargin = left; + lp.topMargin = top; + lp.rightMargin = right; + lp.bottomMargin = bottom; + view.setLayoutParams(params); + } + } + + public static int getMarginLeft(android.view.View view) { + ViewGroup.LayoutParams params = view.getLayoutParams(); + if (params instanceof ViewGroup.MarginLayoutParams) { + ViewGroup.MarginLayoutParams lp = (ViewGroup.MarginLayoutParams) params; + return lp.leftMargin; + } + + return 0; + } + + public static void setMarginLeft(android.view.View view, int value) { + ViewGroup.LayoutParams params = view.getLayoutParams(); + // Initialize if empty. + if (params == null) { + params = new CommonLayoutParams(); + } + + // Set margins only if params are of the correct type. + if (params instanceof ViewGroup.MarginLayoutParams) { + ViewGroup.MarginLayoutParams lp = (ViewGroup.MarginLayoutParams) params; + lp.leftMargin = value; view.setLayoutParams(params); } - public static int getHeight(android.view.View view) { - ViewGroup.LayoutParams params = view.getLayoutParams(); - if (params != null) { - return params.height; - } - - return ViewGroup.LayoutParams.MATCH_PARENT; + if (params instanceof CommonLayoutParams) { + CommonLayoutParams lp = (CommonLayoutParams) params; + lp.leftMarginPercent = -1; + view.setLayoutParams(params); } - public static void setHeight(android.view.View view, int value) { - ViewGroup.LayoutParams params = view.getLayoutParams(); - if (params == null) { - params = new CommonLayoutParams(); - params.height = value; - } + } + public static void setMarginLeftPercent(android.view.View view, int value) { + ViewGroup.LayoutParams params = view.getLayoutParams(); + if (params == null) { + CommonLayoutParams lp = new CommonLayoutParams(); + } + + if (params instanceof CommonLayoutParams) { + CommonLayoutParams lp = (CommonLayoutParams) params; + lp.leftMargin = 0; + lp.leftMarginPercent = value; + view.setLayoutParams(params); + } + } + + public static int getMarginTop(android.view.View view) { + ViewGroup.LayoutParams params = view.getLayoutParams(); + if (params instanceof ViewGroup.MarginLayoutParams) { + ViewGroup.MarginLayoutParams lp = (ViewGroup.MarginLayoutParams) params; + return lp.topMargin; + } + + return 0; + } + + public static void setMarginTop(android.view.View view, int value) { + ViewGroup.LayoutParams params = view.getLayoutParams(); + // Initialize if empty. + if (params == null) { + params = new CommonLayoutParams(); + } + + // Set margins only if params are of the correct type. + if (params instanceof ViewGroup.MarginLayoutParams) { + ViewGroup.MarginLayoutParams lp = (ViewGroup.MarginLayoutParams) params; + lp.topMargin = value; view.setLayoutParams(params); } - public static Rect getMargin(android.view.View view) { - ViewGroup.LayoutParams params = view.getLayoutParams(); - if (params instanceof ViewGroup.MarginLayoutParams) { - ViewGroup.MarginLayoutParams lp = (ViewGroup.MarginLayoutParams) params; - return new Rect(lp.leftMargin, lp.topMargin, lp.rightMargin, lp.bottomMargin); - } - - return new Rect(); + if (params instanceof CommonLayoutParams) { + CommonLayoutParams lp = (CommonLayoutParams) params; + lp.topMarginPercent = -1; + view.setLayoutParams(params); } - public static void setMargin(android.view.View view, int left, int top, int right, int bottom) { - ViewGroup.LayoutParams params = view.getLayoutParams(); - // Initialize if empty. - if (params == null) { - params = new CommonLayoutParams(); - } + } - // Set margins only if params are of the correct type. - if (params instanceof ViewGroup.MarginLayoutParams) { - ViewGroup.MarginLayoutParams lp = (ViewGroup.MarginLayoutParams) params; - lp.leftMargin = left; - lp.topMargin = top; - lp.rightMargin = right; - lp.bottomMargin = bottom; - view.setLayoutParams(params); - } + public static void setMarginTopPercent(android.view.View view, int value) { + ViewGroup.LayoutParams params = view.getLayoutParams(); + if (params == null) { + CommonLayoutParams lp = new CommonLayoutParams(); } - public static int getMarginLeft(android.view.View view) { - ViewGroup.LayoutParams params = view.getLayoutParams(); - if (params instanceof ViewGroup.MarginLayoutParams) { - ViewGroup.MarginLayoutParams lp = (ViewGroup.MarginLayoutParams) params; - return lp.leftMargin; - } - - return 0; + if (params instanceof CommonLayoutParams) { + CommonLayoutParams lp = (CommonLayoutParams) params; + lp.topMargin = 0; + lp.topMarginPercent = value; + view.setLayoutParams(params); } - public static void setMarginLeft(android.view.View view, int value) { - ViewGroup.LayoutParams params = view.getLayoutParams(); - // Initialize if empty. - if (params == null) { - params = new CommonLayoutParams(); - } + } - // Set margins only if params are of the correct type. - if (params instanceof ViewGroup.MarginLayoutParams) { - ViewGroup.MarginLayoutParams lp = (ViewGroup.MarginLayoutParams) params; - lp.leftMargin = value; - view.setLayoutParams(params); - } + public static int getMarginRight(android.view.View view) { + ViewGroup.LayoutParams params = view.getLayoutParams(); + if (params instanceof ViewGroup.MarginLayoutParams) { + ViewGroup.MarginLayoutParams lp = (ViewGroup.MarginLayoutParams) params; + return lp.rightMargin; } - public static int getMarginTop(android.view.View view) { - ViewGroup.LayoutParams params = view.getLayoutParams(); - if (params instanceof ViewGroup.MarginLayoutParams) { - ViewGroup.MarginLayoutParams lp = (ViewGroup.MarginLayoutParams) params; - return lp.topMargin; - } + return 0; + } - return 0; - } - public static void setMarginTop(android.view.View view, int value) { - ViewGroup.LayoutParams params = view.getLayoutParams(); - // Initialize if empty. - if (params == null) { - params = new CommonLayoutParams(); - } - - // Set margins only if params are of the correct type. - if (params instanceof ViewGroup.MarginLayoutParams) { - ViewGroup.MarginLayoutParams lp = (ViewGroup.MarginLayoutParams) params; - lp.topMargin = value; - view.setLayoutParams(params); - } + public static void setMarginRight(android.view.View view, int value) { + ViewGroup.LayoutParams params = view.getLayoutParams(); + // Initialize if empty. + if (params == null) { + params = new CommonLayoutParams(); } - public static int getMarginRight(android.view.View view) { - ViewGroup.LayoutParams params = view.getLayoutParams(); - if (params instanceof ViewGroup.MarginLayoutParams) { - ViewGroup.MarginLayoutParams lp = (ViewGroup.MarginLayoutParams) params; - return lp.rightMargin; - } - - return 0; - } - public static void setMarginRight(android.view.View view, int value) { - ViewGroup.LayoutParams params = view.getLayoutParams(); - // Initialize if empty. - if (params == null) { - params = new CommonLayoutParams(); - } - - // Set margins only if params are of the correct type. - if (params instanceof ViewGroup.MarginLayoutParams) { - ViewGroup.MarginLayoutParams lp = (ViewGroup.MarginLayoutParams) params; - lp.rightMargin = value; - view.setLayoutParams(params); - } + // Set margins only if params are of the correct type. + if (params instanceof ViewGroup.MarginLayoutParams) { + ViewGroup.MarginLayoutParams lp = (ViewGroup.MarginLayoutParams) params; + lp.rightMargin = value; + view.setLayoutParams(params); } - public static int getMarginBottom(android.view.View view) { - ViewGroup.LayoutParams params = view.getLayoutParams(); - if (params instanceof ViewGroup.MarginLayoutParams) { - ViewGroup.MarginLayoutParams lp = (ViewGroup.MarginLayoutParams) params; - return lp.bottomMargin; - } - - return 0; + if (params instanceof CommonLayoutParams) { + CommonLayoutParams lp = (CommonLayoutParams) params; + lp.rightMarginPercent = -1; + view.setLayoutParams(params); } - public static void setMarginBottom(android.view.View view, int value) { - ViewGroup.LayoutParams params = view.getLayoutParams(); - // Initialize if empty. - if (params == null) { - params = new CommonLayoutParams(); - } + } - // Set margins only if params are of the correct type. - if (params instanceof ViewGroup.MarginLayoutParams) { - ViewGroup.MarginLayoutParams lp = (ViewGroup.MarginLayoutParams) params; - lp.bottomMargin = value; - view.setLayoutParams(params); - } + public static void setMarginRightPercent(android.view.View view, int value) { + ViewGroup.LayoutParams params = view.getLayoutParams(); + if (params == null) { + CommonLayoutParams lp = new CommonLayoutParams(); } - public static String getHorizontalAlighment(android.view.View view) { - ViewGroup.LayoutParams params = view.getLayoutParams(); - if (params instanceof FrameLayout.LayoutParams) { - FrameLayout.LayoutParams lp = (FrameLayout.LayoutParams) params; - if (Gravity.isHorizontal(lp.gravity)) { - switch (lp.gravity & Gravity.HORIZONTAL_GRAVITY_MASK) { - case Gravity.LEFT: - return "left"; - case Gravity.CENTER: - return "center"; - case Gravity.RIGHT: - return "right"; - case Gravity.FILL_HORIZONTAL: - return "stretch"; + if (params instanceof CommonLayoutParams) { + CommonLayoutParams lp = (CommonLayoutParams) params; + lp.rightMargin = 0; + lp.rightMarginPercent = value; + view.setLayoutParams(params); + } + } + + public static int getMarginBottom(android.view.View view) { + ViewGroup.LayoutParams params = view.getLayoutParams(); + if (params instanceof ViewGroup.MarginLayoutParams) { + ViewGroup.MarginLayoutParams lp = (ViewGroup.MarginLayoutParams) params; + return lp.bottomMargin; + } + + return 0; + } + + public static void setMarginBottom(android.view.View view, int value) { + ViewGroup.LayoutParams params = view.getLayoutParams(); + // Initialize if empty. + if (params == null) { + params = new CommonLayoutParams(); + } + + // Set margins only if params are of the correct type. + if (params instanceof ViewGroup.MarginLayoutParams) { + ViewGroup.MarginLayoutParams lp = (ViewGroup.MarginLayoutParams) params; + lp.bottomMargin = value; + view.setLayoutParams(params); + } + + if (params instanceof CommonLayoutParams) { + CommonLayoutParams lp = (CommonLayoutParams) params; + lp.bottomMarginPercent = -1; + view.setLayoutParams(params); + } + } + + public static void setMarginBottomPercent(android.view.View view, int value) { + ViewGroup.LayoutParams params = view.getLayoutParams(); + if (params == null) { + CommonLayoutParams lp = new CommonLayoutParams(); + } + + if (params instanceof CommonLayoutParams) { + CommonLayoutParams lp = (CommonLayoutParams) params; + lp.bottomMargin = 0; + lp.bottomMarginPercent = value; + view.setLayoutParams(params); + } + } + + public static String getHorizontalAlignment(android.view.View view) { + ViewGroup.LayoutParams params = view.getLayoutParams(); + if (params instanceof FrameLayout.LayoutParams) { + FrameLayout.LayoutParams lp = (FrameLayout.LayoutParams) params; + if (Gravity.isHorizontal(lp.gravity)) { + switch (lp.gravity & Gravity.HORIZONTAL_GRAVITY_MASK) { + case Gravity.LEFT: + return "left"; + case Gravity.CENTER: + return "center"; + case Gravity.RIGHT: + return "right"; + case Gravity.FILL_HORIZONTAL: + return "stretch"; - } } } - - return "stretch"; - } - public static void setHorizontalAlighment(android.view.View view, String value) { - ViewGroup.LayoutParams params = view.getLayoutParams(); - // Initialize if empty. - if (params == null) { - params = new CommonLayoutParams(); - } - - // Set margins only if params are of the correct type. - if (params instanceof FrameLayout.LayoutParams) { - FrameLayout.LayoutParams lp = (FrameLayout.LayoutParams) params; - switch (value) { - case "left": - lp.gravity = Gravity.LEFT | (lp.gravity & Gravity.VERTICAL_GRAVITY_MASK); - break; - case "center": - lp.gravity = Gravity.CENTER_HORIZONTAL | (lp.gravity & Gravity.VERTICAL_GRAVITY_MASK); - break; - case "right": - lp.gravity = Gravity.RIGHT | (lp.gravity & Gravity.VERTICAL_GRAVITY_MASK); - break; - case "stretch": - lp.gravity = Gravity.FILL_HORIZONTAL | (lp.gravity & Gravity.VERTICAL_GRAVITY_MASK); - break; - } - view.setLayoutParams(params); - } } - public static String getVerticalAlignment(android.view.View view) { - ViewGroup.LayoutParams params = view.getLayoutParams(); - if (params instanceof FrameLayout.LayoutParams) { - FrameLayout.LayoutParams lp = (FrameLayout.LayoutParams) params; - if (Gravity.isHorizontal(lp.gravity)) { - switch (lp.gravity & Gravity.VERTICAL_GRAVITY_MASK) { - case Gravity.TOP: - return "top"; - case Gravity.CENTER: - return "center"; - case Gravity.BOTTOM: - return "bottom"; - case Gravity.FILL_VERTICAL: - return "stretch"; + return "stretch"; + } + + public static void setHorizontalAlignment(android.view.View view, String value) { + ViewGroup.LayoutParams params = view.getLayoutParams(); + // Initialize if empty. + if (params == null) { + params = new CommonLayoutParams(); + } + + // Set margins only if params are of the correct type. + if (params instanceof FrameLayout.LayoutParams) { + FrameLayout.LayoutParams lp = (FrameLayout.LayoutParams) params; + switch (value) { + case "left": + lp.gravity = Gravity.LEFT | (lp.gravity & Gravity.VERTICAL_GRAVITY_MASK); + break; + case "center": + case "middle": + lp.gravity = Gravity.CENTER_HORIZONTAL | (lp.gravity & Gravity.VERTICAL_GRAVITY_MASK); + break; + case "right": + lp.gravity = Gravity.RIGHT | (lp.gravity & Gravity.VERTICAL_GRAVITY_MASK); + break; + case "stretch": + lp.gravity = Gravity.FILL_HORIZONTAL | (lp.gravity & Gravity.VERTICAL_GRAVITY_MASK); + break; + } + view.setLayoutParams(params); + } + } + + public static String getVerticalAlignment(android.view.View view) { + ViewGroup.LayoutParams params = view.getLayoutParams(); + if (params instanceof FrameLayout.LayoutParams) { + FrameLayout.LayoutParams lp = (FrameLayout.LayoutParams) params; + if (Gravity.isHorizontal(lp.gravity)) { + switch (lp.gravity & Gravity.VERTICAL_GRAVITY_MASK) { + case Gravity.TOP: + return "top"; + case Gravity.CENTER: + return "center"; + case Gravity.BOTTOM: + return "bottom"; + case Gravity.FILL_VERTICAL: + return "stretch"; - } } } - - return "stretch"; } - public static void setVerticalAlignment(android.view.View view, String value){ - ViewGroup.LayoutParams params = view.getLayoutParams(); - // Initialize if empty. - if (params == null) { - params = new CommonLayoutParams(); + + return "stretch"; + } + + public static void setVerticalAlignment(android.view.View view, String value) { + ViewGroup.LayoutParams params = view.getLayoutParams(); + // Initialize if empty. + if (params == null) { + params = new CommonLayoutParams(); + } + + // Set margins only if params are of the correct type. + if (params instanceof FrameLayout.LayoutParams) { + FrameLayout.LayoutParams lp = (FrameLayout.LayoutParams) params; + switch (value) { + case "top": + lp.gravity = Gravity.TOP | (lp.gravity & Gravity.HORIZONTAL_GRAVITY_MASK); + break; + case "center": + case "middle": + lp.gravity = Gravity.CENTER_VERTICAL | (lp.gravity & Gravity.HORIZONTAL_GRAVITY_MASK); + break; + case "bottom": + lp.gravity = Gravity.BOTTOM | (lp.gravity & Gravity.HORIZONTAL_GRAVITY_MASK); + break; + case "stretch": + lp.gravity = Gravity.FILL_VERTICAL | (lp.gravity & Gravity.HORIZONTAL_GRAVITY_MASK); + break; } + view.setLayoutParams(params); + } + } - // Set margins only if params are of the correct type. - if (params instanceof FrameLayout.LayoutParams) { - FrameLayout.LayoutParams lp = (FrameLayout.LayoutParams) params; - switch (value) { - case "top": - lp.gravity = Gravity.TOP | (lp.gravity & Gravity.HORIZONTAL_GRAVITY_MASK); - break; - case "center": - lp.gravity = Gravity.CENTER_VERTICAL | (lp.gravity & Gravity.HORIZONTAL_GRAVITY_MASK); - break; - case "bottom": - lp.gravity = Gravity.BOTTOM| (lp.gravity & Gravity.HORIZONTAL_GRAVITY_MASK); - break; - case "stretch": - lp.gravity = Gravity.FILL_VERTICAL | (lp.gravity & Gravity.HORIZONTAL_GRAVITY_MASK); - break; - } - view.setLayoutParams(params); - } + public static Rect getPadding(android.view.View view) { + return new Rect(view.getPaddingLeft(), view.getPaddingTop(), view.getPaddingRight(), view.getPaddingBottom()); + } + + public static void setPadding(android.view.View view, int left, int top, int right, int bottom) { + view.setPadding(left, top, right, bottom); + } + + public static int getPaddingLeft(android.view.View view) { + return view.getPaddingLeft(); + } + + public static void setPaddingLeft(android.view.View view, int value) { + view.setPadding(value, view.getPaddingTop(), view.getPaddingRight(), view.getPaddingBottom()); + } + + public static int getPaddingTop(android.view.View view) { + return view.getPaddingTop(); + } + + public static void setPaddingTop(android.view.View view, int value) { + view.setPadding(view.getPaddingLeft(), value, view.getPaddingRight(), view.getPaddingBottom()); + } + + public static int getPaddingRight(android.view.View view) { + return view.getPaddingRight(); + } + + public static void setPaddingRight(android.view.View view, int value) { + view.setPadding(view.getPaddingLeft(), view.getPaddingTop(), value, view.getPaddingBottom()); + } + + public static int getPaddingBottom(android.view.View view) { + return view.getPaddingBottom(); + } + + public static void setPaddingBottom(android.view.View view, int value) { + view.setPadding(view.getPaddingLeft(), view.getPaddingTop(), view.getPaddingRight(), value); + } + + public static float getRotate(android.view.View view) { + return view.getRotation(); + } + + public static void setRotate(android.view.View view, float value) { + view.setRotation(value); + } + + public static float getScaleX(android.view.View view) { + return view.getScaleX(); + } + + public static void setScaleX(android.view.View view, float value) { + view.setScaleX(value); + } + + public static float getScaleY(android.view.View view) { + return view.getScaleY(); + } + + public static void setScaleY(android.view.View view, float value) { + view.setScaleY(value); + } + + public static float getTranslateX(android.view.View view) { + return view.getTranslationX(); + } + + public static void setTranslateX(android.view.View view, float value) { + view.setTranslationX(value); + } + + public static float getTranslateY(android.view.View view) { + return view.getTranslationY(); + } + + public static void setTranslateY(android.view.View view, float value) { + view.setTranslationY(value); + } + + @TargetApi(21) + public static float getZIndex(android.view.View view) { + if (ViewHelper.version >= 21) { + return view.getZ(); } - public static Rect getPadding(android.view.View view) { - return new Rect(view.getPaddingLeft(), view.getPaddingTop(), view.getPaddingRight(), view.getPaddingBottom()); + return 0; + } + + @TargetApi(21) + public static void setZIndex(android.view.View view, float value) { + if (ViewHelper.version >= 21) { + view.setZ(value); } - public static void setPadding(android.view.View view, int left, int top, int right, int bottom) { - view.setPadding(left, top, right, bottom); + } + + @TargetApi(21) + public static float getLetterspacing(android.widget.TextView textView) { + if (ViewHelper.version >= 21) { + return textView.getLetterSpacing(); } - public static int getPaddingLeft(android.view.View view) { - return view.getPaddingLeft(); - } - public static void setPaddingLeft(android.view.View view, int value) { - view.setPadding(value, view.getPaddingTop(), view.getPaddingRight(), view.getPaddingBottom()); - } + return 0; + } - public static int getPaddingTop(android.view.View view) { - return view.getPaddingTop(); - } - public static void setPaddingTop(android.view.View view, int value) { - view.setPadding(view.getPaddingLeft(), value, view.getPaddingRight(), view.getPaddingBottom()); - } - - public static int getPaddingRight(android.view.View view) { - return view.getPaddingRight(); - } - public static void setPaddingRight(android.view.View view, int value) { - view.setPadding(view.getPaddingLeft(), view.getPaddingTop(), value, view.getPaddingBottom()); - } - - public static int getPaddingBottom(android.view.View view) { - return view.getPaddingBottom(); - } - public static void setPaddingBottom(android.view.View view, int value) { - view.setPadding(view.getPaddingLeft(), view.getPaddingTop(), view.getPaddingRight(), value); - } - - public static float getRotate(android.view.View view) { - return view.getRotation(); - } - public static void setRotate(android.view.View view, float value) { - view.setRotation(value); - } - - public static float getScaleX(android.view.View view) { - return view.getScaleX(); - } - public static void setScaleX(android.view.View view, float value) { - view.setScaleX(value); - } - - public static float getScaleY(android.view.View view) { - return view.getScaleY(); - } - public static void setScaleY(android.view.View view, float value) { - view.setScaleY(value); - } - - public static float getTranslateX(android.view.View view) { - return view.getTranslationX(); - } - public static void setTranslateX(android.view.View view, float value) { - view.setTranslationX(value); - } - - public static float getTranslateY(android.view.View view) { - return view.getTranslationY(); - } - public static void setTranslateY(android.view.View view, float value) { - view.setTranslationY(value); - } - - @TargetApi(21) - public static float getZIndex(android.view.View view) { - if (ViewHelper.version >= 21) { - return view.getZ(); - } - - return 0; - } - - @TargetApi(21) - public static void setZIndex(android.view.View view, float value) { - if (ViewHelper.version >= 21) { - view.setZ(value); - } - } - - @TargetApi(21) - public static float getLetterspacing(android.widget.TextView textView) { - if (ViewHelper.version >= 21) { - return textView.getLetterSpacing(); - } - - return 0; - } - - @TargetApi(21) - public static void setLetterspacing(android.widget.TextView textView, float value) { - if (ViewHelper.version >= 21) { - textView.setLetterSpacing(value); - } + @TargetApi(21) + public static void setLetterspacing(android.widget.TextView textView, float value) { + if (ViewHelper.version >= 21) { + textView.setLetterSpacing(value); } + } } \ No newline at end of file From a9aa5f45e184bdc0a2d3591994e74173c7995a78 Mon Sep 17 00:00:00 2001 From: Hristo Hristov Date: Fri, 16 Dec 2016 17:59:53 +0200 Subject: [PATCH 2/5] BorderDrawable density fixed to 1. It will be set through JS. --- .../main/java/org/nativescript/widgets/BorderDrawable.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/android/widgets/src/main/java/org/nativescript/widgets/BorderDrawable.java b/android/widgets/src/main/java/org/nativescript/widgets/BorderDrawable.java index aa6e03f8e..cac506bee 100644 --- a/android/widgets/src/main/java/org/nativescript/widgets/BorderDrawable.java +++ b/android/widgets/src/main/java/org/nativescript/widgets/BorderDrawable.java @@ -175,12 +175,12 @@ public class BorderDrawable extends ColorDrawable { public BorderDrawable(float density){ super(); - this.density = density; + this.density = 1f; } public BorderDrawable(float density, String id){ super(); - this.density = density; + this.density = 1f; this.id = id; } From e443f1cf4e170fa94164f7eb286049ed3502a6b9 Mon Sep 17 00:00:00 2001 From: vakrilov Date: Thu, 22 Dec 2016 13:24:49 +0200 Subject: [PATCH 3/5] Fixed setWidth/setHeight methods --- .../src/main/java/org/nativescript/widgets/ViewHelper.java | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/android/widgets/src/main/java/org/nativescript/widgets/ViewHelper.java b/android/widgets/src/main/java/org/nativescript/widgets/ViewHelper.java index 4f4946c33..3f47551f1 100644 --- a/android/widgets/src/main/java/org/nativescript/widgets/ViewHelper.java +++ b/android/widgets/src/main/java/org/nativescript/widgets/ViewHelper.java @@ -46,8 +46,9 @@ public class ViewHelper { ViewGroup.LayoutParams params = view.getLayoutParams(); if (params == null) { params = new CommonLayoutParams(); - params.width = value; } + + params.width = value; view.setLayoutParams(params); } @@ -79,8 +80,9 @@ public class ViewHelper { ViewGroup.LayoutParams params = view.getLayoutParams(); if (params == null) { params = new CommonLayoutParams(); - params.height = value; } + + params.height = value; view.setLayoutParams(params); } From cbc2497a533fe9924fd1dc92f3954d3770a5af8a Mon Sep 17 00:00:00 2001 From: Panayot Cankov Date: Thu, 29 Dec 2016 11:49:30 +0200 Subject: [PATCH 4/5] Set percent should use float instead of int values, setting height or width should reset theightPercent and widthPercent --- .../org/nativescript/widgets/ViewHelper.java | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) diff --git a/android/widgets/src/main/java/org/nativescript/widgets/ViewHelper.java b/android/widgets/src/main/java/org/nativescript/widgets/ViewHelper.java index 3f47551f1..d1695f057 100644 --- a/android/widgets/src/main/java/org/nativescript/widgets/ViewHelper.java +++ b/android/widgets/src/main/java/org/nativescript/widgets/ViewHelper.java @@ -49,11 +49,14 @@ public class ViewHelper { } params.width = value; + if (params instanceof CommonLayoutParams) { + ((CommonLayoutParams)params).widthPercent = -1; + } view.setLayoutParams(params); } - public static void setWidthPercent(android.view.View view, int value) { + public static void setWidthPercent(android.view.View view, float value) { ViewGroup.LayoutParams params = view.getLayoutParams(); if (params == null) { CommonLayoutParams lp = new CommonLayoutParams(); @@ -83,11 +86,14 @@ public class ViewHelper { } params.height = value; + if (params instanceof CommonLayoutParams) { + ((CommonLayoutParams)params).heightPercent = -1; + } view.setLayoutParams(params); } - public static void setHeightPercent(android.view.View view, int value) { + public static void setHeightPercent(android.view.View view, float value) { ViewGroup.LayoutParams params = view.getLayoutParams(); if (params == null) { CommonLayoutParams lp = new CommonLayoutParams(); @@ -160,7 +166,7 @@ public class ViewHelper { } } - public static void setMarginLeftPercent(android.view.View view, int value) { + public static void setMarginLeftPercent(android.view.View view, float value) { ViewGroup.LayoutParams params = view.getLayoutParams(); if (params == null) { CommonLayoutParams lp = new CommonLayoutParams(); @@ -205,7 +211,7 @@ public class ViewHelper { } } - public static void setMarginTopPercent(android.view.View view, int value) { + public static void setMarginTopPercent(android.view.View view, float value) { ViewGroup.LayoutParams params = view.getLayoutParams(); if (params == null) { CommonLayoutParams lp = new CommonLayoutParams(); @@ -250,7 +256,7 @@ public class ViewHelper { } } - public static void setMarginRightPercent(android.view.View view, int value) { + public static void setMarginRightPercent(android.view.View view, float value) { ViewGroup.LayoutParams params = view.getLayoutParams(); if (params == null) { CommonLayoutParams lp = new CommonLayoutParams(); @@ -295,7 +301,7 @@ public class ViewHelper { } } - public static void setMarginBottomPercent(android.view.View view, int value) { + public static void setMarginBottomPercent(android.view.View view, float value) { ViewGroup.LayoutParams params = view.getLayoutParams(); if (params == null) { CommonLayoutParams lp = new CommonLayoutParams(); From d015487e6c63ba96ef0c016e92201eccb409995b Mon Sep 17 00:00:00 2001 From: Panayot Cankov Date: Wed, 4 Jan 2017 17:01:00 +0200 Subject: [PATCH 5/5] Add layout params copy constructors --- .../widgets/CommonLayoutParams.java | 33 ++++++++++++++ .../nativescript/widgets/FlexboxLayout.java | 43 ++++++++++++++++++- .../widgets/HorizontalScrollView.java | 41 +++++++++++++++++- .../org/nativescript/widgets/LayoutBase.java | 13 +++++- .../widgets/VerticalScrollView.java | 40 ++++++++++++++++- 5 files changed, 163 insertions(+), 7 deletions(-) diff --git a/android/widgets/src/main/java/org/nativescript/widgets/CommonLayoutParams.java b/android/widgets/src/main/java/org/nativescript/widgets/CommonLayoutParams.java index 4020e9f3f..7c3b6c95f 100644 --- a/android/widgets/src/main/java/org/nativescript/widgets/CommonLayoutParams.java +++ b/android/widgets/src/main/java/org/nativescript/widgets/CommonLayoutParams.java @@ -29,6 +29,39 @@ public class CommonLayoutParams extends FrameLayout.LayoutParams { super(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT, Gravity.FILL); } + public CommonLayoutParams(ViewGroup.LayoutParams source) { + super(source); + } + + public CommonLayoutParams(ViewGroup.MarginLayoutParams source) { + super(source); + } + + public CommonLayoutParams(FrameLayout.LayoutParams source) { + super((ViewGroup.MarginLayoutParams) source); + this.gravity = source.gravity; + } + + public CommonLayoutParams(CommonLayoutParams source) { + this((FrameLayout.LayoutParams) source); + + this.widthPercent = source.widthPercent; + this.heightPercent = source.heightPercent; + + this.topMargin = source.topMargin; + this.leftMargin = source.leftMargin; + this.bottomMargin = source.bottomMargin; + this.rightMargin = source.rightMargin; + + this.left = source.left; + this.top = source.top; + this.row = source.row; + this.column = source.column; + this.rowSpan = source.rowSpan; + this.columnSpan = source.columnSpan; + this.dock = source.dock; + } + public float widthPercent = 0; public float heightPercent = 0; diff --git a/android/widgets/src/main/java/org/nativescript/widgets/FlexboxLayout.java b/android/widgets/src/main/java/org/nativescript/widgets/FlexboxLayout.java index 9dab07605..e11cbaa68 100644 --- a/android/widgets/src/main/java/org/nativescript/widgets/FlexboxLayout.java +++ b/android/widgets/src/main/java/org/nativescript/widgets/FlexboxLayout.java @@ -27,6 +27,7 @@ import android.util.AttributeSet; import android.util.SparseIntArray; import android.view.View; import android.view.ViewGroup; +import android.widget.FrameLayout; import android.widget.LinearLayout; import android.widget.RelativeLayout; @@ -2221,8 +2222,20 @@ public class FlexboxLayout extends ViewGroup { } @Override - protected ViewGroup.LayoutParams generateLayoutParams(ViewGroup.LayoutParams p) { - return new FlexboxLayout.LayoutParams(); + protected ViewGroup.LayoutParams generateLayoutParams(ViewGroup.LayoutParams from) { + if (from instanceof FlexboxLayout.LayoutParams) + return new FlexboxLayout.LayoutParams((FlexboxLayout.LayoutParams)from); + + if (from instanceof CommonLayoutParams) + return new FlexboxLayout.LayoutParams((CommonLayoutParams)from); + + if (from instanceof FrameLayout.LayoutParams) + return new FlexboxLayout.LayoutParams((FrameLayout.LayoutParams)from); + + if (from instanceof ViewGroup.MarginLayoutParams) + return new FlexboxLayout.LayoutParams((ViewGroup.MarginLayoutParams)from); + + return new FlexboxLayout.LayoutParams(from); } @FlexDirection @@ -2631,6 +2644,32 @@ public class FlexboxLayout extends ViewGroup { public LayoutParams() { super(); } + + public LayoutParams(ViewGroup.LayoutParams source) { + super(source); + } + + public LayoutParams(ViewGroup.MarginLayoutParams source) { + super(source); + } + + public LayoutParams(FrameLayout.LayoutParams source) { + super(source); + } + + public LayoutParams(CommonLayoutParams source) { + super(source); + } + + public LayoutParams(LayoutParams source) { + super(source); + + this.order = source.order; + this.flexGrow = source.flexGrow; + this.flexShrink = source.flexShrink; + this.wrapBefore = source.wrapBefore; + this.alignSelf = source.alignSelf; + } } /** diff --git a/android/widgets/src/main/java/org/nativescript/widgets/HorizontalScrollView.java b/android/widgets/src/main/java/org/nativescript/widgets/HorizontalScrollView.java index fa3af6f08..2fb668c45 100644 --- a/android/widgets/src/main/java/org/nativescript/widgets/HorizontalScrollView.java +++ b/android/widgets/src/main/java/org/nativescript/widgets/HorizontalScrollView.java @@ -7,9 +7,11 @@ import android.content.Context; import android.graphics.Rect; import android.os.Parcel; import android.os.Parcelable; +import android.util.AttributeSet; import android.view.View; import android.view.ViewGroup; import android.view.ViewParent; +import android.widget.FrameLayout; /** * @author hhristov @@ -51,7 +53,42 @@ public class HorizontalScrollView extends android.widget.HorizontalScrollView { this.mIsLayoutDirty = true; super.requestLayout(); } - + + @Override + protected CommonLayoutParams generateDefaultLayoutParams() { + return new CommonLayoutParams(); + } + + /** + * {@inheritDoc} + */ + @Override + public CommonLayoutParams generateLayoutParams(AttributeSet attrs) { + return new CommonLayoutParams(); + } + + /** + * {@inheritDoc} + */ + @Override + protected boolean checkLayoutParams(ViewGroup.LayoutParams p) { + return p instanceof CommonLayoutParams; + } + + @Override + protected ViewGroup.LayoutParams generateLayoutParams(ViewGroup.LayoutParams from) { + if (from instanceof CommonLayoutParams) + return new CommonLayoutParams((CommonLayoutParams)from); + + if (from instanceof FrameLayout.LayoutParams) + return new CommonLayoutParams((FrameLayout.LayoutParams)from); + + if (from instanceof ViewGroup.MarginLayoutParams) + return new CommonLayoutParams((ViewGroup.MarginLayoutParams)from); + + return new CommonLayoutParams(from); + } + @Override public void requestChildFocus(View child, View focused) { if (!mIsLayoutDirty) { @@ -179,7 +216,7 @@ public class HorizontalScrollView extends android.widget.HorizontalScrollView { ss.isLayoutRtl = this.isLayoutRtl(); return ss; } - + private void scrollToChild(View child) { child.getDrawingRect(mTempRect); diff --git a/android/widgets/src/main/java/org/nativescript/widgets/LayoutBase.java b/android/widgets/src/main/java/org/nativescript/widgets/LayoutBase.java index 39e235209..f011a4b48 100644 --- a/android/widgets/src/main/java/org/nativescript/widgets/LayoutBase.java +++ b/android/widgets/src/main/java/org/nativescript/widgets/LayoutBase.java @@ -42,8 +42,17 @@ public abstract class LayoutBase extends ViewGroup { } @Override - protected LayoutParams generateLayoutParams(LayoutParams p) { - return new CommonLayoutParams(); + protected ViewGroup.LayoutParams generateLayoutParams(ViewGroup.LayoutParams from) { + if (from instanceof CommonLayoutParams) + return new CommonLayoutParams((CommonLayoutParams)from); + + if (from instanceof FrameLayout.LayoutParams) + return new CommonLayoutParams((FrameLayout.LayoutParams)from); + + if (from instanceof ViewGroup.MarginLayoutParams) + return new CommonLayoutParams((ViewGroup.MarginLayoutParams)from); + + return new CommonLayoutParams(from); } @Override diff --git a/android/widgets/src/main/java/org/nativescript/widgets/VerticalScrollView.java b/android/widgets/src/main/java/org/nativescript/widgets/VerticalScrollView.java index 7e7e9ea20..89cd34df7 100644 --- a/android/widgets/src/main/java/org/nativescript/widgets/VerticalScrollView.java +++ b/android/widgets/src/main/java/org/nativescript/widgets/VerticalScrollView.java @@ -7,7 +7,10 @@ import org.nativescript.widgets.HorizontalScrollView.SavedState; import android.content.Context; import android.graphics.Rect; import android.os.Parcelable; +import android.util.AttributeSet; import android.view.View; +import android.view.ViewGroup; +import android.widget.FrameLayout; import android.widget.ScrollView; /** @@ -50,7 +53,42 @@ public class VerticalScrollView extends ScrollView { this.mIsLayoutDirty = true; super.requestLayout(); } - + + @Override + protected CommonLayoutParams generateDefaultLayoutParams() { + return new CommonLayoutParams(); + } + + /** + * {@inheritDoc} + */ + @Override + public CommonLayoutParams generateLayoutParams(AttributeSet attrs) { + return new CommonLayoutParams(); + } + + /** + * {@inheritDoc} + */ + @Override + protected boolean checkLayoutParams(ViewGroup.LayoutParams p) { + return p instanceof CommonLayoutParams; + } + + @Override + protected ViewGroup.LayoutParams generateLayoutParams(ViewGroup.LayoutParams from) { + if (from instanceof CommonLayoutParams) + return new CommonLayoutParams((CommonLayoutParams)from); + + if (from instanceof FrameLayout.LayoutParams) + return new CommonLayoutParams((FrameLayout.LayoutParams)from); + + if (from instanceof ViewGroup.MarginLayoutParams) + return new CommonLayoutParams((ViewGroup.MarginLayoutParams)from); + + return new CommonLayoutParams(from); + } + @Override public void requestChildFocus(View child, View focused) { if (!this.mIsLayoutDirty) {