@@ -48,8 +48,6 @@ public static void edgeToEdge(ComponentActivity componentActivity) {
4848
4949 StatusBarView statusBarView = new StatusBarView (componentActivity );
5050 statusBarView .setId (ID_STATUSBAR );
51- // statusBarView.setBackgroundColor(Color.WHITE);
52- // setAppearanceLightStatusBars(componentActivity, true);
5351 setStatusBarColor (componentActivity , Color .WHITE );
5452 linearLayout .addView (statusBarView , new ViewGroup .LayoutParams (ViewGroup .LayoutParams .MATCH_PARENT , ViewGroup .LayoutParams .WRAP_CONTENT ));
5553
@@ -59,11 +57,9 @@ public static void edgeToEdge(ComponentActivity componentActivity) {
5957
6058 NavigationBarView navigationBarView = new NavigationBarView (componentActivity );
6159 navigationBarView .setId (ID_NAVIGATIONBAR );
62- //这样会是半透明的,不好看
63- // navigationBarView.setBackgroundColor(callback.setNavigationBarColor());
64- // setAppearanceLightNavigationBars(componentActivity, isLightColor(callback.setNavigationBarColor()));
6560 setNavigationBarColor (componentActivity , Color .WHITE );
6661 linearLayout .addView (navigationBarView , new ViewGroup .LayoutParams (ViewGroup .LayoutParams .MATCH_PARENT , ViewGroup .LayoutParams .WRAP_CONTENT ));
62+
6763 decorView .addView (linearLayout , new ViewGroup .LayoutParams (ViewGroup .LayoutParams .MATCH_PARENT , ViewGroup .LayoutParams .MATCH_PARENT ));
6864 }
6965
@@ -248,15 +244,32 @@ public static void setStatusBarColor(Activity activity, int color) {
248244 * at androidx.appcompat.app.AppCompatDelegateImpl.ensureSubDecor(AppCompatDelegateImpl.java:879)
249245 * at androidx.appcompat.app.AppCompatDelegateImpl.findViewById(AppCompatDelegateImpl.java:667)
250246 */
251- View view_statusbar = activity .findViewById (ID_STATUSBAR );
252- if ( view_statusbar != null ) view_statusbar .setBackgroundColor (color );
247+ View view = activity .findViewById (ID_STATUSBAR );
248+ view .setBackgroundColor (color );
253249 } catch (Exception e ) {
254250
255251 }
256252 boolean isLightColor = isLightColor (color );
257253 setAppearanceLightStatusBars (activity , isLightColor );
258254 }
259255
256+ public static void setNavigationBarColor (Activity activity , int color ) {
257+ try {
258+ /**
259+ * Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void androidx.appcompat.widget.ContentFrameLayout.setDecorPadding(int, int, int, int)' on a null object reference
260+ * at androidx.appcompat.app.AppCompatDelegateImpl.applyFixedSizeWindow(AppCompatDelegateImpl.java:1085)
261+ * at androidx.appcompat.app.AppCompatDelegateImpl.ensureSubDecor(AppCompatDelegateImpl.java:879)
262+ * at androidx.appcompat.app.AppCompatDelegateImpl.findViewById(AppCompatDelegateImpl.java:667)
263+ */
264+ View view = activity .findViewById (ID_NAVIGATIONBAR );
265+ view .setBackgroundColor (color );
266+ } catch (Exception e ) {
267+
268+ }
269+ boolean isLightColor = isLightColor (color );
270+ setAppearanceLightNavigationBars (activity , isLightColor );
271+ }
272+
260273 public static void setStatusBarColorOld (Activity activity , int color ) {
261274 Window window = activity .getWindow ();
262275 //去除statusbar不填充的标志
@@ -277,11 +290,10 @@ public static void setStatusBarColorOld(Activity activity, int color) {
277290
278291 /**
279292 * 设置导航栏颜色,和setNavigationBarTransparent互斥,要么选择自定义导航栏颜色,要么选择导航栏全透明
280- *
281293 * @param activity
282294 * @param color
283295 */
284- public static void setNavigationBarColor (Activity activity , int color ) {
296+ public static void setNavigationBarColorOld (Activity activity , int color ) {
285297 Window window = activity .getWindow ();
286298 window .clearFlags (WindowManager .LayoutParams .FLAG_TRANSLUCENT_NAVIGATION );
287299 int ui = window .getDecorView ().getSystemUiVisibility ();
@@ -302,6 +314,12 @@ public static int getStatusBarHeight(Activity activity) {
302314 return getStatusBarHeight (activity .getWindow ().getDecorView ());
303315 }
304316
317+ /**
318+ * 注意:安卓15及以上不准确,必须 setOnApplyWindowInsetsListener 监控才能获取正确的高度
319+ *
320+ * @param view
321+ * @return
322+ */
305323 public static int getStatusBarHeight (View view ) {
306324 WindowInsetsCompat insets = ViewCompat .getRootWindowInsets (view );
307325 int h = 0 ;
@@ -314,11 +332,17 @@ public static int getNavigationBarHeight(Activity activity) {
314332 return getNavigationBarHeight (activity .getWindow ().getDecorView ());
315333 }
316334
335+ /**
336+ * 注意:安卓15及以上不准确,必须 setOnApplyWindowInsetsListener 监控才能获取正确的高度
337+ *
338+ * @param view
339+ * @return
340+ */
317341 public static int getNavigationBarHeight (View view ) {
318342 WindowInsetsCompat insets = ViewCompat .getRootWindowInsets (view );
319343 int h = 0 ;
320344 if (insets != null )
321- h = insets .getInsetsIgnoringVisibility (WindowInsetsCompat .Type .navigationBars ()).bottom ;
345+ h = insets .getInsetsIgnoringVisibility (WindowInsetsCompat .Type .navigationBars ()).bottom ;
322346 return Math .max (h , getNavigationBarHeightLegacy (view .getContext ()));
323347 }
324348
0 commit comments