Browse Source

Set titleBarSubTitleTextCentered as navigator style for android (#2611)

* Set titleBarSubTitleTextCentered as navigator style

- Adapt title center

* fixed false declaration
Michael Kuczera 6 years ago
parent
commit
e437b7a23a

+ 1
- 0
android/app/src/main/java/com/reactnativenavigation/params/StyleParams.java View File

@@ -120,6 +120,7 @@ public class StyleParams {
120 120
     public int titleBarTitleFontSize;
121 121
     public boolean titleBarTitleFontBold;
122 122
     public boolean titleBarTitleTextCentered;
123
+    public boolean titleBarSubTitleTextCentered;
123 124
     public int titleBarHeight;
124 125
     public boolean backButtonHidden;
125 126
     public Font titleBarButtonFontFamily;

+ 5
- 0
android/app/src/main/java/com/reactnativenavigation/params/parsers/StyleParamsParser.java View File

@@ -66,6 +66,7 @@ public class StyleParamsParser {
66 66
         result.titleBarTitleFontSize = getInt("titleBarTitleFontSize", getDefaultTitleTextFontSize());
67 67
         result.titleBarTitleFontBold = getBoolean("titleBarTitleFontBold", getDefaultTitleTextFontBold());
68 68
         result.titleBarTitleTextCentered = getBoolean("titleBarTitleTextCentered", getDefaultTitleBarTextCentered());
69
+        result.titleBarSubTitleTextCentered = getBoolean("titleBarSubTitleTextCentered", getDefaultTitleBarTextCentered());
69 70
         result.titleBarHeight = getInt("titleBarHeight", getDefaultTitleBarHeight());
70 71
         result.backButtonHidden = getBoolean("backButtonHidden", getDefaultBackButtonHidden());
71 72
         result.topTabsHidden = getBoolean("topTabsHidden", getDefaultTopTabsHidden());
@@ -326,6 +327,10 @@ public class StyleParamsParser {
326 327
         return AppStyle.appStyle != null && AppStyle.appStyle.titleBarTitleTextCentered;
327 328
     }
328 329
 
330
+    private boolean getDefaultSubTitleBarTextCentered() {
331
+        return AppStyle.appStyle != null && AppStyle.appStyle.titleBarSubTitleTextCentered;
332
+    }
333
+
329 334
     private int getDefaultTitleBarHeight() {
330 335
         return AppStyle.appStyle == null ? -1 : AppStyle.appStyle.titleBarHeight;
331 336
     }

+ 22
- 1
android/app/src/main/java/com/reactnativenavigation/views/TitleBar.java View File

@@ -84,7 +84,7 @@ public class TitleBar extends Toolbar {
84 84
         setSubtitleFont(params);
85 85
         colorOverflowButton(params);
86 86
         setBackground(params);
87
-        centerTitle(params);
87
+        centerTopBarContent(params);
88 88
         setTopPadding(params);
89 89
     }
90 90
 
@@ -102,6 +102,7 @@ public class TitleBar extends Toolbar {
102 102
         super.setSubtitle(subtitle);
103 103
         setSubtitleFontSize(styleParams);
104 104
         setSubtitleFont(styleParams);
105
+        centerSubTitle(styleParams);
105 106
     }
106 107
 
107 108
     private void setSubtitleFontSize(StyleParams params) {
@@ -120,6 +121,11 @@ public class TitleBar extends Toolbar {
120 121
         }
121 122
     }
122 123
 
124
+    private void centerTopBarContent(final StyleParams params) {
125
+        centerTitle(params);
126
+        centerSubTitle(params);
127
+    }
128
+
123 129
     private void centerTitle(final StyleParams params) {
124 130
         final View titleView = getTitleView();
125 131
         if (titleView == null) {
@@ -135,6 +141,21 @@ public class TitleBar extends Toolbar {
135 141
         });
136 142
     }
137 143
 
144
+    private void centerSubTitle(final StyleParams params) {
145
+        final TextView subTitleView = getSubtitleView();
146
+        if (subTitleView == null) {
147
+            return;
148
+        }
149
+        ViewUtils.runOnPreDraw(subTitleView, new Runnable() {
150
+            @Override
151
+            public void run() {
152
+                if (params.titleBarSubTitleTextCentered) {
153
+                    subTitleView.setX(ViewUtils.getWindowWidth((Activity) getContext()) / 2 - subTitleView.getWidth() / 2);
154
+                }
155
+            }
156
+        });
157
+    }
158
+
138 159
     private void setTopPadding(final StyleParams params) {
139 160
         setPadding(0, (int) ViewUtils.convertDpToPixel(params.titleBarTopPadding), 0,0);
140 161
     }

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

@@ -108,6 +108,7 @@ this.props.navigator.setStyle({
108 108
   // Android only
109 109
   navigationBarColor: '#000000', // change the background color of the bottom native navigation bar.
110 110
   navBarTitleTextCentered: true, // default: false. centers the title.
111
+  navBarSubTitleTextCentered: true, // (Android - default: false, iOS - default: depending on navBarTitleTextCentered). centers the subTitle.
111 112
   navBarButtonFontFamily: 'sans-serif-thin', // Change the font family of textual buttons
112 113
   statusBarColor: '#000000', // change the color of the status bar.
113 114
   drawUnderStatusBar: false, // default: false, will draw the screen underneath the statusbar. Useful togheter with statusBarColor: transparent

+ 1
- 0
src/deprecated/platformSpecificDeprecated.android.js View File

@@ -180,6 +180,7 @@ function convertStyleParams(originalStyleObject) {
180 180
     titleBarTitleFontSize: originalStyleObject.navBarTextFontSize,
181 181
     titleBarTitleFontBold: originalStyleObject.navBarTextFontBold,
182 182
     titleBarTitleTextCentered: originalStyleObject.navBarTitleTextCentered,
183
+    titleBarSubTitleTextCentered: originalStyleObject.navBarSubTitleTextCentered,
183 184
     titleBarHeight: originalStyleObject.navBarHeight,
184 185
     titleBarTopPadding: originalStyleObject.navBarTopPadding,
185 186
     backButtonHidden: originalStyleObject.backButtonHidden,