Browse Source

Add topBar.topMargin option

Guy Carmeli 6 years ago
parent
commit
563c3c8ca4

+ 1
- 0
docs/docs/styling.md View File

231
     borderColor: 'red',
231
     borderColor: 'red',
232
     borderHeight: 1.3,
232
     borderHeight: 1.3,
233
     elevation: 1.5, // TopBar elevation in dp
233
     elevation: 1.5, // TopBar elevation in dp
234
+    topMargin: 24, // top margin in dp
234
     title: {
235
     title: {
235
       height: 70 // TitleBar height in dp
236
       height: 70 // TitleBar height in dp
236
     }
237
     }

+ 4
- 0
lib/android/app/src/main/java/com/reactnativenavigation/parse/TopBarOptions.java View File

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

+ 7
- 0
lib/android/app/src/main/java/com/reactnativenavigation/presentation/StackOptionsPresenter.java View File

8
 import android.view.Gravity;
8
 import android.view.Gravity;
9
 import android.view.View;
9
 import android.view.View;
10
 import android.view.ViewGroup.LayoutParams;
10
 import android.view.ViewGroup.LayoutParams;
11
+import android.view.ViewGroup.MarginLayoutParams;
11
 
12
 
12
 import com.reactnativenavigation.parse.Alignment;
13
 import com.reactnativenavigation.parse.Alignment;
13
 import com.reactnativenavigation.parse.AnimationsOptions;
14
 import com.reactnativenavigation.parse.AnimationsOptions;
151
     private void applyTopBarOptions(TopBarOptions options, AnimationsOptions animationOptions, Component component, Options componentOptions) {
152
     private void applyTopBarOptions(TopBarOptions options, AnimationsOptions animationOptions, Component component, Options componentOptions) {
152
         topBar.setHeight(options.height.get(LayoutParams.WRAP_CONTENT));
153
         topBar.setHeight(options.height.get(LayoutParams.WRAP_CONTENT));
153
         topBar.setElevation(options.elevation.get(DEFAULT_ELEVATION));
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
         topBar.setTitleHeight(options.title.height.get(LayoutParams.WRAP_CONTENT));
159
         topBar.setTitleHeight(options.title.height.get(LayoutParams.WRAP_CONTENT));
156
         topBar.setTitle(options.title.text.get(""));
160
         topBar.setTitle(options.title.text.get(""));
334
     private void mergeTopBarOptions(TopBarOptions options, AnimationsOptions animationsOptions, Component component) {
338
     private void mergeTopBarOptions(TopBarOptions options, AnimationsOptions animationsOptions, Component component) {
335
         if (options.height.hasValue()) topBar.setHeight(options.height.get());
339
         if (options.height.hasValue()) topBar.setHeight(options.height.get());
336
         if (options.elevation.hasValue()) topBar.setElevation(options.elevation.get());
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
         if (options.title.height.hasValue()) topBar.setTitleHeight(options.title.height.get());
345
         if (options.title.height.hasValue()) topBar.setTitleHeight(options.title.height.get());
339
         if (options.title.text.hasValue()) topBar.setTitle(options.title.text.get());
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 View File

60
         final int resourceId = resources.getIdentifier("status_bar_height", "dimen", "android");
60
         final int resourceId = resources.getIdentifier("status_bar_height", "dimen", "android");
61
         statusBarHeight = resourceId > 0 ?
61
         statusBarHeight = resourceId > 0 ?
62
                 resources.getDimensionPixelSize(resourceId) :
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
         return statusBarHeight;
64
         return statusBarHeight;
65
     }
65
     }
66
 
66
 
72
         final int resourceId = resources.getIdentifier("action_bar_size", "dimen", "android");
72
         final int resourceId = resources.getIdentifier("action_bar_size", "dimen", "android");
73
         toolBarHeight = resourceId > 0 ?
73
         toolBarHeight = resourceId > 0 ?
74
                 resources.getDimensionPixelSize(resourceId) :
74
                 resources.getDimensionPixelSize(resourceId) :
75
-                (int) dpToPx(context, DEFAULT_TOOLBAR_HEIGHT);
75
+                dpToPx(context, DEFAULT_TOOLBAR_HEIGHT);
76
         return toolBarHeight;
76
         return toolBarHeight;
77
     }
77
     }
78
 
78
 
81
         return dp * scale + 0.5f;
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
     public static float dpToSp(Context context, float dp) {
89
     public static float dpToSp(Context context, float dp) {
85
         return dpToPx(context, dp) / context.getResources().getDisplayMetrics().density;
90
         return dpToPx(context, dp) / context.getResources().getDisplayMetrics().density;
86
     }
91
     }