android LayoutParams are not overridden (#2383)

* LayoutParams are no longer overriden with CommonLayoutParams

* Small code refactoring to get intellisense
This commit is contained in:
Hristo Hristov
2016-06-29 17:13:37 +03:00
committed by GitHub
parent ffb82e250a
commit bc1224e351
5 changed files with 102 additions and 72 deletions

View File

@ -238,7 +238,7 @@ export class View extends viewCommon.View {
} }
this._createUI(); this._createUI();
// Ensure layout params // Ensure layout params
if (this._nativeView && !(this._nativeView.getLayoutParams() instanceof org.nativescript.widgets.CommonLayoutParams)) { if (this._nativeView && !this._nativeView.getLayoutParams()) {
this._nativeView.setLayoutParams(new org.nativescript.widgets.CommonLayoutParams()); this._nativeView.setLayoutParams(new org.nativescript.widgets.CommonLayoutParams());
} }
@ -500,34 +500,11 @@ export class ViewStyler implements style.Styler {
(<android.view.View>view._nativeView).setMinimumHeight(0); (<android.view.View>view._nativeView).setMinimumHeight(0);
} }
private static getNativeLayoutParams(nativeView: android.view.View): org.nativescript.widgets.CommonLayoutParams {
var lp = <org.nativescript.widgets.CommonLayoutParams>nativeView.getLayoutParams();
if (!(lp instanceof org.nativescript.widgets.CommonLayoutParams)) {
lp = new org.nativescript.widgets.CommonLayoutParams();
}
return lp;
}
private static setNativeLayoutParamsProperty(view: View, params: CommonLayoutParams): void { private static setNativeLayoutParamsProperty(view: View, params: CommonLayoutParams): void {
var nativeView: android.view.View = view._nativeView; let nativeView: android.view.View = view._nativeView;
var lp = ViewStyler.getNativeLayoutParams(nativeView);
lp.widthPercent = params.widthPercent; let width = params.width * utils.layout.getDisplayDensity();
lp.heightPercent = params.heightPercent; let height = params.height * utils.layout.getDisplayDensity();
lp.leftMarginPercent = params.leftMarginPercent;
lp.topMarginPercent = params.topMarginPercent;
lp.rightMarginPercent = params.rightMarginPercent;
lp.bottomMarginPercent = params.bottomMarginPercent;
lp.leftMargin = Math.round(params.leftMargin * utils.layout.getDisplayDensity());
lp.topMargin = Math.round(params.topMargin * utils.layout.getDisplayDensity());
lp.rightMargin = Math.round(params.rightMargin * utils.layout.getDisplayDensity());
lp.bottomMargin = Math.round(params.bottomMargin * utils.layout.getDisplayDensity());
var width = params.width * utils.layout.getDisplayDensity();
var height = params.height * utils.layout.getDisplayDensity();
// If width is not specified set it as WRAP_CONTENT // If width is not specified set it as WRAP_CONTENT
if (width < 0) { if (width < 0) {
@ -539,7 +516,7 @@ export class ViewStyler implements style.Styler {
height = -2; height = -2;
} }
var gravity = 0; let gravity = 0;
switch (params.horizontalAlignment) { switch (params.horizontalAlignment) {
case enums.HorizontalAlignment.left: case enums.HorizontalAlignment.left:
gravity |= android.view.Gravity.LEFT; gravity |= android.view.Gravity.LEFT;
@ -591,10 +568,70 @@ export class ViewStyler implements style.Styler {
throw new Error("Invalid verticalAlignment value: " + params.verticalAlignment); throw new Error("Invalid verticalAlignment value: " + params.verticalAlignment);
} }
lp.gravity = gravity; let lp = nativeView.getLayoutParams();
lp.width = Math.round(width); lp.width = Math.round(width);
lp.height = Math.round(height); lp.height = Math.round(height);
if (lp instanceof org.nativescript.widgets.CommonLayoutParams) {
lp.widthPercent = params.widthPercent;
lp.heightPercent = params.heightPercent;
lp.leftMarginPercent = params.leftMarginPercent;
lp.topMarginPercent = params.topMarginPercent;
lp.rightMarginPercent = params.rightMarginPercent;
lp.bottomMarginPercent = params.bottomMarginPercent;
lp.leftMargin = Math.round(params.leftMargin * utils.layout.getDisplayDensity());
lp.topMargin = Math.round(params.topMargin * utils.layout.getDisplayDensity());
lp.rightMargin = Math.round(params.rightMargin * utils.layout.getDisplayDensity());
lp.bottomMargin = Math.round(params.bottomMargin * utils.layout.getDisplayDensity());
lp.gravity = gravity;
}
else {
let layoutParams: any = lp;
if (types.isDefined(layoutParams.widthPercent)) {
layoutParams.widthPercent = params.widthPercent;
}
if (types.isDefined(layoutParams.heightPercent)) {
layoutParams.heightPercent = params.heightPercent;
}
if (types.isDefined(layoutParams.leftMarginPercent)) {
layoutParams.leftMarginPercent = params.leftMarginPercent;
}
if (types.isDefined(layoutParams.topMarginPercent)) {
layoutParams.topMarginPercent = params.topMarginPercent;
}
if (types.isDefined(layoutParams.rightMarginPercent)) {
layoutParams.rightMarginPercent = params.rightMarginPercent;
}
if (types.isDefined(layoutParams.bottomMarginPercent)) {
layoutParams.bottomMarginPercent = params.bottomMarginPercent;
}
if (types.isDefined(layoutParams.leftMargin)) {
layoutParams.leftMargin = Math.round(params.leftMargin * utils.layout.getDisplayDensity());
}
if (types.isDefined(layoutParams.topMargin)) {
layoutParams.topMargin = Math.round(params.topMargin * utils.layout.getDisplayDensity());
}
if (types.isDefined(layoutParams.rightMargin)) {
layoutParams.rightMargin = Math.round(params.rightMargin * utils.layout.getDisplayDensity());
}
if (types.isDefined(layoutParams.bottomMargin)) {
layoutParams.bottomMargin = Math.round(params.bottomMargin * utils.layout.getDisplayDensity());
}
if (types.isDefined(layoutParams.gravity)) {
layoutParams.gravity = gravity;
}
}
nativeView.setLayoutParams(lp); nativeView.setLayoutParams(lp);
} }

View File

@ -10,15 +10,13 @@ function setNativeProperty(data: PropertyChangeData, setter: (lp: org.nativescri
var view = data.object; var view = data.object;
if (view instanceof View) { if (view instanceof View) {
var nativeView: android.view.View = view._nativeView; var nativeView: android.view.View = view._nativeView;
var lp = nativeView.getLayoutParams() || new org.nativescript.widgets.CommonLayoutParams();
var lp = <org.nativescript.widgets.CommonLayoutParams>nativeView.getLayoutParams(); if (lp instanceof org.nativescript.widgets.CommonLayoutParams) {
if (!(lp instanceof org.nativescript.widgets.CommonLayoutParams)) {
lp = new org.nativescript.widgets.CommonLayoutParams();
}
setter(lp); setter(lp);
nativeView.setLayoutParams(lp); nativeView.setLayoutParams(lp);
} }
} }
}
function setNativeLeftProperty(data: PropertyChangeData) { function setNativeLeftProperty(data: PropertyChangeData) {
setNativeProperty(data, (lp) => { lp.left = data.newValue * utils.layout.getDisplayDensity(); }); setNativeProperty(data, (lp) => { lp.left = data.newValue * utils.layout.getDisplayDensity(); });

View File

@ -10,12 +10,8 @@ function setNativeDockProperty(data: PropertyChangeData) {
var view = data.object; var view = data.object;
if (view instanceof View) { if (view instanceof View) {
var nativeView: android.view.View = view._nativeView; var nativeView: android.view.View = view._nativeView;
var lp = nativeView.getLayoutParams() || new org.nativescript.widgets.CommonLayoutParams();
var lp = <org.nativescript.widgets.CommonLayoutParams>nativeView.getLayoutParams(); if (lp instanceof org.nativescript.widgets.CommonLayoutParams) {
if (!(lp instanceof org.nativescript.widgets.CommonLayoutParams)) {
lp = new org.nativescript.widgets.CommonLayoutParams();
}
switch (data.newValue) { switch (data.newValue) {
case Dock.left: case Dock.left:
lp.dock = org.nativescript.widgets.Dock.left; lp.dock = org.nativescript.widgets.Dock.left;
@ -36,6 +32,7 @@ function setNativeDockProperty(data: PropertyChangeData) {
nativeView.setLayoutParams(lp); nativeView.setLayoutParams(lp);
} }
} }
}
(<PropertyMetadata>common.DockLayout.dockProperty.metadata).onSetNativeValue = setNativeDockProperty; (<PropertyMetadata>common.DockLayout.dockProperty.metadata).onSetNativeValue = setNativeDockProperty;

View File

@ -10,15 +10,13 @@ function setNativeProperty(data: PropertyChangeData, setter: (lp: org.nativescri
let view = data.object; let view = data.object;
if (view instanceof View) { if (view instanceof View) {
let nativeView: android.view.View = view._nativeView; let nativeView: android.view.View = view._nativeView;
var lp = nativeView.getLayoutParams() || new org.nativescript.widgets.CommonLayoutParams();
let lp = <org.nativescript.widgets.CommonLayoutParams>nativeView.getLayoutParams(); if (lp instanceof org.nativescript.widgets.CommonLayoutParams) {
if (!(lp instanceof org.nativescript.widgets.CommonLayoutParams)) {
lp = new org.nativescript.widgets.CommonLayoutParams();
}
setter(lp); setter(lp);
nativeView.setLayoutParams(lp); nativeView.setLayoutParams(lp);
} }
} }
}
function setNativeRowProperty(data: PropertyChangeData) { function setNativeRowProperty(data: PropertyChangeData) {
setNativeProperty(data, (lp) => { lp.row = data.newValue; }); setNativeProperty(data, (lp) => { lp.row = data.newValue; });

View File

@ -228,7 +228,7 @@ export class TabView extends common.TabView {
} }
this._viewPager = new android.support.v4.view.ViewPager(this._context); this._viewPager = new android.support.v4.view.ViewPager(this._context);
var lp = new org.nativescript.widgets.CommonLayoutParams() var lp = new org.nativescript.widgets.CommonLayoutParams();
lp.row = 1; lp.row = 1;
this._viewPager.setLayoutParams(lp); this._viewPager.setLayoutParams(lp);
this._grid.addView(this._viewPager); this._grid.addView(this._viewPager);