Guy Carmeli 6 лет назад
Родитель
Сommit
563c3c8ca4

+ 1
- 0
docs/docs/styling.md Просмотреть файл

@@ -231,6 +231,7 @@ Navigation.mergeOptions(this.props.componentId, {
231 231
     borderColor: 'red',
232 232
     borderHeight: 1.3,
233 233
     elevation: 1.5, // TopBar elevation in dp
234
+    topMargin: 24, // top margin in dp
234 235
     title: {
235 236
       height: 70 // TitleBar height in dp
236 237
     }

+ 4
- 0
lib/android/app/src/main/java/com/reactnativenavigation/parse/TopBarOptions.java Просмотреть файл

@@ -41,6 +41,7 @@ public class TopBarOptions {
41 41
         options.borderColor = ColorParser.parse(json, "borderColor");
42 42
         options.borderHeight = FractionParser.parse(json, "borderHeight");
43 43
         options.elevation = FractionParser.parse(json, "elevation");
44
+        options.topMargin = NumberParser.parse(json, "topMargin");
44 45
         options.buttons = TopBarButtons.parse(typefaceLoader, json);
45 46
 
46 47
         options.rightButtonColor = ColorParser.parse(json, "rightButtonColor");
@@ -61,6 +62,7 @@ public class TopBarOptions {
61 62
     public Bool drawBehind = new NullBool();
62 63
     public Number height = new NullNumber();
63 64
     public Fraction elevation = new NullFraction();
65
+    public Number topMargin = new NullNumber();
64 66
     public Fraction borderHeight = new NullFraction();
65 67
     public Colour borderColor = new NullColor();
66 68
 
@@ -90,6 +92,7 @@ public class TopBarOptions {
90 92
         if (other.borderHeight.hasValue()) borderHeight = other.borderHeight;
91 93
         if (other.borderColor.hasValue()) borderColor = other.borderColor;
92 94
         if (other.elevation.hasValue()) elevation = other.elevation;
95
+        if (other.topMargin.hasValue()) topMargin = other.topMargin;
93 96
 
94 97
         if (other.rightButtonColor.hasValue()) rightButtonColor = other.rightButtonColor;
95 98
         if (other.leftButtonColor.hasValue()) leftButtonColor = other.leftButtonColor;
@@ -113,6 +116,7 @@ public class TopBarOptions {
113 116
         if (!borderHeight.hasValue()) borderHeight = defaultOptions.borderHeight;
114 117
         if (!borderColor.hasValue()) borderColor = defaultOptions.borderColor;
115 118
         if (!elevation.hasValue()) elevation = defaultOptions.elevation;
119
+        if (!topMargin.hasValue()) topMargin = defaultOptions.topMargin;
116 120
 
117 121
         if (!rightButtonColor.hasValue()) rightButtonColor = defaultOptions.rightButtonColor;
118 122
         if (!leftButtonColor.hasValue()) leftButtonColor = defaultOptions.leftButtonColor;

+ 7
- 0
lib/android/app/src/main/java/com/reactnativenavigation/presentation/StackOptionsPresenter.java Просмотреть файл

@@ -8,6 +8,7 @@ import android.support.v7.widget.Toolbar;
8 8
 import android.view.Gravity;
9 9
 import android.view.View;
10 10
 import android.view.ViewGroup.LayoutParams;
11
+import android.view.ViewGroup.MarginLayoutParams;
11 12
 
12 13
 import com.reactnativenavigation.parse.Alignment;
13 14
 import com.reactnativenavigation.parse.AnimationsOptions;
@@ -151,6 +152,9 @@ public class StackOptionsPresenter {
151 152
     private void applyTopBarOptions(TopBarOptions options, AnimationsOptions animationOptions, Component component, Options componentOptions) {
152 153
         topBar.setHeight(options.height.get(LayoutParams.WRAP_CONTENT));
153 154
         topBar.setElevation(options.elevation.get(DEFAULT_ELEVATION));
155
+        if (topBar.getLayoutParams() instanceof MarginLayoutParams) {
156
+            ((MarginLayoutParams) topBar.getLayoutParams()).topMargin = UiUtils.dpToPx(activity, options.topMargin.get(0));
157
+        }
154 158
 
155 159
         topBar.setTitleHeight(options.title.height.get(LayoutParams.WRAP_CONTENT));
156 160
         topBar.setTitle(options.title.text.get(""));
@@ -334,6 +338,9 @@ public class StackOptionsPresenter {
334 338
     private void mergeTopBarOptions(TopBarOptions options, AnimationsOptions animationsOptions, Component component) {
335 339
         if (options.height.hasValue()) topBar.setHeight(options.height.get());
336 340
         if (options.elevation.hasValue()) topBar.setElevation(options.elevation.get());
341
+        if (options.topMargin.hasValue() && topBar.getLayoutParams() instanceof MarginLayoutParams) {
342
+            ((MarginLayoutParams) topBar.getLayoutParams()).topMargin = UiUtils.dpToPx(activity, options.topMargin.get());
343
+        }
337 344
 
338 345
         if (options.title.height.hasValue()) topBar.setTitleHeight(options.title.height.get());
339 346
         if (options.title.text.hasValue()) topBar.setTitle(options.title.text.get());

+ 7
- 2
lib/android/app/src/main/java/com/reactnativenavigation/utils/UiUtils.java Просмотреть файл

@@ -60,7 +60,7 @@ public class UiUtils {
60 60
         final int resourceId = resources.getIdentifier("status_bar_height", "dimen", "android");
61 61
         statusBarHeight = resourceId > 0 ?
62 62
                 resources.getDimensionPixelSize(resourceId) :
63
-                (int) dpToPx(context, Build.VERSION.SDK_INT >= Build.VERSION_CODES.M ? STATUS_BAR_HEIGHT_M : STATUS_BAR_HEIGHT_L);
63
+                dpToPx(context, Build.VERSION.SDK_INT >= Build.VERSION_CODES.M ? STATUS_BAR_HEIGHT_M : STATUS_BAR_HEIGHT_L);
64 64
         return statusBarHeight;
65 65
     }
66 66
 
@@ -72,7 +72,7 @@ public class UiUtils {
72 72
         final int resourceId = resources.getIdentifier("action_bar_size", "dimen", "android");
73 73
         toolBarHeight = resourceId > 0 ?
74 74
                 resources.getDimensionPixelSize(resourceId) :
75
-                (int) dpToPx(context, DEFAULT_TOOLBAR_HEIGHT);
75
+                dpToPx(context, DEFAULT_TOOLBAR_HEIGHT);
76 76
         return toolBarHeight;
77 77
     }
78 78
 
@@ -81,6 +81,11 @@ public class UiUtils {
81 81
         return dp * scale + 0.5f;
82 82
     }
83 83
 
84
+    public static int dpToPx(Context context, int dp) {
85
+        float scale = context.getResources().getDisplayMetrics().density;
86
+        return (int) (dp * scale + 0.5f);
87
+    }
88
+
84 89
     public static float dpToSp(Context context, float dp) {
85 90
         return dpToPx(context, dp) / context.getResources().getDisplayMetrics().density;
86 91
     }