Skip to content

Commit f165382

Browse files
author
Hristo Hristov
authored
Apply gravity in android if LayoutParams have gravity field. (NativeScript#3814)
ActionBar titleView aligments are applied only if no alignment are set to titleView and after view is added to visual tree otherwise they were reset in addView method
1 parent 6398cbd commit f165382

File tree

2 files changed

+53
-7
lines changed

2 files changed

+53
-7
lines changed

tns-core-modules/ui/action-bar/action-bar-common.ts

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -61,10 +61,18 @@ export class ActionBarBase extends View implements ActionBarDefinition {
6161

6262
this._titleView = value;
6363

64-
if (this._titleView) {
65-
this._titleView.style[horizontalAlignmentProperty.cssName] = "center";
66-
this._titleView.style[verticalAlignmentProperty.cssName] = "middle";
67-
this._addView(this._titleView);
64+
if (value) {
65+
// Addview will reset CSS properties so we first add it then set aligments with lowest priority.
66+
this._addView(value);
67+
const style = value.style;
68+
69+
if (!horizontalAlignmentProperty.isSet(style)) {
70+
style[horizontalAlignmentProperty.cssName] = "center";
71+
}
72+
73+
if (!verticalAlignmentProperty.isSet(style)) {
74+
style[verticalAlignmentProperty.cssName] = "middle";
75+
}
6876
}
6977

7078
this.update();

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

Lines changed: 41 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -370,15 +370,53 @@ export class View extends ViewCommon {
370370
get [horizontalAlignmentProperty.native](): HorizontalAlignment {
371371
return <HorizontalAlignment>org.nativescript.widgets.ViewHelper.getHorizontalAlignment(this.nativeView);
372372
}
373-
set [horizontalAlignmentProperty.native](value: HorizontalAlignment) {
374-
org.nativescript.widgets.ViewHelper.setHorizontalAlignment(this.nativeView, value);
373+
set [horizontalAlignmentProperty.native](value: HorizontalAlignment)  {
374+
const  nativeView = this.nativeView;
375+
const  lp: any = nativeView.getLayoutParams() || new org.nativescript.widgets.CommonLayoutParams();
376+
// Set only if params gravity exists.
377+
if (lp.gravity !== undefined) {
378+
switch (value) {
379+
case "left":
380+
lp.gravity = android.view.Gravity.LEFT | (lp.gravity & android.view.Gravity.VERTICAL_GRAVITY_MASK);
381+
break;
382+
case "center":
383+
lp.gravity = android.view.Gravity.CENTER_HORIZONTAL | (lp.gravity & android.view.Gravity.VERTICAL_GRAVITY_MASK);
384+
break;
385+
case "right":
386+
lp.gravity = android.view.Gravity.RIGHT | (lp.gravity & android.view.Gravity.VERTICAL_GRAVITY_MASK);
387+
break;
388+
case "stretch":
389+
lp.gravity = android.view.Gravity.FILL_HORIZONTAL | (lp.gravity & android.view.Gravity.VERTICAL_GRAVITY_MASK);
390+
break;
391+
}
392+
nativeView.setLayoutParams(lp);
393+
}
375394
}
376395

377396
get [verticalAlignmentProperty.native](): VerticalAlignment {
378397
return <VerticalAlignment>org.nativescript.widgets.ViewHelper.getVerticalAlignment(this.nativeView);
379398
}
380399
set [verticalAlignmentProperty.native](value: VerticalAlignment) {
381-
org.nativescript.widgets.ViewHelper.setVerticalAlignment(this.nativeView, value);
400+
const  nativeView = this.nativeView;
401+
const  lp: any = nativeView.getLayoutParams() || new org.nativescript.widgets.CommonLayoutParams();
402+
// Set only if params gravity exists.
403+
if (lp.gravity !== undefined) {
404+
switch (value) {
405+
case "top":
406+
lp.gravity = android.view.Gravity.TOP | (lp.gravity & android.view.Gravity.HORIZONTAL_GRAVITY_MASK);
407+
break;
408+
case "middle":
409+
lp.gravity = android.view.Gravity.CENTER_VERTICAL | (lp.gravity & android.view.Gravity.HORIZONTAL_GRAVITY_MASK);
410+
break;
411+
case "bottom":
412+
lp.gravity = android.view.Gravity.BOTTOM | (lp.gravity & android.view.Gravity.HORIZONTAL_GRAVITY_MASK);
413+
break;
414+
case "stretch":
415+
lp.gravity = android.view.Gravity.FILL_VERTICAL | (lp.gravity & android.view.Gravity.HORIZONTAL_GRAVITY_MASK);
416+
break;
417+
}
418+
nativeView.setLayoutParams(lp);
419+
}
382420
}
383421

384422
get [rotateProperty.native](): number {

0 commit comments

Comments
 (0)