Apply gravity in android if LayoutParams have gravity field. (#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
This commit is contained in:
Hristo Hristov
2017-03-20 15:09:23 +02:00
committed by GitHub
parent 6398cbdec9
commit f165382bb7
2 changed files with 53 additions and 7 deletions

View File

@ -61,10 +61,18 @@ export class ActionBarBase extends View implements ActionBarDefinition {
this._titleView = value; this._titleView = value;
if (this._titleView) { if (value) {
this._titleView.style[horizontalAlignmentProperty.cssName] = "center"; // Addview will reset CSS properties so we first add it then set aligments with lowest priority.
this._titleView.style[verticalAlignmentProperty.cssName] = "middle"; this._addView(value);
this._addView(this._titleView); const style = value.style;
if (!horizontalAlignmentProperty.isSet(style)) {
style[horizontalAlignmentProperty.cssName] = "center";
}
if (!verticalAlignmentProperty.isSet(style)) {
style[verticalAlignmentProperty.cssName] = "middle";
}
} }
this.update(); this.update();

View File

@ -370,15 +370,53 @@ export class View extends ViewCommon {
get [horizontalAlignmentProperty.native](): HorizontalAlignment { get [horizontalAlignmentProperty.native](): HorizontalAlignment {
return <HorizontalAlignment>org.nativescript.widgets.ViewHelper.getHorizontalAlignment(this.nativeView); return <HorizontalAlignment>org.nativescript.widgets.ViewHelper.getHorizontalAlignment(this.nativeView);
} }
set [horizontalAlignmentProperty.native](value: HorizontalAlignment) { set [horizontalAlignmentProperty.native](value: HorizontalAlignment)  {
org.nativescript.widgets.ViewHelper.setHorizontalAlignment(this.nativeView, value); const  nativeView = this.nativeView;
const  lp: any = nativeView.getLayoutParams() || new org.nativescript.widgets.CommonLayoutParams();
// Set only if params gravity exists.
if (lp.gravity !== undefined) {
switch (value) {
case "left":
lp.gravity = android.view.Gravity.LEFT | (lp.gravity & android.view.Gravity.VERTICAL_GRAVITY_MASK);
break;
case "center":
lp.gravity = android.view.Gravity.CENTER_HORIZONTAL | (lp.gravity & android.view.Gravity.VERTICAL_GRAVITY_MASK);
break;
case "right":
lp.gravity = android.view.Gravity.RIGHT | (lp.gravity & android.view.Gravity.VERTICAL_GRAVITY_MASK);
break;
case "stretch":
lp.gravity = android.view.Gravity.FILL_HORIZONTAL | (lp.gravity & android.view.Gravity.VERTICAL_GRAVITY_MASK);
break;
}
nativeView.setLayoutParams(lp);
}
} }
get [verticalAlignmentProperty.native](): VerticalAlignment { get [verticalAlignmentProperty.native](): VerticalAlignment {
return <VerticalAlignment>org.nativescript.widgets.ViewHelper.getVerticalAlignment(this.nativeView); return <VerticalAlignment>org.nativescript.widgets.ViewHelper.getVerticalAlignment(this.nativeView);
} }
set [verticalAlignmentProperty.native](value: VerticalAlignment) { set [verticalAlignmentProperty.native](value: VerticalAlignment) {
org.nativescript.widgets.ViewHelper.setVerticalAlignment(this.nativeView, value); const  nativeView = this.nativeView;
const  lp: any = nativeView.getLayoutParams() || new org.nativescript.widgets.CommonLayoutParams();
// Set only if params gravity exists.
if (lp.gravity !== undefined) {
switch (value) {
case "top":
lp.gravity = android.view.Gravity.TOP | (lp.gravity & android.view.Gravity.HORIZONTAL_GRAVITY_MASK);
break;
case "middle":
lp.gravity = android.view.Gravity.CENTER_VERTICAL | (lp.gravity & android.view.Gravity.HORIZONTAL_GRAVITY_MASK);
break;
case "bottom":
lp.gravity = android.view.Gravity.BOTTOM | (lp.gravity & android.view.Gravity.HORIZONTAL_GRAVITY_MASK);
break;
case "stretch":
lp.gravity = android.view.Gravity.FILL_VERTICAL | (lp.gravity & android.view.Gravity.HORIZONTAL_GRAVITY_MASK);
break;
}
nativeView.setLayoutParams(lp);
}
} }
get [rotateProperty.native](): number { get [rotateProperty.native](): number {