Просмотр исходного кода

Add title.topMargin option (#5163)

Useful for aligning the title when TopBar height is changed.
Guy Carmeli 5 лет назад
Родитель
Сommit
069cb85132
No account linked to committer's email address

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

@@ -32,6 +32,7 @@ public class TitleOptions {
32 32
         options.fontFamily = typefaceManager.getTypeFace(json.optString("fontFamily", ""));
33 33
         options.alignment = Alignment.fromString(TextParser.parse(json, "alignment").get(""));
34 34
         options.height = NumberParser.parse(json, "height");
35
+        options.topMargin = NumberParser.parse(json, "topMargin");
35 36
 
36 37
         return options;
37 38
     }
@@ -43,6 +44,7 @@ public class TitleOptions {
43 44
     @Nullable public Typeface fontFamily;
44 45
     public Component component = new Component();
45 46
     public Number height = new NullNumber();
47
+    public Number topMargin = new NullNumber();
46 48
 
47 49
     void mergeWith(final TitleOptions other) {
48 50
         if (other.text.hasValue()) text = other.text;
@@ -52,6 +54,7 @@ public class TitleOptions {
52 54
         if (other.alignment != Alignment.Default) alignment = other.alignment;
53 55
         if (other.component.hasValue()) component = other.component;
54 56
         if (other.height.hasValue()) height = other.height;
57
+        if (other.topMargin.hasValue()) topMargin = other.topMargin;
55 58
     }
56 59
 
57 60
     void mergeWithDefault(TitleOptions defaultOptions) {
@@ -62,5 +65,6 @@ public class TitleOptions {
62 65
         if (alignment == Alignment.Default) alignment = defaultOptions.alignment;
63 66
         component.mergeWithDefault(defaultOptions.component);
64 67
         if (!height.hasValue()) height = defaultOptions.height;
68
+        if (!topMargin.hasValue()) topMargin = defaultOptions.topMargin;
65 69
     }
66 70
 }

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

@@ -171,6 +171,7 @@ public class StackPresenter {
171 171
 
172 172
         topBar.setTitleHeight(options.title.height.get(UiUtils.getTopBarHeightDp(activity)));
173 173
         topBar.setTitle(options.title.text.get(""));
174
+        topBar.setTitleTopMargin(options.title.topMargin.get(0));
174 175
 
175 176
         if (options.title.component.hasValue()) {
176 177
             if (titleControllers.containsKey(component)) {
@@ -400,6 +401,7 @@ public class StackPresenter {
400 401
 
401 402
         if (options.title.height.hasValue()) topBar.setTitleHeight(options.title.height.get());
402 403
         if (options.title.text.hasValue()) topBar.setTitle(options.title.text.get());
404
+        if (options.title.topMargin.hasValue()) topBar.setTitleTopMargin(options.title.topMargin.get());
403 405
 
404 406
         if (options.title.component.hasValue()) {
405 407
             if (titleControllers.containsKey(component)) {

+ 10
- 0
lib/android/app/src/main/java/com/reactnativenavigation/views/titlebar/TitleBar.java Просмотреть файл

@@ -226,6 +226,16 @@ public class TitleBar extends Toolbar {
226 226
         setLayoutParams(lp);
227 227
     }
228 228
 
229
+    public void setTopMargin(int topMargin) {
230
+        int pixelTopMargin = UiUtils.dpToPx(getContext(), topMargin);
231
+        if (getLayoutParams() instanceof MarginLayoutParams) {
232
+            MarginLayoutParams lp = (MarginLayoutParams) getLayoutParams();
233
+            if (lp.topMargin == pixelTopMargin) return;
234
+            lp.topMargin = pixelTopMargin;
235
+            setLayoutParams(lp);
236
+        }
237
+    }
238
+
229 239
     public void setOverflowButtonColor(int color) {
230 240
         ActionMenuView actionMenuView = ViewUtils.findChildByClass(this, ActionMenuView.class);
231 241
         if (actionMenuView != null) {

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

@@ -122,6 +122,10 @@ public class TopBar extends AppBarLayout implements ScrollEventListener.ScrollAw
122 122
         titleBar.setHeight(height);
123 123
     }
124 124
 
125
+    public void setTitleTopMargin(int topMargin) {
126
+        titleBar.setTopMargin(topMargin);
127
+    }
128
+
125 129
     public void setTitle(String title) {
126 130
         titleBar.setTitle(title);
127 131
     }