Parcourir la source

[V3] Android: re-add support for navigationBarColor (#5547)

Add support for NavigationBar color on Android
Maurus Cuelenaere il y a 5 ans
Parent
révision
8af95da24a

+ 9
- 1
docs/docs/options-migration.md Voir le fichier

@@ -540,6 +540,15 @@ statusBar: {
540 540
 }
541 541
 ```
542 542
 
543
+## navigationBarColor
544
+NavigationBar color
545
+
546
+```js
547
+navigationBar: {
548
+  backgroundColor: 'red'
549
+}
550
+```
551
+
543 552
 ## drawUnderStatusBar
544 553
 Draw content behind the StatusBar
545 554
 
@@ -603,7 +612,6 @@ topBar: {
603 612
 * topBarShadowOffset
604 613
 * topBarShadowRadius
605 614
 * preferredContentSize
606
-* navigationBarColor
607 615
 * navBarSubTitleTextCentered
608 616
 * collapsingToolBarImage
609 617
 * collapsingToolBarCollapsedColor

+ 3
- 0
docs/docs/styling.md Voir le fichier

@@ -258,6 +258,9 @@ Navigation.mergeOptions(this.props.componentId, {
258 258
     drawBehind: true,
259 259
     visible: false
260 260
   },
261
+  navigationBar: {
262
+    backgroundColor: 'red',
263
+  },
261 264
   layout: {
262 265
     topMargin: (await Navigation.constants()).statusBarHeight, // Set the layout's top margin
263 266
     orientation: ['portrait', 'landscape'] | ['sensorLandscape'], // An array of supported orientations

+ 28
- 0
lib/android/app/src/main/java/com/reactnativenavigation/parse/NavigationBarOptions.java Voir le fichier

@@ -0,0 +1,28 @@
1
+package com.reactnativenavigation.parse;
2
+
3
+import com.reactnativenavigation.parse.params.Colour;
4
+import com.reactnativenavigation.parse.params.NullColor;
5
+import com.reactnativenavigation.parse.parsers.ColorParser;
6
+
7
+import org.json.JSONObject;
8
+
9
+public class NavigationBarOptions {
10
+    public static NavigationBarOptions parse(JSONObject json) {
11
+        NavigationBarOptions result = new NavigationBarOptions();
12
+        if (json == null) return result;
13
+
14
+        result.backgroundColor = ColorParser.parse(json, "backgroundColor");
15
+
16
+        return result;
17
+    }
18
+
19
+    public Colour backgroundColor = new NullColor();
20
+
21
+    public void mergeWith(NavigationBarOptions other) {
22
+        if (other.backgroundColor.hasValue()) backgroundColor = other.backgroundColor;
23
+    }
24
+
25
+    public void mergeWithDefault(NavigationBarOptions defaultOptions) {
26
+        if (!backgroundColor.hasValue()) backgroundColor = defaultOptions.backgroundColor;
27
+    }
28
+}

+ 5
- 0
lib/android/app/src/main/java/com/reactnativenavigation/parse/Options.java Voir le fichier

@@ -27,6 +27,7 @@ public class Options {
27 27
         result.sideMenuRootOptions = SideMenuRootOptions.parse(json.optJSONObject("sideMenu"));
28 28
         result.animations = AnimationsOptions.parse(json.optJSONObject("animations"));
29 29
         result.modal = ModalOptions.parse(json);
30
+        result.navigationBar = NavigationBarOptions.parse(json.optJSONObject("navigationBar"));
30 31
         result.statusBar = StatusBarOptions.parse(json.optJSONObject("statusBar"));
31 32
         result.layout = LayoutOptions.parse(json.optJSONObject("layout"));
32 33
         result.transitions = Transitions.parse(json.optJSONObject("customTransition"));
@@ -44,6 +45,7 @@ public class Options {
44 45
     @NonNull public AnimationsOptions animations = new AnimationsOptions();
45 46
     @NonNull public SideMenuRootOptions sideMenuRootOptions = new SideMenuRootOptions();
46 47
     @NonNull public ModalOptions modal = new ModalOptions();
48
+    @NonNull public NavigationBarOptions navigationBar = new NavigationBarOptions();
47 49
     @NonNull public StatusBarOptions statusBar = new StatusBarOptions();
48 50
     @NonNull public LayoutOptions layout = new LayoutOptions();
49 51
     @NonNull public Transitions transitions = new Transitions();
@@ -65,6 +67,7 @@ public class Options {
65 67
         result.sideMenuRootOptions.mergeWith(sideMenuRootOptions);
66 68
         result.animations.mergeWith(animations);
67 69
         result.modal.mergeWith(modal);
70
+        result.navigationBar.mergeWith(navigationBar);
68 71
         result.statusBar.mergeWith(statusBar);
69 72
         result.layout.mergeWith(layout);
70 73
         result.transitions.mergeWith(transitions);
@@ -83,6 +86,7 @@ public class Options {
83 86
         result.animations.mergeWith(other.animations);
84 87
         result.sideMenuRootOptions.mergeWith(other.sideMenuRootOptions);
85 88
         result.modal.mergeWith(other.modal);
89
+        result.navigationBar.mergeWith(other.navigationBar);
86 90
         result.statusBar.mergeWith(other.statusBar);
87 91
         result.layout.mergeWith(other.layout);
88 92
         result.transitions.mergeWith(transitions);
@@ -99,6 +103,7 @@ public class Options {
99 103
         animations.mergeWithDefault(defaultOptions.animations);
100 104
         sideMenuRootOptions.mergeWithDefault(defaultOptions.sideMenuRootOptions);
101 105
         modal.mergeWithDefault(defaultOptions.modal);
106
+        navigationBar.mergeWithDefault(defaultOptions.navigationBar);
102 107
         statusBar.mergeWithDefault(defaultOptions.statusBar);
103 108
         layout.mergeWithDefault(defaultOptions.layout);
104 109
         transitions.mergeWithDefault(defaultOptions.transitions);

+ 9
- 0
lib/android/app/src/main/java/com/reactnativenavigation/presentation/Presenter.java Voir le fichier

@@ -10,6 +10,7 @@ import android.view.View;
10 10
 import android.view.ViewGroup.MarginLayoutParams;
11 11
 import android.view.Window;
12 12
 
13
+import com.reactnativenavigation.parse.NavigationBarOptions;
13 14
 import com.reactnativenavigation.parse.Options;
14 15
 import com.reactnativenavigation.parse.OrientationOptions;
15 16
 import com.reactnativenavigation.parse.StatusBarOptions;
@@ -84,6 +85,7 @@ public class Presenter {
84 85
     }
85 86
 
86 87
     private void applyStatusBarOptions(Options options) {
88
+        setNavigationBarBackgroundColor(options.navigationBar);
87 89
         setStatusBarBackgroundColor(options.statusBar);
88 90
         setTextColorScheme(options.statusBar.textColorScheme);
89 91
         setTranslucent(options.statusBar);
@@ -117,6 +119,13 @@ public class Presenter {
117 119
         }
118 120
     }
119 121
 
122
+    private void setNavigationBarBackgroundColor(NavigationBarOptions navigationBar) {
123
+        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP && navigationBar.backgroundColor.canApplyValue()) {
124
+            int defaultColor = activity.getWindow().getNavigationBarColor();
125
+            activity.getWindow().setNavigationBarColor(navigationBar.backgroundColor.get(defaultColor));
126
+        }
127
+    }
128
+
120 129
     private void setTextColorScheme(TextColorScheme scheme) {
121 130
         final View view = activity.getWindow().getDecorView();
122 131
         if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M) return;