Skip to content

Commit bc1224e

Browse files
author
Hristo Hristov
authored
android LayoutParams are not overridden (#2383)
* LayoutParams are no longer overriden with CommonLayoutParams * Small code refactoring to get intellisense
1 parent ffb82e2 commit bc1224e

File tree

5 files changed

+103
-73
lines changed

5 files changed

+103
-73
lines changed

tns-core-modules/ui/core/view.android.ts

Lines changed: 74 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ function onIsUserInteractionEnabledPropertyChanged(data: dependencyObservable.Pr
5353

5454
export class View extends viewCommon.View {
5555
private _disableUserInteractionListener: android.view.View.OnTouchListener = new android.view.View.OnTouchListener({
56-
onTouch: function(view: android.view.View, event: android.view.MotionEvent) {
56+
onTouch: function (view: android.view.View, event: android.view.MotionEvent) {
5757
return true;
5858
}
5959
});
@@ -116,7 +116,7 @@ export class View extends viewCommon.View {
116116
this._nativeView.setClickable(true);
117117
}
118118
this._nativeView.setOnTouchListener(new android.view.View.OnTouchListener({
119-
onTouch: function(view: android.view.View, motionEvent: android.view.MotionEvent) {
119+
onTouch: function (view: android.view.View, motionEvent: android.view.MotionEvent) {
120120
var owner = that.get();
121121
if (!owner) {
122122
return false;
@@ -178,7 +178,7 @@ export class View extends viewCommon.View {
178178
if (this._childrenCount > 0) {
179179
// Notify each child for the _onAttached event
180180
var that = this;
181-
var eachChild = function(child: View): boolean {
181+
var eachChild = function (child: View): boolean {
182182
child._onAttached(context);
183183
if (!child._isAddedToNativeVisualTree) {
184184
// since we have lazy loading of the android widgets, we need to add the native instances at this point.
@@ -197,7 +197,7 @@ export class View extends viewCommon.View {
197197
if (this._childrenCount > 0) {
198198
// Detach children first
199199
var that = this;
200-
var eachChild = function(child: View): boolean {
200+
var eachChild = function (child: View): boolean {
201201
if (child._isAddedToNativeVisualTree) {
202202
that._removeViewFromNativeVisualTree(child);
203203
}
@@ -238,7 +238,7 @@ export class View extends viewCommon.View {
238238
}
239239
this._createUI();
240240
// Ensure layout params
241-
if (this._nativeView && !(this._nativeView.getLayoutParams() instanceof org.nativescript.widgets.CommonLayoutParams)) {
241+
if (this._nativeView && !this._nativeView.getLayoutParams()) {
242242
this._nativeView.setLayoutParams(new org.nativescript.widgets.CommonLayoutParams());
243243
}
244244

@@ -348,7 +348,7 @@ export class View extends viewCommon.View {
348348
return {
349349
x: utils.layout.toDeviceIndependentPixels(nativeArray[0]),
350350
y: utils.layout.toDeviceIndependentPixels(nativeArray[1]),
351-
}
351+
}
352352
}
353353

354354
public getLocationOnScreen(): viewDefinition.Point {
@@ -361,7 +361,7 @@ export class View extends viewCommon.View {
361361
return {
362362
x: utils.layout.toDeviceIndependentPixels(nativeArray[0]),
363363
y: utils.layout.toDeviceIndependentPixels(nativeArray[1]),
364-
}
364+
}
365365
}
366366

367367
public getLocationRelativeTo(otherView: viewDefinition.View): viewDefinition.Point {
@@ -378,7 +378,7 @@ export class View extends viewCommon.View {
378378
return {
379379
x: utils.layout.toDeviceIndependentPixels(myArray[0] - otherArray[0]),
380380
y: utils.layout.toDeviceIndependentPixels(myArray[1] - otherArray[1]),
381-
}
381+
}
382382
}
383383

384384
public static resolveSizeAndState(size: number, specSize: number, specMode: number, childMeasuredState: number): number {
@@ -500,35 +500,12 @@ export class ViewStyler implements style.Styler {
500500
(<android.view.View>view._nativeView).setMinimumHeight(0);
501501
}
502502

503-
private static getNativeLayoutParams(nativeView: android.view.View): org.nativescript.widgets.CommonLayoutParams {
504-
var lp = <org.nativescript.widgets.CommonLayoutParams>nativeView.getLayoutParams();
505-
if (!(lp instanceof org.nativescript.widgets.CommonLayoutParams)) {
506-
lp = new org.nativescript.widgets.CommonLayoutParams();
507-
}
508-
509-
return lp;
510-
}
511-
512503
private static setNativeLayoutParamsProperty(view: View, params: CommonLayoutParams): void {
513-
var nativeView: android.view.View = view._nativeView;
514-
var lp = ViewStyler.getNativeLayoutParams(nativeView);
515-
516-
lp.widthPercent = params.widthPercent;
517-
lp.heightPercent = params.heightPercent;
504+
let nativeView: android.view.View = view._nativeView;
518505

519-
lp.leftMarginPercent = params.leftMarginPercent;
520-
lp.topMarginPercent = params.topMarginPercent;
521-
lp.rightMarginPercent = params.rightMarginPercent;
522-
lp.bottomMarginPercent = params.bottomMarginPercent;
506+
let width = params.width * utils.layout.getDisplayDensity();
507+
let height = params.height * utils.layout.getDisplayDensity();
523508

524-
lp.leftMargin = Math.round(params.leftMargin * utils.layout.getDisplayDensity());
525-
lp.topMargin = Math.round(params.topMargin * utils.layout.getDisplayDensity());
526-
lp.rightMargin = Math.round(params.rightMargin * utils.layout.getDisplayDensity());
527-
lp.bottomMargin = Math.round(params.bottomMargin * utils.layout.getDisplayDensity());
528-
529-
var width = params.width * utils.layout.getDisplayDensity();
530-
var height = params.height * utils.layout.getDisplayDensity();
531-
532509
// If width is not specified set it as WRAP_CONTENT
533510
if (width < 0) {
534511
width = -2;
@@ -539,7 +516,7 @@ export class ViewStyler implements style.Styler {
539516
height = -2;
540517
}
541518

542-
var gravity = 0;
519+
let gravity = 0;
543520
switch (params.horizontalAlignment) {
544521
case enums.HorizontalAlignment.left:
545522
gravity |= android.view.Gravity.LEFT;
@@ -591,10 +568,70 @@ export class ViewStyler implements style.Styler {
591568
throw new Error("Invalid verticalAlignment value: " + params.verticalAlignment);
592569
}
593570

594-
lp.gravity = gravity;
571+
let lp = nativeView.getLayoutParams();
595572
lp.width = Math.round(width);
596573
lp.height = Math.round(height);
597574

575+
if (lp instanceof org.nativescript.widgets.CommonLayoutParams) {
576+
lp.widthPercent = params.widthPercent;
577+
lp.heightPercent = params.heightPercent;
578+
lp.leftMarginPercent = params.leftMarginPercent;
579+
lp.topMarginPercent = params.topMarginPercent;
580+
lp.rightMarginPercent = params.rightMarginPercent;
581+
lp.bottomMarginPercent = params.bottomMarginPercent;
582+
lp.leftMargin = Math.round(params.leftMargin * utils.layout.getDisplayDensity());
583+
lp.topMargin = Math.round(params.topMargin * utils.layout.getDisplayDensity());
584+
lp.rightMargin = Math.round(params.rightMargin * utils.layout.getDisplayDensity());
585+
lp.bottomMargin = Math.round(params.bottomMargin * utils.layout.getDisplayDensity());
586+
lp.gravity = gravity;
587+
}
588+
else {
589+
let layoutParams: any = lp;
590+
if (types.isDefined(layoutParams.widthPercent)) {
591+
layoutParams.widthPercent = params.widthPercent;
592+
}
593+
594+
if (types.isDefined(layoutParams.heightPercent)) {
595+
layoutParams.heightPercent = params.heightPercent;
596+
}
597+
598+
if (types.isDefined(layoutParams.leftMarginPercent)) {
599+
layoutParams.leftMarginPercent = params.leftMarginPercent;
600+
}
601+
602+
if (types.isDefined(layoutParams.topMarginPercent)) {
603+
layoutParams.topMarginPercent = params.topMarginPercent;
604+
}
605+
606+
if (types.isDefined(layoutParams.rightMarginPercent)) {
607+
layoutParams.rightMarginPercent = params.rightMarginPercent;
608+
}
609+
610+
if (types.isDefined(layoutParams.bottomMarginPercent)) {
611+
layoutParams.bottomMarginPercent = params.bottomMarginPercent;
612+
}
613+
614+
if (types.isDefined(layoutParams.leftMargin)) {
615+
layoutParams.leftMargin = Math.round(params.leftMargin * utils.layout.getDisplayDensity());
616+
}
617+
618+
if (types.isDefined(layoutParams.topMargin)) {
619+
layoutParams.topMargin = Math.round(params.topMargin * utils.layout.getDisplayDensity());
620+
}
621+
622+
if (types.isDefined(layoutParams.rightMargin)) {
623+
layoutParams.rightMargin = Math.round(params.rightMargin * utils.layout.getDisplayDensity());
624+
}
625+
626+
if (types.isDefined(layoutParams.bottomMargin)) {
627+
layoutParams.bottomMargin = Math.round(params.bottomMargin * utils.layout.getDisplayDensity());
628+
}
629+
630+
if (types.isDefined(layoutParams.gravity)) {
631+
layoutParams.gravity = gravity;
632+
}
633+
}
634+
598635
nativeView.setLayoutParams(lp);
599636
}
600637

@@ -674,7 +711,7 @@ export class ViewStyler implements style.Styler {
674711
if (view.android.setZ) {
675712
view.android.setZ(newValue);
676713

677-
if(view.android instanceof android.widget.Button){
714+
if (view.android instanceof android.widget.Button) {
678715
view.android.setStateListAnimator(null);
679716
}
680717
}

tns-core-modules/ui/layouts/absolute-layout/absolute-layout.android.ts

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,11 @@ function setNativeProperty(data: PropertyChangeData, setter: (lp: org.nativescri
1010
var view = data.object;
1111
if (view instanceof View) {
1212
var nativeView: android.view.View = view._nativeView;
13-
14-
var lp = <org.nativescript.widgets.CommonLayoutParams>nativeView.getLayoutParams();
15-
if (!(lp instanceof org.nativescript.widgets.CommonLayoutParams)) {
16-
lp = new org.nativescript.widgets.CommonLayoutParams();
13+
var lp = nativeView.getLayoutParams() || new org.nativescript.widgets.CommonLayoutParams();
14+
if (lp instanceof org.nativescript.widgets.CommonLayoutParams) {
15+
setter(lp);
16+
nativeView.setLayoutParams(lp);
1717
}
18-
setter(lp);
19-
nativeView.setLayoutParams(lp);
2018
}
2119
}
2220

tns-core-modules/ui/layouts/dock-layout/dock-layout.android.ts

Lines changed: 20 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -10,30 +10,27 @@ function setNativeDockProperty(data: PropertyChangeData) {
1010
var view = data.object;
1111
if (view instanceof View) {
1212
var nativeView: android.view.View = view._nativeView;
13-
14-
var lp = <org.nativescript.widgets.CommonLayoutParams>nativeView.getLayoutParams();
15-
if (!(lp instanceof org.nativescript.widgets.CommonLayoutParams)) {
16-
lp = new org.nativescript.widgets.CommonLayoutParams();
17-
}
18-
19-
switch (data.newValue) {
20-
case Dock.left:
21-
lp.dock = org.nativescript.widgets.Dock.left;
22-
break;
23-
case Dock.top:
24-
lp.dock = org.nativescript.widgets.Dock.top;
25-
break;
26-
case Dock.right:
27-
lp.dock = org.nativescript.widgets.Dock.right;
28-
break;
29-
case Dock.bottom:
30-
lp.dock = org.nativescript.widgets.Dock.bottom;
31-
break;
32-
default:
33-
throw new Error("Invalid dock value: " + data.newValue + " on element: " + view);
13+
var lp = nativeView.getLayoutParams() || new org.nativescript.widgets.CommonLayoutParams();
14+
if (lp instanceof org.nativescript.widgets.CommonLayoutParams) {
15+
switch (data.newValue) {
16+
case Dock.left:
17+
lp.dock = org.nativescript.widgets.Dock.left;
18+
break;
19+
case Dock.top:
20+
lp.dock = org.nativescript.widgets.Dock.top;
21+
break;
22+
case Dock.right:
23+
lp.dock = org.nativescript.widgets.Dock.right;
24+
break;
25+
case Dock.bottom:
26+
lp.dock = org.nativescript.widgets.Dock.bottom;
27+
break;
28+
default:
29+
throw new Error("Invalid dock value: " + data.newValue + " on element: " + view);
30+
}
31+
32+
nativeView.setLayoutParams(lp);
3433
}
35-
36-
nativeView.setLayoutParams(lp);
3734
}
3835
}
3936

tns-core-modules/ui/layouts/grid-layout/grid-layout.android.ts

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,11 @@ function setNativeProperty(data: PropertyChangeData, setter: (lp: org.nativescri
1010
let view = data.object;
1111
if (view instanceof View) {
1212
let nativeView: android.view.View = view._nativeView;
13-
14-
let lp = <org.nativescript.widgets.CommonLayoutParams>nativeView.getLayoutParams();
15-
if (!(lp instanceof org.nativescript.widgets.CommonLayoutParams)) {
16-
lp = new org.nativescript.widgets.CommonLayoutParams();
13+
var lp = nativeView.getLayoutParams() || new org.nativescript.widgets.CommonLayoutParams();
14+
if (lp instanceof org.nativescript.widgets.CommonLayoutParams) {
15+
setter(lp);
16+
nativeView.setLayoutParams(lp);
1717
}
18-
setter(lp);
19-
nativeView.setLayoutParams(lp);
2018
}
2119
}
2220

tns-core-modules/ui/tab-view/tab-view.android.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -228,7 +228,7 @@ export class TabView extends common.TabView {
228228
}
229229

230230
this._viewPager = new android.support.v4.view.ViewPager(this._context);
231-
var lp = new org.nativescript.widgets.CommonLayoutParams()
231+
var lp = new org.nativescript.widgets.CommonLayoutParams();
232232
lp.row = 1;
233233
this._viewPager.setLayoutParams(lp);
234234
this._grid.addView(this._viewPager);

0 commit comments

Comments
 (0)